unexpected bool behaviour

Hi All,

I’m getting a really strange result when trying to detect a state change.

This is running on a Teensy 3.6 fyi

The suspect is:

void blockChange()
{
//find out if any block has changed 
  for (int i=0;i<NumSensors;i++) {
    if (ledState[i] =! previousBlockArray[i]) {
      hasChanged[i] = true;
    }
    previousBlockArray[i] = ledState[i];
  
    //end for loop

    if (mode == 'c' && !hasChanged[i] && timer > timeout)  {
      mode = 'a';
    }
    else if (mode == 'c' && hasChanged[i]){
  //a block has been moved, so reset the timer
      timer = 0;
    }
    else if (mode == 'a' && hasChanged[i]){
      mode = 'b';
      timer = 0;
    }
  }
  
  timer++; // timer will increment one every time the main loop, calls blockChange.
  Serial.print("timer= ");
  Serial.println(timer, DEC);
  Serial.print("block1= ");
  Serial.println(ledState[0]);
}

ledState goes between 1 and 0 with every loop, and I cannot for the life of me work out why.

This is what i’m getting at the console (note: there is no pulldown on sensor 1, but that should have no impact on the weirdness i’m seeing) :

Mode = c
sensor 0 Value = 0
sensor 1 Value = 301
color not detectable
color not detectable
timer= 1
block1= 0
Mode = c
sensor 0 Value = 0
sensor 1 Value = 269
color not detectable
color not detectable
timer= 1
block1= 1
Mode = c
sensor 0 Value = 0
sensor 1 Value = 246
color not detectable
color not detectable
timer= 1
block1= 0

and the full code for context.

code @ github

any advice that you can give me would be greatly appreciated. thanks

Quick glance

    previousBlockArray[i] = ledState[i];
  
    //end for loop

    if (mode == 'c' && !hasChanged[i] && timer > timeout)  {

rings an alarmbell in my head - because you have a comment that says that's the end of the for loop, yet the for loop doesn't end for another 13 lines....

if (ledState[i] =! previousBlockArray[i]) {

Do you mean ledState not equal to previousBlockArray? The syntax is !=. The way it is written, it says ledState equal the logical inverse (not) of previousBlockArray.

Oh man... thank you. that's fixed the flipping behaviour. But the timer is still stuck at 1.

I've gotta run now. but will look back over it shortly. THANK YOU!

DrAzzy:
Quick glance

    previousBlockArray[i] = ledState[i];

//end for loop

if (mode == 'c' && !hasChanged[i] && timer > timeout)  {




rings an alarmbell in my head - because you have a comment that says that's the end of the for loop, yet the for loop doesn't end for another 13 lines....

Oh, yes sorry, originally the hasChanged was not an array, just a single bool, and I thought that might have been the issue, I will get my comments right. thanks for your attention.