Hello Friends,
I'm a new to arduino world. I needed to establish a master slave connection between arduino's using rs-485 protocol.
I succeeded in doing so by connecting the serial ports of both arduino's. But, when i tried to interpret the same code in rs-485 communication using MAX485 between arduino's it doesn't work.
when trying the code using the chip, I uncommitted "digitalWrite(controlPin, HIGH);"
This is the code for my slave arduino(arduino uno):
#define myID 1
#define controlPin 3
int called_ID;
void setup() {
Serial.begin(9600);
//pinMode(controlPin, OUTPUT);
//digitalWrite(controlPin, LOW);
}
void loop() {
called_ID = Serial.read();
//digitalWrite(controlPin, HIGH);
if(called_ID == myID){
Serial.println("" + String(10)+","+ String(20)+","+ String(30)+","+ String(40)+","+ String(50)+","+ String(60));
delay(100);
//digitalWrite(controlPin, LOW);
called_ID = 0;
}
}
This is the code for my master arduino(arduino mega):
#define slave_1 1
#define slave_2 2
#define slave_3 3
#define slave_4 4
#define slave_5 5
#define slave_6 6
#define controlPin 3
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
pinMode(controlPin, OUTPUT);
Serial.println("Setup is ready!");
Serial.println("Waiting for the communication process...");
digitalWrite(controlPin, LOW);
}
void loop() {
read_slave_1();
}
And, read_slave_1() function:
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void read_slave_1() {
//digitalWrite(controlPin, HIGH);
Serial1.write(slave_1);
//digitalWrite(controlPin, LOW);
latchData();
//digitalWrite(controlPin, LOW);
printData();
}
void latchData() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial1.available() > 0 && newData == false) {
rc = Serial1.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void printData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}
}
I couldn't figure out the problem. can anyone please help me in this regards? I would really appreciate it.
Thank you.