// Modbus:
#include <SimpleModbusSlave.h> //http://code.google.com/p/simple-modbus/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
const int HOLDING_REGS_SIZE = 22; // I use 22 registers, first 11 for sendidng info to Mach3
//and next 11 registers for receiving info from Mach3
unsigned int holdingRegs[HOLDING_REGS_SIZE]; // function 3 and 16 register array
int rcd[11]; //holds sending info
int mcd[11]; //holds received info
void setup(){
pinMode(13, OUTPUT); //indicator LED
modbus_configure(&Serial, 115200, SERIAL_8N2, 2, 13, HOLDING_REGS_SIZE, holdingRegs);
//2 is slave address, 13 is indicator led pin (useless)
lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines, turn on backlight
lcd.print("Hello, ARDUINO ");
lcd.setCursor ( 0, 1 ); // go to the next line
lcd.print (" FORUM - fm ");
delay ( 1000 );
}
void loop(){
//Reads buttons:
if(digitalRead(4) == 1) bitSet(rcd[0],0);
else bitClear(rcd[0],0);
if(digitalRead(5) == 1) bitSet(rcd[0],1);
else bitClear(rcd[0],1);
if(digitalRead(6) == 1) bitSet(rcd[0],2); //can be up to 15
else bitClear(rcd[0],2);
//Potens´iometers:
rcd[3] = analogRead(A0);
rcd[4] = analogRead(A1);
//Other variables:
rcd[1] = 0x1111;
rcd[2] = 0x2222;
rcd[5] = 0x3333;
rcd[6] = 32767;
rcd[7] = 12345;
rcd[8] = 54321;
rcd[9] = 0;
rcd[10] = 1;
//Sending and receiving info:
for (int i=0; i < 11; i++)
holdingRegs[i] = rcd[i];
modbus_update();
for (int i=0; i < 11; i++)
mcd[i] = holdingRegs[i+11];
// Writes cordinates to LCD:
float Xpos = mcd[0]+(mcd[1]/100.00);
float Ypos = mcd[2]+(mcd[3]/100.00);
float Zpos = mcd[4]+(mcd[5]/100.00);
lcd.setCursor( 0,0 );
lcd.print(" ");
lcd.setCursor( 0, 0 );
lcd.print("X");
lcd.setCursor( 1, 0 );
lcd.print(Xpos);
lcd.setCursor( 0, 1 );
lcd.print(" ");
lcd.setCursor( 0, 1 );
lcd.print("Y");
lcd.setCursor( 1, 1 );
lcd.print(Ypos);
lcd.setCursor( 0, 2 );
lcd.print(" ");
lcd.setCursor( 0, 2 );
lcd.print("Z");
lcd.setCursor( 1, 2 );
lcd.print(Zpos);
lcd.setCursor(0, 3);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print("A");
}