Looping conditionally in a case

I have a question; how would I loop a case until the case changes from a button press?

#include <LiquidCrystal.h>

#include <SimpleDHT.h>

int buttonValue;
int previous = HIGH;
int pin = 2;
int screenNumber = 0;

int pinDHT11 = 1;
SimpleDHT11 dht11;

int xValue, yValue;

#define joyX A1
#define joyY A2

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 8, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const int trigPin = 7;
const int echoPin = 6;

long duration;
int distance;

void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input

  Serial.begin(9600);
  pinMode(13, INPUT_PULLUP);
}

void loop()
{
  buttonValue = digitalRead(13);

  if (buttonValue == LOW && previous == HIGH)  // Has a button just been pressed?
  {
    switch (screenNumber)                      // Select the screen to display.
    {
      case 0:
        lcd.clear();
        lcd.print("Joystick control");  // joystick and rgb led page
        delay(250);
        lcd.clear();
        while(buttonValue = LOW);{
            xValue = analogRead(joyX);//Reading X and Y values from joystick
  yValue = analogRead(joyY);
  lcd.setCursor(0,0);
  lcd.print("X Value : ");
  lcd.print(xValue);//Displaying xValue on top
  lcd.setCursor(0,1);
  lcd.print("Y Value : ");
  lcd.print(yValue);//Displaying yValue on bottom
        }
        break;
      case 1:
        lcd.clear();
        lcd.print("Humidity/Temp");    // dht11 temp/humidity module page
      while(buttonValue = LOW);{
         Serial.println("=================================");
  Serial.println("DHT11 readings...");

  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  
  Serial.print("Readings: ");
  Serial.print((int)temperature); Serial.print(" Celcius, ");
  Serial.print((int)humidity); Serial.println(" %");
 
  //Telling our lcd to refresh itself every 0.75 seconds
  lcd.clear();
 
  //Choosing the first line and row
  lcd.setCursor(0,0);
  //Typing Temp: to the first line starting from the first row
  lcd.print("Temp: ");
  //Typing the temperature readings after "Temp: " 
  lcd.print((int)temperature);
  //Choosing the second line and first row
  lcd.setCursor(0,1);
  //Typing Humidity(%): to the second line starting from the first row
  lcd.print("Humidity(%): ");
  //Typing the humidity readings after "Humidity(%): "
  lcd.print((int)humidity);
  delay(750);
      }
        break;
      case 2:
        lcd.clear();
        lcd.print("Light level");       // photoresistor page
        delay(250);
        lcd.clear();
      while(buttonValue = LOW);{
      int sensorValue = analogRead(A0);
 double dV = sensorValue;
 double le = (dV/1023)*100;
 int level = le;
 lcd.clear();
 lcd.setCursor(0, 0);
 lcd.print("LIGHT LEVEL:");
 lcd.print(level);
 lcd.print("%");
 lcd.setCursor(0, 1);
 
 if ((level >= 0) && (level <= 5))
 {
  lcd.print("VERY DARK"); 
 }
 else if ((level > 5) && (level <= 10))
 {
  lcd.print("DARK"); 
 }
 else if ((level > 10) && (level <= 50))
 {
  lcd.print("BRIGHT"); 
 }
 else 
 {
  lcd.print("VERY BRIGHT"); 
 }
 delay(500); 
}
        break;
      case 3:
        lcd.clear();
        lcd.print("Distance");        // ultrasonic sensor page
        delay(250);
        lcd.clear();                  // Clears the screen in preparation of new measurement
      while(buttonValue = LOW);{
        // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  // Prints the distance on the Serial Monitor
  lcd.print("Distance: ");
  lcd.print(distance);
  lcd.print(" cm");
      }
        break;
      case 4:
        lcd.clear();
        lcd.print("IR control");          // infrared controller page
        delay(250);
        lcd.clear();
      while(buttonValue = LOW);{
        
      }
       break;
    }

    screenNumber = (screenNumber + 1 ) % 5;    // Calculate the next screen number
  }

  previous = buttonValue;
}

Not exactly sure what you are trying to do... but just having a quick look through your code there are a number of issues...

= is not the same as ==. When comparing 2 values you should use ==.

Be careful where you put ; in the line above the while statement will not execute the code in {} because you told it to stop with the ;

Be very careful declaring variables inside a switch statement (like temperature above)... you will get some very weird results. If you are going to do this make sure you enclose the full case block inside {}.

Please use the auto format tool (Control-T) to make your code just a bit easy to read.

1 Like

That's a complete while statement that does nothing. Except assign the value of LOW to the variable buttonVakje.

You want == for comparison.

Also, the semicolon ends the statement. Lose it and the stuff in the { braces } will be conditionally executed.

And… if nothing changes buttonValje in the body of the loop, it will either never execute the body or get stuck executing it…

a7

1 Like

And in the while loop this buttonValue = digitalRead(13); may be needed to check on the thing. Otherwise one may become stuck in the while().

1 Like

Thank you so much! I'll test it when I can after I wake up!

Yeah, I had noticed lots of errors already for my sensors. Thank you so much!!!! And I will use ctrl t in the future, I hadn't known about it, sorry! :slight_smile:

Pro tip: You should not try to write code when you are asleep.

2 Likes

yup lmao
I have made some dumb mistakes doing that :pensive: they will haunt me forever

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.