Hello dotn:
I have a short tutorial to help you get started. It would be helpful to print it out as a reference as
you start writing the code into the framework of the modbus program from Zafar. It ends up getting
fairly long. You might just start with making a "Cycle Start" button, and then add more stuff after you
get that part working.
If you have a working knowledge of Arduino I/O codeing, this is the standard Arduino
sketch code sections of: declaration, setup, and "void" loop sections of the modbus program.
Example:
This part is the declaration of pins and variables. After the beginning where the code
has: int t1,t2,t3; (this is the same as A0, A1, A2 for the analog pins.
plus the declaration for input pins;
int inPin2 = 2; //This is digital pin D2
int inPin3 = 3; // this is digital pin D3 etc.
int inPin4 = 4; // ........
The Mega has lots of pins. You can do this up to inPin 25 or 30 if you want.
--------------------------------------------------------------------------------------------------------------------
Save some pins for Outputs in the declaration;
int outPin1 = 13; // D13, this is the built in led pin. You can make it blink on as an output.
int outPin2 = 25; // D25, this is Mega digital pin 25
int outPin3 = 26; // D26
int outPin4 = 27; .....etc.
int buttonState1 = 0; // you will need the declaration of "buttonState" for reading the input state of pins in the "void" section.
int buttonState2 = 0;
int buttonState3 = 0;
---------------------------------------------------------------------------------------------------------------------------
Then in the next area : (this is copied directly from the program to show location)
/* Modbus RTU common parameters, the Master MUST use the same parameters */
enum {
MB_SLAVE = 1, /* modbus slave id */
};
/* slave registers example */
enum {
MB_REG0, // analog pin 0
MB_REG1, // analog pin 1
MB_REG2, // analog pin 2
//
MB_REG3, // digital pin 2
MB_REG4, // digital pin 3
MB_REG5, // digital pin 4
These are the modbus registers listed by number. The program allows up to 40 registers.
You have to label the inputs (as above shown), and the outputs in the same list.
It is not necessary to go all the way to 40 registers, but you can if needs be.
Every comma, underline, semi-colon is absolutely necessary. The program will not
compile is so much as one comma is out of place.
The double slash, // is to precede any comments you want to add for keeping track of what
the program lists are all about.
------------------------------------------------------------------------------------------------------------------------
Now is the "setup" section of the program: The following lines are copied from the program to show
location, and some of my own code, i.e., Cycle Start.......
int regs[MB_REGS]; /* this is the slave's modbus data map */
void setup()
{
pinMode(inPin2, INPUT); // D2, Cycle Start (connect your pushbutton to pin D2.)
pinMode(inPin3, INPUT); // D3, Feed Hold
pinMode(inPin4, INPUT); // D4, Stop
pinMode(inPin5, INPUT); // D5, Reset
pinMode(inPin6, INPUT); // D6, Spindle on/off
This is the standard Arduino sketch pinMode listing for INPUT and OUTPUT
pinMode(outPin1, OUTPUT); // D13, Led pin 13 output
pinMode(outPin2, OUTPUT); // D36,
pinMode(outPin3, OUTPUT); // D37,
pinMode(outPin4, OUTPUT); // D38,
This listing will include all the inputs and outputs you need.
---------------------------------------------------------------------------------------------------------
Now comes the real deal. This is the "void" loop. All your program tasks happen here.
void loop()
{
/* This is all for the Modbus slave */
update_mb_slave(MB_SLAVE, regs, MB_REGS) ;
// analog input pins ----------
regs[0]=analogRead(A0); // this is the area reading the analog inputs.
regs[1]=analogRead(A1);
regs[2]=analogRead(A2);
// ----------------------------
regs[3]=digitalRead(inPin2); // this is the area reading the digital input pins.
regs[4]=digitalRead(inPin3);
regs[5]=digitalRead(inPin4);
regs[6]=digitalRead(inPin5);
regs[7]=digitalRead(inPin6);
Every digital input and output, and every analog input must have it's own register; regs[16] etc.
Here is a short code to turn on a green LED in combination with the Cycle Start button, and turn off the yellow, and red
led's that match the Feedhold and Stop buttons.
This is at the end section of the "void" loop, and before the loop closing bracket.
// Read "Cycle Start" button; if activated, turn on green LED
buttonState1 = digitalRead(inPin2);
if(buttonState1 == HIGH) {
digitalWrite(outPin2, HIGH);
digitalWrite(outPin3, LOW);
digitalWrite(outPin4, LOW);
}
As you can see, I connect a green led to "outPin2"
a yellow led to "outPin3"
and a red led to "outPin4"
You can write similar code yourself, to turn on/off various led's to match the
buttons pushed.
All the stuff in the "void" loop must be inside of loop starting bracket....
void setup()
{
and the ending bracket in the program....
}
---------------------------------------------------
Now you need to write the brains to implement the input buttons. Zafar made sample brains for that already.
Do just a section at a time when you start writing it all in the program. Do a "compile" to find any mistakes before you have
too much code written down.
------------------------------------------------------
Have fun.
John