Having a problem with a digitalRead in a Case

LED flashing program. 6 different modes, all run about 1-2 seconds, mode is changed in an ISR. Mode 7, does all 6 modes, 1 at a time, it takes say 10 seconds to complete a mode 7. In case there was a Mode change during that, I added a check at various steps to see if the mode changed from 7, and if so, {break;} from mode 7. Longest wait time is about 1 second, acceptable.

Thought, oh, what if the main ON switch went off during mode 7. Better add a check on that too. So I added an OR to that Mode change line- if Mode is NOT 7 anymore, OR, if the Master ON is low, {break;}. It would not work properly though. Added some serial writes, sure enough, in Mode 7, Master = HIGH, it always did the break. Hmmm… Moved the checks in the OR statement back and forth, no. Took out the check for Mode 7, just a read on the master, that did not work. So I added a 2-step check- set y= digitalRead(master), then used that y as the check, and THAT worked.

Any idea why I cannot test a digitalRead directly in a Case? Vs having to write that to a variable then check that variable??

 if (digitalRead(FRONTMASTER) == HIGH && digitalRead(REARMASTER) == LOW){                     // Front flash mode needs to be HIGH.  If Rear is off, only flash the fronts of course
  switch (MASTERMODE){
    case 1: TRIPLEX(CHAN1,CHAN2,CHAN3,CHAN4, 'nil', 'nil', 'nil', 'nil');           break;    //X-triple flash
    case 2: COMET(CHAN1,CHAN2,CHAN3,CHAN4, 'nil', 'nil', 'nil', 'nil');             break;    //ALL triple flash
    case 3: TRIPLETOPBOTTOM(CHAN1,CHAN2,CHAN3,CHAN4, 'nil', 'nil', 'nil', 'nil');   break;    //Top-Bottom triple flash
    case 4: TRIPLELEFTRIGHT(CHAN1,CHAN2,CHAN3,CHAN4, 'nil', 'nil', 'nil', 'nil');   break;    //left-Right triple flash
    case 5: FANCYWIGWAG(CHAN1,CHAN2,CHAN3,CHAN4, 'nil', 'nil', 'nil', 'nil');       break;    //CW, comet, CCW, comet
    case 6: CIRCLES(CHAN1,CHAN2,CHAN3,CHAN4, 'nil', 'nil', 'nil', 'nil');           break;    //LL RR LL RR LL RR ALL ALL 
    case 7: for (z=0;z<3;z++){TRIPLEX(CHAN1,CHAN2,CHAN3,CHAN4, 'nil', 'nil', 'nil', 'nil');}   //ALL modes, check for a mode change between each mode instead of at end
 //            y = digitalRead(FRONTMASTER);                                                  //these 2 lines make it work
 //            if (y == 0)                          {break;}
              if (MASTERMODE !=7  || digitalRead(FRONTMASTER) == 0)   {break;}  //this does NOT work

other modes below, identical to above, just different names

You are doing “switch(MASTERMODE)”.

You are executing the case for MASTERMODE == 7.

How could the expression “MASTERMODE != 7” EVER possibly evaluate to true???

Ray L.

Yes. Mastermode, is set in an ISR so it is possible to be changed in the middle of that case. It works

The issue is still why I cannot directly test a digitalread rather, need to write that to an int first then test the int.

You absolutely CAN directly test the result of a digital read. Your problem, whatever it is, lies elsewhere., probably in the code you didn't show...

Regards, Ray L.