Hi everyone, I bought a pair of arduino just yesterday, and I am trying to communicate two arduinos with a RS 485 Serial Communication, one arduino is reading a potenciometer and send the data to the other one who changes the speed of an animations with leds.
I have read a lot of post about this topic and i am planning to do this schematic
http://johanneskinzig.de/_Media/arduinors485_schaltplan_med_hr.jpeg
then i got this code:
Server:
void loop()
{
// send message to slave ("A") with different value
for (int i = 0; i < 5; i++)
{
switch (i)
{
case 0 : Serial.println("A1000"); break;
case 1 : Serial.println("A0100"); break;
case 2 : Serial.println("A0010"); break;
case 3 : Serial.println("A0001"); break;
}
delay(2000);
}
}
and Slave
while (Serial.available())
{
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += 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;
}
}
i also learned that i have to set D13 high or low if i want to send or receive, that is clear for me,
but i dont understand how to use Serial.avaliable, acording with the documentation that function returns 1 when there is some new Data on Port
so if i want to send a message to the server saying sarcastically "hey!, im ready which is he speed?" the server need to have the Serial.avaliable and then we should respond "speed is XX" so now slave has to have the line Serial.avaliable ?
how do i create a good comunication protocol?
thanks