If statement only running once?

I'm trying to make a gas sensor as a 'page' on an lcd. Whenever I move to that page, it updates the output, but it won't keep running. I want it to keep looping through until page != 2 Any thoughts?

else if (Page == 2){
    strip.setPixelColor(3, 0, 0, 0);
    strip.setPixelColor(4, 0, 0, 0);
    strip.setPixelColor(5, 0, 191, 255);
    strip.setPixelColor(6, 0, 0, 0);
    strip.setPixelColor(1, 0, 0, 0);
    strip.setPixelColor(2, 0, 0, 0);
      
      strip.show();
      whiletop:
      gasValue = analogRead(GasSensorPin); // read analog input pin 1
      if (oldGas == gasValue){
      goto whiletop;
      }
      lcd.clear();
      lcd.setCursor(0,0);      
      lcd.print("gas = ");
      lcd.println(gasValue, DEC);
      sensorValue = analogRead(analogInPin);
      Page = map(sensorValue, 0, 1023, 5, 0);
      oldGas = gasValue;
      }

Then you need to show us all the code...

And I wonder if the goto might be the culprit... NO reason to use it in a higher language like C++..

Here’s the code, I don’t usually use goto’s, but every so often I find they have their uses.

PipBoy.ino (4.08 KB)

but every so often I find they have their uses.

As in emulating a while loop, as you are doing?

I don't... Why not just use a do-while-loop?

I tried using something like

while (Page == 2) {

.. .. .. .. .. ..

read what the page is }

inside the else if (Page == 2), but it would only run once.

First, press ctrl+T in the IDE, looks better, doesn't it?

Second try to remove all those goto's. Still think the problem is there.

     whiletop:
      gasValue = analogRead(GasSensorPin); // read analog input pin 1
      if (oldGas == gasValue){
      goto whiletop;
      }

Is just a terrible way of writing

   do{
      gasValue = analogRead(GasSensorPin); // read analog input pin 1
    }
    while(oldGas == gasValue);

Although I have no idea why you want to keep reading the sensor while it's the same as the old reading...

I want to keep re-reading the sensor to update it as quickly as possible, and comparing it to the old value helps make sure it doesnt just update the display and constantly flicker the same number. That way, if there's no change, it won't change anything. And I'll try the do while

Then keep the loop() running and only reprint the value if it is different then the last reading ;)

That's what I thought I was doing with

 gasValue = analogRead(GasSensorPin); // read analog input pin 1
      if (oldGas == gasValue){

If I wrote that how I thought I wrote that, that should keep checking the values, and sending it through only when the number changes. I got it to update itself, but now I have two problems. I have block characters showing up after the display, and it locks up the page, not allowing me to navigate away, I think it has something to do with

 lcd.print("gas = ");
      lcd.println(gasValue, DEC);
      oldGas = gasValue;
      }
    while(Page == 2);

Do I need to insert a read function for Page, like

sensorValue = analogRead(analogInPin);
Page = map(sensorValue, 0, 1023, 5, 0);

Nope, your not. Let's say you have two sensors in front of you (A and B) and a white board. It's your task to write the values onto the board. Like you said, it's a dumb idea to just keep on repeating -read sensor A -clean part of the board -write sensor A value -read sensor B -clean part of the board -write sensor B value -etc

But now you are repeating 1) read sensor A 2) is it the same as last time? if it is, back to 1 3) clean part of the board 4) write the value on the board 5) read sensor B 6) is it the same as last time? if it is, back to 4 7) clean part of the board 8) write the value on the board

But then your stuck at 2 or 6 if the value you read does not change... So it's better to repeat the reading and only act on it if it's different. So 1) read sensor A 2) is it the same as last time? if it is, skip 3 and 4 3) clean part of the board 4) write the value on the board 5) read sensor B 6) is it the same as last time? if it is, skip 7 and 8 7) clean part of the board 8) write the value on the board

That way you just keep on going.

Aka, don't while() it, the loop() will take care of the looping. Just only take action if you need to. In this case, if the value has changed.