Compare two string in a while loop

hi, first, I'm french so please don't takke a look at my faults :slight_smile:

I'm working on a projects with an Arduino Uno :
I read the tension in Analog 3 ans if it's more than 2500 mV, i put a 1 else, i put a 0.
Here is my program :

//variable stockant la valeur lue sur le CAN
int valeurLue;
int i;
char motInter[] = "0000";
String motRecu ;
String motFinal ;
char test_depart[] = "1111";
char test_fin[] = "0000";

//résultat stockant la conversion de valeurLue en Volts
float tension;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.println("Attente");
  Serial.println(test_depart);
}

void loop()
{
    
    //boucle tant que l'émission n'a pas démarrée
    while (strncmp(motInter,test_depart,4) != 0) {
      Serial.println("Attente");
      Serial.println(strncmp(motInter,test_depart,4));
      //On reinitialise le mot intermediaire
      char motInter[] = "1111";
      char test_depart[] = "1111";
      //Pour le capter à nouveau
      for (i=0; i<4; i++) {
          delay (1000);
          valeurLue = analogRead(3);
          //Serial.println(valeurLue);
          tension = valeurLue * 4.88;
          //Serial.println(tension);
          if (tension < 2500) {
            motInter[i] = '0';
      }
      Serial.println(motInter);   
    }
    }
    String motRecu = String(motInter);
    //Une fois que l'émission a commencé on attend la fin
    while (strcmp(motInter,test_fin) != 0) {
      Serial.println("emission en cours");
      char motInter[] = "0000";
      for (i=0; i<4; i++) {
        delay(2000);
        valeurLue = analogRead(3);
        //Serial.println(valeurLue);
        tension = valeurLue * 4.88;
        //Serial.println(tension);
        if (tension > 2500) {
          motInter[i] = '1';
        }
      }
      motRecu.concat(motInter);
    }
      
        
    Serial.println("FIN");
    Serial.println(motRecu);
    motFinal = motRecu.substring(4 , motRecu.length()-4);
    Serial.println(motFinal);
    
    //on ne fait plus rien
    while(true){
      Serial.println("rien");
    }
   

}

So the problem is in the first while loop : it's never go out of it even if motInter = "1111" !
But if i initialize it as "1111" directly, it don't go inside the while loop.

I don't understand what's the problem, maybe i don't understand how arduino change a char type variable.

Thanks for you help !

You have three variables named motInter. One is global scope and the others have local scope inside your while loops. Inside your while loops you change the value of the local variable and in your loop test you compare the global variable. Remove the local variable declarations and things should improve.

Your WHILE test looks very complicated.

Can you write a simpler sketch that just uses a single character and get that to work before moving on to longer character arrays.

And if I was writing that code I do the strncmp() separately from the WHILE so that I could print the result for debugging purposes.

Also you have char test_depart[] = "1111"; within the WHILE loop. That creates another variable of that name which is NOT the variable that is used in the WHILE test. You may not need that line at all. At the very least delete the "char" from it.

...R

PS I hadn't spotted the othe local defintions that @DavidOConnor mentions.

Ok but how can I reinitialyze the value of motInter global variable inside the void loop without declare a local variable ?

Here is a program that just use a single caracter :

int valeurLue;
float tension;
char bitlu;

void setup() {
  Serial.begin(9600);
  Serial.println("attente");
}

void loop() {
  
  //initialize
  valeurLue = analogRead(3);
  tension = valeurLue * 4.88;
  if (tension > 2500) {
    bitlu = '1';
  }
  else {
    bitlu = '0';
  }
  
  //while loop
  while (bitlu != '1') {
    delay(1000);
    valeurLue = analogRead(3);
  tension = valeurLue * 4.88;
  Serial.println(tension);
  if (tension > 2500) {
    bitlu = '1';
  }
  else {
    bitlu = '0';
  }
  Serial.println(bitlu);
  }
  
  Serial.println("fin du test");
  delay(5000);
}

For example, instead of...

char motInter[] = "1111";

Do this...

strncpy(motInter, "1111");

Perfect ! It's works !
Thank you for this very fast help, and if I had another problem, I know where ask some help :slight_smile:

I'm gonna make the while loop a bit more simple !