Arduino, problème communication RX TX

Bonjour à tous !

Il m'arrive quelque chose de très étrange dans mon projet (mais surement logique au final =p) pour lequel j'ai besoin de faire communiquer 2 arduinos ensemble.

Pour être plus précis, j'ai un master, qui envoie un message à un slave.

Voici le code du master :

int buttonPin1 = 2;
int buttonPin2 = 3 ;

int ledPin1 = 4;
int ledPin2 = 5;
int ledPin3 = 6;
int ledPin4 = 7;
int ledPin5 = 8;
int ledPin6 = 9;
int ledPin7 = 10;
int ledPin8 = 11;
 
int diff;
int nb;

int buttonState1 ;
int buttonState2 ;

int lastButtonState1 = 0;     
int lastButtonState2 = 0;     

void setup()

{

pinMode(buttonPin1,INPUT);
pinMode(buttonPin2,INPUT);
     
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
pinMode(ledPin4,OUTPUT);
pinMode(ledPin5,OUTPUT);
pinMode(ledPin6,OUTPUT);
pinMode(ledPin7,OUTPUT);
pinMode(ledPin8,OUTPUT);


 
 Serial.begin(9600);
 
}

/**********************************/
void loop()

{
 
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
  
/***************GERER LE KEEP PRESSED*******************/



if (buttonState1 != lastButtonState1) {
  
     if (buttonState1 == HIGH) {
     diff = diff + 1 ;}
     }

if (buttonState2 != lastButtonState2) {
  
     if (buttonState2 == HIGH) {
     nb = nb +1 ;}
     }

 


if (diff == 1)
{
digitalWrite(ledPin6,HIGH);
digitalWrite(ledPin7,LOW);
digitalWrite(ledPin8,LOW);
Serial.print('f');
}
else if (diff == 2)
{
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,HIGH);
digitalWrite(ledPin8,LOW);
Serial.print('m');
 }
else if (diff == 3)
{
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);
digitalWrite(ledPin8,HIGH);
Serial.print('d');
 }
else if (diff == 4)
{
digitalWrite(ledPin6,HIGH);
digitalWrite(ledPin7,LOW);
digitalWrite(ledPin8,LOW);
diff= 1;
}

if (nb== 2)
{
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
Serial.print('2');
}
else if (nb== 3)
{
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
Serial.print('3');
 }
else if (nb== 4)
{
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,HIGH);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
Serial.print('4');
 }
else if (nb== 5)
{
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,HIGH);
digitalWrite(ledPin5,LOW);
Serial.print('5');
 }
else if (nb== 6)
{
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,HIGH);
Serial.print('6');
 }
else if (nb== 7)
{
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
nb= 2;
}

lastButtonState1 = buttonState1;
lastButtonState2 = buttonState2;


    
delay(100);

}

Et celui du slave !

int nb ;

int ventPin1 = 8;     
int ventPin2 = 9;    
int ventPin3 = 10;    
int ventPin4 = 11;     

   
int solPin = 12;   

int relay2 = 13 ; 

int buttonState1 ;
int buttonState2 ;
int buttonState3 ;
int buttonState4 ;
int buttonState5 ;
int buttonState6 ;

int buttonPin1 = 2 ;
int buttonPin2 = 3 ;
int buttonPin3 = 4;
int buttonPin4 = 5 ;
int buttonPin5 = 6 ;
int buttonPin6 = 7 ;



 
void setup()
{
  
Serial.begin(9600);

pinMode(relay2,OUTPUT);

pinMode(ventPin1,OUTPUT);
pinMode(ventPin2,OUTPUT);
pinMode(ventPin3,OUTPUT);
pinMode(ventPin4,OUTPUT);


pinMode(solPin,OUTPUT);

pinMode(buttonPin1,INPUT);
pinMode(buttonPin2,INPUT);
pinMode(buttonPin3,INPUT);
pinMode(buttonPin4,INPUT);
pinMode(buttonPin5,INPUT);
pinMode(buttonPin6,INPUT);

digitalWrite(ventPin1,HIGH);
digitalWrite(ventPin2,HIGH);
digitalWrite(ventPin3,HIGH);
digitalWrite(ventPin4,HIGH);

digitalWrite(relay2,HIGH);
digitalWrite(solPin,LOW);



}

