Programme_arduino_esclave 4.99 KB
#include <SoftwareSerial.h>

SoftwareSerial soft1(A3,A2);
SoftwareSerial soft2(6,5);
SoftwareSerial soft3(9,10);

const int in1 = A5;
const int out1 = A4;

const int in2 = 8;
const int out2 = 7;

const int in3 = 11;
const int out3 = 12;

const int in4 = 2;
const int out4 = 3;

int prem = 0;
int papa = 0;

int potar = A0;

void setup()
{
  Serial.begin(9600);
  soft1.begin(9600);
  soft2.begin(9600);
  soft3.begin(9600);

  pinMode(in1,INPUT);
  pinMode(out1,OUTPUT);
  pinMode(in2,INPUT);
  pinMode(out2,OUTPUT);
  pinMode(in3,INPUT);
  pinMode(out3,OUTPUT);
  pinMode(in4,INPUT);
  pinMode(out4,OUTPUT);
  
  digitalWrite(out1,LOW);
  digitalWrite(out2,LOW);
  digitalWrite(out3,LOW);
  digitalWrite(out4,LOW);   
}

void loop()
{
  String msg="";
  char inByte='a';
  int tps;
  int timeout;
  int fin1=0,fin2=0,fin3=0,fin4=0;
  char id = 0b01100001;
  if(digitalRead(in1)==HIGH){
    digitalWrite(out1,HIGH);
    soft1.listen();
     timeout=0;
    tps=millis();
    while(soft1.available()<=0 && timeout<1000){
      timeout= millis() - tps;
      }
    if(timeout<1000){
    fin1=1;
    while(inByte != '\n'){
      inByte = soft1.read();
      if(inByte !='\n'){
        msg += inByte;
        delay(10);
      }
      }
      if(prem==0){
        prem=1;
        papa=1;
        msg += id;
        if((id & 0xF0) ==0x60) msg += '{'; //char pour 123
        else if((id & 0xF0) ==0x40) msg += 0b00000100;
        msg += '\n';
      }
     else msg+= '\n';
    }
      digitalWrite(out1, LOW);
      delay(50);
  }

  else if(digitalRead(in2)==HIGH){
    digitalWrite(out2,HIGH);
    soft2.listen();
    timeout=0;
    tps=millis();
    while(soft2.available()<=0 && timeout<1000){
      timeout= millis() - tps;
      }
    if(timeout<1000){  
    fin2=1;
    while(inByte != '\n'){
      inByte = soft2.read();
       if(inByte !='\n'){
        msg += inByte;
        delay(10);
      }
      }
      if(prem==0){
        prem=1;
        papa=2;
        msg += id;
        if((id & 0xF0) ==0x60) msg += '{'; //char pour 123
        else if((id & 0xF0) ==0x40) msg += 0b00000100;
        msg += '\n';
      }
      else msg+= '\n';
    }
      digitalWrite(out2, LOW);
      delay(50);
  }

else if(digitalRead(in3)==HIGH){
    digitalWrite(out3,HIGH);
    soft3.listen();
    timeout=0;
    tps=millis();
    while(soft3.available()<=0 && timeout<1000){
      timeout= millis() - tps;
      }
    if(timeout<1000){  
    fin3=1;
    while(inByte != '\n'){
      inByte = soft3.read();
       if(inByte !='\n'){
        msg += inByte;
        delay(10);
      }
      }
      if(prem==0){
        prem=1;
        papa=3;
        msg += id;
        if((id & 0xF0) ==0x60) msg += '{'; //char pour 123
        else if((id & 0xF0) ==0x40) msg += 0b00000100;
        msg += '\n';
      }
      else msg+= '\n';
    }
      digitalWrite(out3, LOW);
      delay(50);
  }

  else if(digitalRead(in4)==HIGH){
    digitalWrite(out4,HIGH);
    timeout=0;
    tps=millis();
    while(Serial.available()<=0 && timeout<1000){
      timeout= millis() - tps;
      }
    if(timeout<1000){  
    fin4=1;
    while(inByte != '\n'){
      inByte = Serial.read();
       if(inByte !='\n'){
        msg += inByte;
        delay(10);
      }
    }
      if(prem==0){
        prem=1;
        papa=4;
        msg += id;
        if((id & 0xF0) ==0x60) msg += '{'; //char pour 123
        else if((id & 0xF0) ==0x40) msg += 0b00000100;
        msg += '\n';
      }
      else msg+= '\n';
    }
      digitalWrite(out4, LOW);
      delay(50);
  }
    
  if(fin1==1 || fin2==1 || fin3==1 || fin4==1){
    if((fin1==1 && papa == 1) || (fin1 == 0)){
    timeout=0;
    tps = millis();
    while(timeout<1000){
      digitalWrite(out1,HIGH);
      timeout= millis() - tps;
      if(fin1==1) timeout=0;
      if(digitalRead(in1)==HIGH){
        soft1.print(msg);
        soft1.flush();
        timeout = 1001;
      }
    }
    digitalWrite(out1,LOW);
    }

   if((fin2==1 && papa == 2) || (fin2 == 0)){
    timeout=0;
    tps = millis();
    while(timeout<1000){
      digitalWrite(out2,HIGH);
      timeout= millis() - tps;
      if(fin2==1) timeout=0;
      if(digitalRead(in2)==HIGH){
        soft2.print(msg);
        soft2.flush();
        digitalWrite(out2,LOW);
        timeout = 1001;
      }
    }
    digitalWrite(out2,LOW);
    }
    
    
    if((fin3==1 && papa == 3) || (fin3 == 0)){
    timeout=0;
    tps = millis();
    while(timeout<1000){
      digitalWrite(out3,HIGH);
      timeout= millis() - tps;
      if(fin3==1) timeout=0;
      if(digitalRead(in3)==HIGH){
        soft3.print(msg);
        soft3.flush();
        digitalWrite(out3,LOW);
        timeout = 1001;
      }
    }
    digitalWrite(out3,LOW);
    }

    if((fin4==1 && papa == 4) || (fin4 == 0)){
    timeout=0;
    tps = millis();
    while(timeout<1000){
      digitalWrite(out4,HIGH);
      timeout= millis() - tps;
      if(fin4==1) timeout=0;
      if(digitalRead(in4)==HIGH){
        Serial.print(msg);
        Serial.flush();
        digitalWrite(out4,LOW);
        timeout = 1001;
      }
    }
    digitalWrite(out4,LOW);
    }
  }
}