I want to send data between Arduino Uno and Mega via MAX485, when.
When I set the data Enable pin to HIGH on Mega (to transmit data) and set the same pin on UNO to LOW (to receive data) I can read data from Mega into Uno properly. However, but when I want to do it vice versa (send data from uno to mega) can't get any data from Uno into Mega. Tried different pins on both boards but still no answer. This
This problem happens when I want to change data Enable pins to low or high inside loop(), I mean when I use for example Uno as permanent master and mega as permanent slave, it works properly but when I try to change them from slave to master on runtime, data cannot be sent from uno to mega! PS
PS: Im newbie on RS485 and MAX485. Here is my code in MEGA side:
#include <SoftwareSerial.h>'
SoftwareSerial mySerial(2, 3);
String inputS = "";
boolean stringComplete = false;
int i = 0;
bool b= false;
void setup() {
mySerial.begin(9600);//Using SoftwareSerial Port
Serial.begin(9600);
pinMode(8, OUTPUT);//DE/RE Controling pin of RS-485
digitalWrite(8, HIGH);//DE/RE=HIGH Transmit Enabled M1
}
void loop() {
mySerial.println("MEGA:Hi");
Serial.println("Hi Sent");
digitalWrite(8, LOW);
CheckAndReceive();
if(stringComplete)
{
stringComplete = false;
inputS.trim();
Serial.print(inputS);
Serial.println(": " + String(i++));
inputS = "";
}
digitalWrite(8, HIGH);
delay(1000);
}
void CheckAndReceive()
{
while (mySerial.available()) {
// get the new byte:
char inChar = (char)mySerial.read();
// add it to the inputString:
inputS += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
This code is supposed to send a string to UNO and listen to it.
UPDATE: UNO cannot send data to MEGA at all. I think there is a problem with my UNO