void loop()
{



if (Serial.available()>0){
    if(Serial.read() == '2')
    {
     nb=2;
     Serial.println('2');}
    else if(Serial.read() == '3')
    {
     nb=3;
     Serial.println('3');}
     else if(Serial.read() == '4')
    {
     nb=4;
     Serial.println('4');}
     else if(Serial.read() == '5')
    {
     nb=5;
     Serial.println('5');}
     else if(Serial.read() == '6')
    {
     nb=6;
     Serial.println('6');}
  }
  
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);
buttonState6 = digitalRead(buttonPin6);
        
  

if (nb == 2) {
  
digitalWrite(ventPin1,LOW);
digitalWrite(ventPin2,LOW);
digitalWrite(ventPin3,LOW);
digitalWrite(ventPin4,LOW);

}
else if (nb == 3){

digitalWrite(ventPin1,LOW);
digitalWrite(ventPin2,LOW);
digitalWrite(ventPin3,LOW);
digitalWrite(ventPin4,HIGH);

}
else if (nb == 4){

digitalWrite(ventPin1,LOW);
digitalWrite(ventPin2,LOW);
digitalWrite(ventPin3,HIGH);
digitalWrite(ventPin4,HIGH);

}
else if (nb == 5){
  
digitalWrite(ventPin1,LOW);
digitalWrite(ventPin2,HIGH);
digitalWrite(ventPin3,HIGH);
digitalWrite(ventPin4,HIGH);

}
else if (nb == 6){
  
digitalWrite(ventPin1,LOW);
digitalWrite(ventPin2,HIGH);
digitalWrite(ventPin3,HIGH);
digitalWrite(ventPin4,HIGH);

}
else if (nb == 7){
nb=2;
}

if (nb == 6) {
  if ((buttonState1 == HIGH) && (buttonState2 == HIGH)&&(buttonState3 == HIGH) &&(buttonState4 == HIGH) && (buttonState5 == HIGH)&&(buttonState6 == HIGH))
  {
  

  Serial.println("GAGNE 6J");
  digitalWrite(relay2,LOW);
  digitalWrite(solPin,HIGH);
  delay(2000);
  digitalWrite(solPin,LOW);
  } 
  else {}
}
else if (nb == 5){
if ((buttonState1 == HIGH) && (buttonState2 == HIGH)&&(buttonState3 == HIGH) &&(buttonState4 == HIGH) && (buttonState5 == HIGH))
  {
  

  Serial.println("GAGNE 5J");
  digitalWrite(relay2,LOW);
  digitalWrite(solPin,HIGH);
  delay(2000);
  digitalWrite(solPin,LOW);
  
  } 
  else {}
}
else if (nb == 4){
if ((buttonState1 == HIGH) && (buttonState2 == HIGH)&&(buttonState3 == HIGH) &&(buttonState4 == HIGH) )
  {
    

  Serial.println("GAGNE 4J");
  digitalWrite(relay2,LOW);
  digitalWrite(solPin,HIGH);
  delay(2000);
  digitalWrite(solPin,LOW);
  
  } 
  else {}
}
else if (nb == 3){
if ((buttonState1 == HIGH) && (buttonState2 == HIGH)&&(buttonState3 == HIGH) )
  {
    
  Serial.println("GAGNE 3J");
  digitalWrite(relay2,LOW);
  digitalWrite(solPin,HIGH);
  delay(2000);
  digitalWrite(solPin,LOW);
  
  } 
  else {}
}
else if (nb == 2){
if ((buttonState1 == HIGH) && (buttonState2 == HIGH))
  {
   
Serial.println("GAGNE 2J");
digitalWrite(solPin,HIGH);
digitalWrite(relay2,LOW);
delay(2000);
digitalWrite(solPin,LOW);
  exit(0); 
  } 
  else 
  {
  }
}
else if (nb == 1){
if (buttonState2 == HIGH) 
  {
   
Serial.println("GAGNE 1J");
digitalWrite(solPin,HIGH);
digitalWrite(relay2,LOW);
delay(2000);
digitalWrite(solPin,LOW);
  exit(0); 
  } 
  else 
  {
  }
}

