12. Networking and Communications

Now, we work to make 3 pcb speaking together

i use arduino uno, and the TX and RX ports.

principles

On Arduino is the master and the two others are slaves.

To send code to the Arduino, you need to unplug all wires but the usb

wire

So we need 3 codes : one for each arduino.

master

`

//Arduino1

void setup()  {
  pinMode(13,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  Serial.write(0);
  delay(500);
  Serial.write(1);
  delay(500);
  Serial.write(2);
  delay(500);
 digitalWrite(13,HIGH);
 delay(500);
 digitalWrite(13,LOW);
 delay(500);



}

Use serial.write and not serial.print

Slave 1

//Arduino2

void setup()  {
  pinMode(13,OUTPUT);
  Serial.begin(9600);
}

void loop() {
char c;
 if(Serial.available()>0){
  c=Serial.read();
 if(c==1){
  digitalWrite(13,HIGH);
  }
 if(c==0){
  digitalWrite(13,LOW);
  }
if(c==2){
 digitalWrite(13,LOW);
 }
}

}

Slave 2

use byte if you don’t want to use " " to send the information. If you want to use " ", you can use char. if you don’t, you’ll have some misunderstanding with the ascii code.

byte c;

//Arduino2

void setup()  {
  pinMode(13,OUTPUT);
  Serial.begin(9600);
}

void loop() {

 if(Serial.available() > 0){
         c = Serial.read();

         if(c == 1){
          digitalWrite(13,HIGH);
          }

         if(c == 0){
          digitalWrite(13,LOW);
          }

        if(c == 2){
         digitalWrite(13,LOW);
         }
  }

}

Slave 3

byte c;

//Arduino3

void setup()  {
  pinMode(13,OUTPUT);
  Serial.begin(9600);
}

void loop() {

 if(Serial.available() > 0){
         c = Serial.read();

         if(c == 1){
          digitalWrite(13,LOW);
          }

         if(c == 0){
          digitalWrite(13,LOW);
          }

        if(c == 2){
         digitalWrite(13,HIGH);
         }
  }

}