setFaceColor behaving differently in setup() vs elsewhere?


#1

So I’m using a fairly simple bit of code to set the face lights to different levels of dimness using a FOREACH_FACE loop. It looks like this:

FOREACH_FACE(f){
    setFaceColor(f,dim(WHITE,51*f));
  }

When I run it in setup it works perfectly for any color I want. The P0 light is off, the P5 light is full strength, and so on. But later in my code I change the color within an if/else statement, and now I’m getting a different result. Here’s the code:

FOREACH_FACE(f){
    if(getLastValueReceivedOnFace(f) == 0 && !isValueReceivedOnFaceExpired(f)){
      //you have been commanded to be RED
      setFaceColor(f,dim(RED,51*f));
      gameState = 3;
    } else if (getLastValueReceivedOnFace(f) == 2 && !isValueReceivedOnFaceExpired(f)){
      setFaceColor(f,dim(GREEN,51*f));
      gameState = 3;
    } else if (getLastValueReceivedOnFace(f) == 4 && !isValueReceivedOnFaceExpired(f)){
      setFaceColor(f,dim(BLUE,51*f));
      gameState = 3;
    }
}

This results in the brightest face turning the requested color, but the other 5 stay white (I can’t say if the off face has changed, but still). It seems like the loop is being interrupted, and only one value is changing while the others stay the same?


#2

After puzzling with this, I suddenly realized it was an incredibly obvious issue. The setFaceColor command, which needs to run 6 times, was within the if/else statement, and therefore was only recoloring on the particular instance where a neighboring value was found. To fix this, I created a second FOREACH_FACE loop outside the first, and did the coloring in there once I had received my color (which I just stored in a variable in between).

Turns out it wasn’t as confounding a problem as I first thought.


#4

#5

It sounds like the Blinks was following the correct commands, but the logic was a little wonky…
Here is a bit of code for a structure I typically follow when checking my neighbors

FOREACH_FACE(f){
  if (!isValueReceivedOnFaceExpired(f)) { 
    // a neighbor is present on face f awake
    byte neighbor = getLastValueReceivedOnFace(f);
    
    // now do any logic we need to do with our valid neighbor value
    if(neighbor == <INSERT VALUE YOU ARE CHECKING FOR>) {
      //...
    }
  }
}