Sending an array but receiving the opposite

Hello, I'm working on a project which has multiple ultrasonic sensors. With D0-D1 I'm sending chars to another arduino. One is working as a slave and has all the sensors on it. The other one works as the master and drives all the motors. The problem here is that I'm sending for exemple AN and after a few milliseconds, on the master arduino I'm getting NA and 2 white blocs (No idea how to write them). Plus it seems the code creates a delay. I've been stuck on this for a whole day. I've tried a similar code which was working. But I don't understand why it doesn't work now.
the code to send the data on the slave arduino is as this :

#include <NewPing.h>

/* EC-X = Echo capteur X    TC-X = Trigger capteur X
 * G = gauche   C = centre    D = droite           */
#define ECG 2
#define ECC 3
#define ECD 4
#define TCG 6
#define TCC 7
#define TCD 8
#define DISTANCE_RIEN 25
#define MAX_DISTANCE 200 //

#define POWER 12  //Switch on/off du programme

unsigned int distance_gauche, distance_droite, distance_centre;
char valeur[2];

NewPing capteur_gauche(TCG, ECG, 50);
NewPing capteur_droite(TCD, ECD, 50);
NewPing capteur_centre(TCC, ECC, 50);

void setup()
{
  Serial.begin(115200); // Starts the serial communication
  pinMode(POWER,INPUT);
}

void loop() {     
   
  delay (10);                
  distance_gauche = capteur_gauche.ping() / US_ROUNDTRIP_CM; 
  distance_droite = capteur_droite.ping() / US_ROUNDTRIP_CM; 
  distance_centre = capteur_centre.ping() / US_ROUNDTRIP_CM;
  
/*  Envoi en uart de données sur le second arduino: 
 *  G = Gauche, D = Droite, C = Centre (mur), A = Avance  */
  // DISTANCE GAUCHE ONLY
  if((distance_gauche > 0 && distance_gauche < DISTANCE_RIEN)&&(distance_droite == 0 || distance_droite > DISTANCE_RIEN)){
    valeur[0] = 'G';
  }
  // DISTANCE DROITE ONLY
  if((distance_droite > 0 && distance_droite < DISTANCE_RIEN)&&(distance_gauche == 0 || distance_gauche > DISTANCE_RIEN)){
    valeur[0] = 'D';
  }
  // DISTANCE MUR
  if((distance_droite > 0 && distance_droite <= DISTANCE_RIEN)&&(distance_gauche > 0 && distance_gauche <= DISTANCE_RIEN)){
    valeur[0] = 'M';  
  }
  // DETECTE RIEN
  if((distance_droite == 0 || distance_droite > DISTANCE_RIEN)&&(distance_gauche == 0 || distance_gauche > DISTANCE_RIEN)){
    valeur[0] = 'A';
  } 
  // DETECTION DU CAPTEUR DU MILIEU 
  /* Si le capteur détecte qqch c'est un obstacle donc il envoie Y car il detecte qqch
   * Si le capteur détecte rien c'est qu'il n'y a pas d'obstacle donc il renvoie N
   */
  if(distance_centre != 0){
    valeur[1] = 'Y';
  }
  else
  {
    valeur[1] = 'N';    
  }
  Serial.write(valeur,2);
  Serial.println();
}

and the code for the main arduino is this :

#include <Servo.h>

/*  Le moteur de droite est le A, gauche le B
    si (IN1,H / IN2,L) si (IN3,H / IN4,l) alors
    le robot va tout droit                    */
#define IN1 9 // commande demi-pont 1 (A) sur broche 9
#define IN2 10 // commande demi-pont 2 (A) sur broche 10
#define IN3 12 // commande demi-pont 3 (B) sur broche 12
#define IN4 13 // commande demi-pont 4 (B) sur broche 13
#define G1 6 // commande transistor 1 sur broche 6
#define G2 7 // commande transistor 2 sur broche 7
#define ENA 3 // validation pont A sur broche 3
#define ENB 5 // validation pont B sur broche 5

#define VMAX_A 100
#define VROTA_A 110
#define V50_A 80
#define VMAX_B 120
#define VROTA_B 135
#define V50_B 90

#define OFF 0 

Servo monServomoteur; 

bool AVANCER = true;
int angle_rotation, compteur_avance, pause_avance, pos = 0;
char etat[2];
char flag_avance, moteur, tourelle;
long current_time, start_time;

void setup()
{
  Serial.begin(115200); // Starts the serial communication
  pinMode(ENA, OUTPUT); // sets the pin as output
  pinMode(ENB, OUTPUT); // sets the pin as output 
  pinMode(G1, OUTPUT); // sets the pin as output
  pinMode(G2, OUTPUT); // sets the pin as output
  pinMode(IN1, OUTPUT); // sets the pin as output
  pinMode(IN2, OUTPUT); // sets the pin as output
  pinMode(IN3, OUTPUT); // sets the pin as output
  pinMode(IN4, OUTPUT); // sets the pin as output
  digitalWrite(G2, LOW); // relais 2 OFF
  digitalWrite(G1, LOW); // relais 1 OFF
  analogWrite(ENA, 0); // arrĂȘt du moteur A
  analogWrite(ENB, 0); // arrĂȘt du moteur B
  monServomoteur.attach(10); 
  current_time = millis();
  start_time = current_time; 
}