//Serial.println(buttonState1);
//Serial.println(buttonState2);
Serial.println(nb);
delay(100);
   
}

Bon, tout ne sert pas forcément, mais on peut se focaliser sur la partie Serial.print pour la communication.

Mes 2 arduinos ont bien un GND commun.

Mon master envoie 2, 3, 4, 5 ou 6 à mon slave qui en fonction, ouvre plus ou moins de canaux sur un relais 4 canaux.

Mon fil jaune, pour le TX fait environ 4 mètres de long.

J'ai un bouton sur mon master pour envoie ces fameux 2,3 ... au slave.

Et voilà ce qu'il se passe :

Quand j'appuie une fois pour passer ma variable nb à 2 et l'envoyer à mon slave, il faut attendre 1 minute 20 secondes, 1 minute 15 secondes, 1 minute 13 secondes ... donc quasi toujours le même temps !

Par contre, si j'appuie à nouveau pour passer nb à 3, ça se fait instantannément !!! Et à chaque fois !!!

Pour passer nb à 4 et l'envoyer, 50 secondes, 1 minute 20 secondes... donc quasi encore toujours le même temps.

Par contre pour passer à 5 et à 6, trop long, je n'ai même pas chonométré !

Je me doute qu'il y a une explication, mais j'amerai la connaître =p

Et surtout, pouvoir passer ma variable nb à 5 et 6 sans attendre trop longtemps !

Merci d'avance à tous.

Je pense que c'est dû à des rebonds de ton bouton, qui font que tes nombres diff et nb prennent des valeurs incohérentes. Utilise la bibliothèque de bricoleau pour gérer ça. Au besoin contacte-le pour lui demander de l'aide pour l'utiliser.

Bonjour,

Il y a un problème dans ton programme de réception. Si le test est faux tu fais plusieurs Serial.read() or le premier Serial.read() a dépiler le caractère donc les tests suivants sont faux.

Il faut lire le caractère dans une variable puis faire le test sur cette variable.

void loop()
{
  if (Serial.available() > 0) {
    char rec=Serial.read();
    if (rec == '2')
    {
      nb = 2;
      Serial.println('2');
    }
    else if (rec == '3')
    {
      nb = 3;
      Serial.println('3');
    }
    else if (rec == '4')
    {
      nb = 4;
      Serial.println('4');
    }
    else if (rec == '5')
    {
      nb = 5;
      Serial.println('5');
    }
    else if (rec == '6')
    {
      nb = 6;
      Serial.println('6');
    }
  }

  // ...

C'est exactement ça Kamill ! Merci beaucoup !

Ok donc ça fonctionne avec la modification de kamill.

Par contre, c'est quand même vachement long à changer !

Environ 2 minutes.

Et ça ne fonctionne jamais pour nb ==6, sachant que j'ai corrigé l'erreur suivante :

digitalWrite(ventPin1,LOW); ---> digitalWrite(ventPin1,HIGH);
digitalWrite(ventPin2,HIGH);
digitalWrite(ventPin3,HIGH);
digitalWrite(ventPin4,HIGH);

Est-ce normal ?

Si je change sur le master puis que je reboot le slave, il reboot bien instantanément avec la bonne valeur de nb. Sauf pour ce fameux nb ==6 =p

Merci !

question bête, ton master et/ou ton salve ne serais pas branché sur un pc avec la console ouverte des fois ?

quelle version IDE?
quel windaub?
ca ne serait pas une version IDE avec cloud?

Non je me suis déjà fait avoir avec la console =p

Rien, ni mon Slave, ni mon Master ne sont reliés au PC. Les 2 arduinos tournent sur secteur.

Mon Master change bien ma variable nb quand j'appuie sur le bouton (je le vois bien avec mes LED indicatrices) mais mon Slave ne change pas, ou alors il est trèèèèèèès long.

Par contre si je le reboot, il prend directement au démarrage la valeur correcte de nb.

IDE 1.6.9 sans cloud.

Windows 10 Famille 64 bits.