[solved]cant get serial read to work

hi

ik have a tricky question, cant get my arduino to understand the code i send to it

here is (a part of) my script:

void serieel(){
if (Serial.available() > 0) {
  char incoming[4] ;
    while(Serial.available() < 4) {
      ;
    }
    for (int i=0; i < 4; i++) {
      incoming[i] = Serial.read();
    }
  
if (incoming == "9130"){ w13 = 0 ;}
if (incoming == "9131"){ w13 = 1 ;}
if (incoming == "5010"){ sb1 = 0 ;}
if (incoming == "5011"){ sb1 = 1 ;}
if (incoming == "5020"){ sb2 = 0 ;}
if (incoming == "5021"){ sb2 = 1 ;}
if (incoming == "5030"){ sb3 = 0 ;}
if (incoming == "5031"){ sb3 = 1 ;}

       }
 
  }

if i type the numbers in the serial monitor i can return them on the screen with Serial.print(incoming) but i can’t use the numbers to change the variables…
do you guys have any idia’s??

greetz

if (incoming == "9130"){ w13 = 0 ;}

The variable incoming is an array. An array can never equal a character constant.

You can use strcmp to compare the contents of an array to a character constant (or another array). Before you can do this, though, you need to NULL terminate the incoming array, which will require that you change the size of the array to include room for the NULL.

you cant compare a char array with ==

you need int strncmp ( const char * str1, const char * str2, size_t num );

if ( strncmp(incoming, "9130", 4) == 0 ) w13 = 0 ;

and that for all of them.

it works :smiley:

thank you guys