void loop()
{
  /* Lecture de l'état affilié à la course en fonction des capteurs
      A = avance tout droit (si pas de capteurs detectent)
      G = tourne Ă  gauche (seulement si capteur droite detecte)
      D = tourne Ă  droite (seulement si capteur gauche detecte)
      M = mur final (si les deux capteurs detectent)

  */

delay(50);
current_time = millis();
  
if(Serial.available()){
  Serial.readBytes(etat,2);
/*  Serial.print("Etat Ă  : ");*/
  moteur = etat[0];
  tourelle = etat[1];
  Serial.print(moteur);
  Serial.println(tourelle);
/*  Serial.print(" et angle de : ");
  Serial.println(angle_rotation);*/
}

if((etat[0] == 'A')&&(flag_avance >= 3)){
      //VERIFICATION DE LA ROTATION  
      if (angle_rotation != 0) {
        //SI PAS ENCORE AVANCE POUR QUITTER L'ANGLE
        if (AVANCER) {
          if(flag_avance >= 17){
            flag_avance = 0;
            AVANCER = false;            
          }
          else{
          avance(VMAX_A,VMAX_B);
          flag_avance ++;
          }
        }
        //TOURNE A DROITE
        if ((angle_rotation < 0)&&(AVANCER == false)) {
          tourne_droite();
          if(angle_rotation == 0)
          {
            flag_avance = 0;
          }
        }
        //TOURNE A GAUCHE
        if ((angle_rotation > 0)&&(AVANCER == false)) {
          tourne_gauche();
          if(angle_rotation == 0)
          {
            flag_avance = 0;
          }
        }
      }
      else{
        avance(VMAX_A,VMAX_B);
        AVANCER = true;
      }   
}
else{
  if(etat[0] == 'A')
  {
    flag_avance++;
  }
}
// ETAT D ---------------------------------------------------------
if(etat[0] == 'D'){
      //TOURNE A DROITE
      tourne_droite();
      flag_avance = 0;
      AVANCER = true;
}
// ETAT G ---------------------------------------------------------
if(etat[0] == 'G'){
      //TOURNE A GAUCHE
      tourne_gauche();
      flag_avance = 0;
      AVANCER = true;
}
// ETAT M ---------------------------------------------------------
if(etat[0] == 'M'){
    for (pos = 0; pos <= 180; pos += 1) {
      monServomoteur.write(pos);
      if(etat[0] == 'H'){
        for (pos = pos; pos <= pos - 10; pos -= 1){
          monServomoteur.write(pos);
        }
      }      
    }

}
}
int pause() {
  digitalWrite(IN1, LOW); // moteur A tourne dans sens -
  digitalWrite(IN2, LOW);
  digitalWrite(ENA,OFF);
  digitalWrite(IN3, LOW); // moteur A tourne dans sens -
  digitalWrite(IN4, LOW);  
  digitalWrite(ENB,OFF);                                                 
}

int tourne_droite() {
  digitalWrite(IN1, HIGH); // moteur A tourne dans sens -
  digitalWrite(IN2, LOW);
  analogWrite(ENA, VROTA_A); // moteur A vitesse 1/2
  digitalWrite(IN3, HIGH); // moteur A tourne dans sens -
  digitalWrite(IN4, LOW);
  analogWrite(ENB, VROTA_B); // moteur A vitesse 1/2
  angle_rotation += 1;
}

int tourne_gauche() {
  digitalWrite(IN1, LOW); // moteur A tourne dans sens -
  digitalWrite(IN2, HIGH);
  analogWrite(ENA, VROTA_A); // moteur A vitesse 1/2
  digitalWrite(IN3, LOW); // moteur A tourne dans sens -
  digitalWrite(IN4, HIGH);
  analogWrite(ENB, VROTA_B); // moteur A vitesse 1/2
  angle_rotation -= 1;
}

int avance(int vitesse_A,int vitesse_B ) {
  digitalWrite(IN1, HIGH); // moteur A tourne dans sens -
  digitalWrite(IN2, LOW);
  analogWrite(ENA, vitesse_A); // moteur A vitesse 1/2
  digitalWrite(IN3, LOW); // moteur A tourne dans sens -
  digitalWrite(IN4, HIGH);
  analogWrite(ENB, vitesse_B); // moteur A vitesse 1/2
}

for avoiding this do not send

Serial.println();

Explanation - Serial.println() adds two characters to the end of the line - "carriage return, line feed, usually shortened to CRLF, that have no visual representation, hence the two "white blocks". Serial.print() doesn't add these.
C

1 Like

yes, but Serial.print() do not need here too

okay thanks for the serial print ;). I quite got the code working now also by reducing the delay from 50ms to 10

What makes you think there are two bytes available to read at that point?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.