By reading a value in an array it changes?!? too confused for words :S

I’ve been writing some code for a simple robot, the robot itself is simply a learning platform for my self and things have been going slowly but surly until I hit an odd problem.
The control commands are sent out over serial. They are then check to make sure their at least with in the specified boundaries and then sent on to do what ever I’m telling the robot to do. There are 18 entries in the command array (data*), the main control for the motors that move the robot are in data[1] & data[3] (data[0] & data[2] are not used). These first 4 data commands I’ve managed to assign function too with no problem what so ever, they run a double HBRIDGE through PWM. The problems started when I tried to use data[4] and up, these are all simple on(1) or off(0) commands. The error checking shows me that I’m receiving the right command until I assign it a function. So if I’m simply sending a 0 I can see that I’m sending a 0 by having it print out “0”, if I send a 1 I see a “1” printed out, It doesn’t ever seem to give me a wrong reading. However if I then add to the void commandDispatch for eg.*
if (data[4] = 0){
digitalWrite(led, HIGH);
}
I always see a “1” printed out. if I’m sending a “1” or a “0”
even odder tho is, if I do
if (data[4] = 1{
digitalWrite(led, HIGH);
}
I always see a “1” printed out. even if I’m sending a “0” or a “1”
what’s more the led doesn’t light up.
now if I put
if (data[4] = 5{ //impossible value as command would not be sent passed error checking
digitalWrite(led, HIGH);
}
I always see a “1” printed out. It doesn’t matter if I’m sending a “0” or a “1”
& the led still doesn’t light up doesn’t light up.
here’s the code
```
*String command; // a string to hold the incoming command
boolean commandReceived = false; // whether the command has been received in full

int data[18]; // up to 18 integers of data
int lf = 3;  //Hbridge Pin
int lb = 5;  //Hbridge Pin
int rf = 6;  //Hbridge Pin
int rb = 10;  //Hbridge Pin

int i;

int led = 13;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(lf, OUTPUT); 
  pinMode(lb, OUTPUT);
  pinMode(rf, OUTPUT);
  pinMode(rb, OUTPUT);
digitalWrite(lf, LOW);
digitalWrite(lb, LOW);
digitalWrite(rf, LOW);
digitalWrite(rb, LOW);
pinMode(led, OUTPUT);
}

void loop() {
  // main loop
  handleCommand();
}

void serialEvent(){
  while (Serial.available()) {
  // all we do is construct the incoming command to be handled in the main loop
   
    // get the incoming byte from the serial stream
    char incomingByte = (char)Serial.read();
   
    if (incomingByte == ‘>’)
    {
      // marks the end of a command
      commandReceived = true;
      return;
    }
    else if (incomingByte == ‘<’)
    {
      // marks the start of a new command
      command = “”;
      commandReceived = false;
     
    }
    else
    {
      command += incomingByte;
     
    }
   
  }
}

void handleCommand() {
 
  if (!commandReceived) return; // no command to handle
 
  char cmd[command.length()+1];
  command.toCharArray(cmd, command.length()+1);
  char *token = strtok(cmd, “,”);
 
 
  // data 
  for (i = 0; i < 18; i++) {
   
    if (token) {
      data[i] = atoi(token);
      token = strtok(NULL, “,”);
    }
      // flag that we’ve handled the command
  commandReceived = false;
    commandCheck();
  }
}

void commandCheck() {
 
  if (data[0] < 1000 || data[0] > 3000) {
    Serial.println(“ERROR”);
return; 
}
else if (data[1] < 1000 || data[1] > 3000) {
    Serial.println(“ERROR”);
  return;
}
  else if (data[2] < 1000 || data[2] > 3000) {
  Serial.println(“ERROR”);
    return;
  }
  else if (data[3] < 1000 || data[3] > 3000) {
  Serial.println(“ERROR”);
    return;
  }
  else if (data[4] > 1) {
    Serial.println(“ERROR”);
  return;
  }
  else if (data[5] > 1) {
    Serial.println(“ERROR”);
  return;
  }
  else if (data[6] > 1) {
    Serial.println(“ERROR”);
  return;
  }
  else if (data[7] > 1) {
    Serial.println(“ERROR”);
  return;
  }
  else if (data[8] > 1) {
    Serial.println(“ERROR”);
  return;
  }
  else if (data[9] > 1) {
    Serial.println(“ERROR”);
  return;
  }
  else if (data[10] > 1) {
    Serial.println(“ERROR”);
  return;
  }
  else if (data[11] > 1) {
    Serial.println(“ERROR”);
  return;
  }
  else if (data[12] > 1) {
    Serial.println(“ERROR”);
  return;
  }
  else if (data[13] > 1) {
    Serial.println(“ERROR”);
  return;
  }
  else if (data[14] > 1) {
    Serial.println(“ERROR”);
  return;
  }
  else if (data[15] > 1) {
    Serial.println(“ERROR”);
  return;
  }
  else if (data[16] > 1) {
    Serial.println(“ERROR”);
  return;
  }
  else if (data[17] > 1) {
    Serial.println(“ERROR”);
  return;
  }
else{
  // digitalWrite(led, HIGH);
commandDispatch();
}
}

void commandDispatch() {

int y1 = (data[1]);
    int y2 = (data[3]);
    int Lfy = map(y1,2100,3000,0,255);
    int Rfy = map(y2,2100,3000,0,255);
    int Lby = map(y1,1900,1000,0,255);
    int Rby = map(y2,1900,1000,0,255);

//Complete Stop (DeadZone)

if (y1 >=1901 && y1 <=2099) {
  digitalWrite(lf,LOW);
  digitalWrite(lb,LOW);
}

if (y2 >=1901 && y2 <=2099) {
  digitalWrite(rf,LOW);
  digitalWrite(rb,LOW);
}

//Move Forward

if (y1 >=2100){
  digitalWrite(lb, LOW);
  analogWrite(lf, Lfy);
  }
  else {
    digitalWrite(lf, LOW);
  }
    if (y2 >=2100){
  digitalWrite(rb, LOW);
  analogWrite(rf, Rfy);
  }
  else {
    digitalWrite(rf, LOW);
  }
 
  //Move Backward
 
  if (y1 <=1900){
  digitalWrite(lf, LOW);
  analogWrite(lb, Lby);
  }
  else {
    digitalWrite(lb, LOW);
  }
    if (y2 <=1900){
  digitalWrite(rf, LOW);
  analogWrite(rb, Rby);
  }
  else {
    digitalWrite(rb, LOW);
  }

if (data[4] = 1{
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);

}

//DEBUG
  Serial.print(data[0]);
  Serial.print(data[1]);
  Serial.print(data[2]);
  Serial.print(data[3]);
  Serial.print(data[4]);
  Serial.print(data[5]);
  Serial.print(data[6]);
  Serial.print(data[7]);
  Serial.print(data[8]);
  Serial.print(data[9]);
  Serial.print(data[10]);
  Serial.print(data[11]);
  Serial.print(data[12]);
  Serial.print(data[13]);
  Serial.print(data[14]);
  Serial.print(data[15]);
  Serial.print(data[16]);
  Serial.print(data[17]);
  Serial.println("");
//digitalWrite(led, LOW);
//i=0;
//data[i] = ‘\0’;
}*
```
I’m sorry to ask as I’m sure its something simple but my noobness blinds me from the truth. Thanks in advance.

if (data[4] = 0){

remember, = means to assign. So that is saying assign 0 to data[4] and then, if the result of that is true, execute the iff.

the operator == means check if they're equal. You will almost always want to use this one inside of if statements.

think about it this way: if you see "=", think "assign", if you see "==" think, "are they the same?"

Heh, now I know how much of a fool I am. I been sitting here for hours scratching my head over that. I cant believe I didn't spot it.

DOH!

thanks. sadly im sure ill be back but for now its fixed.

I been sitting here for hours scratching my head over that.

You're not the first and won't be the last, it's the oldest gotcha in the C book.


Rob