"While" my statement is not working . . .

Greetings,

I am up against an interesting situation. This takes place in a “while loop” while awaiting a data button to be pressed. In either instance, the loop is to display the output from a gyro chip on a glcd screen. What I want to happen is for a graphics loop to run when the range of the angle is small and for a text screen to appear when the data is outside the range of the graphics loop. I have tried juxtaposing the “if” statements to no avail. Here is what is currently happening:

If the data is within the range of -9 to 9 degrees when the loop is running, the graphics “if” statement is called up. When the angle increases or decreases outside of that range, instead of going to the text screen (the text “if” statement), the screen goes blank. It will stay blank until the range is, once again, within the -9 to 9 area.

If the data is outside of the range of -9 to 9 degrees when the loop is running, the text “if” statement is called up. As long as I stay (<-9 || >9), this display is maintained. As soon as I enter the -9 to 9 degrees range, the graphics if statement is called up but will not return to the text if statement when the conditions have changed.

So, in summary, I can go from the text if statement to the graphic if statement but I cannot return to the text if statement. In my mind, with both statements in the “while” loop, they should be run as conditions change. Please help me determine the (I am certain) simple mistake I am making.

Respectfully - Baran

  while (digitalRead(resetButton) == HIGH )  //  For as long as the data button is NOT pressed, do the following:
  { 
  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);   // gather data from all axis'
  roundUp = int(ax/191.1);  // interpret "X" data as 0 - 90 by dividing by 191.1 - The denominator "should" be 180, but I guess there are internal discrepencies.
  counter = abs (roundUp);  //  Create an absolute value of the -90 to 90 range of data.

if (roundUp < -9 || roundUp > 9)  //  If the angle lies outside of the range (-9 to 9), print out "Angle: XXX Degrees."
  {
  display.display();  
  display.clearDisplay();
  display.invertDisplay(false);
  display.setTextSize(2);
  display.setCursor(0,0);
  display.print("Angle: "); 
  display.println(roundUp);
  display.setCursor(0,16);
  display.print(" Degrees.");
  delay(500);
  }  //  End of numeric display loop
  
  if(roundUp >= -9 && roundUp <=9) //  If the angle lies within the range (-9 to 9) display a graphic representation of this situation. (Graphics to follow;).
  {
  display.display();
  display.clearDisplay();
  //display.invertDisplay(false);
  display.setTextColor(BLACK);
  display.setCursor();
  //roundUp = abs(roundUp);
  display.print(roundUp);
  display.display();
  delay(1000);
  } //  End of graphic display function
  
  } //  End of the While loop.

Should you be setting text color in the other if statement ?

Yours, TonyWilk

Way 2 tests ? The value is either going to be in one range or the other so why not use if/else ?

UKHeliBob: Way 2 tests ? The value is either going to be in one range or the other so why not use if/else ?

Any, why the same code in the if block and the else block. Put the common code before and/or after the if/else statement that decide which non-common code to execute.