whats happening here.

can someone explain (breakdown)what is happening in this code.

void settingsChangeUnits() {
  int selected = 2;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Select Unit     ");
  lcd.setCursor(0,1);
  lcd.print("Currently it's ");
  while (!buttonR.wasPressed()) {
    selected += readEncoder();
    if (selected<0) selected=2;
    if (selected>2) selected=0;
    if (moved) {
      lcd.setCursor(0,1);
      switch(selected) {
      case 0:
        lcd.print("Celcius         ");
        break;
      case 1:
        lcd.print("Fahrenheit      ");
        break;
      case 2:
        lcd.print("Cancel          ");
        break;
      }
    }
  }
  // when button is pressed...
  switch(selected) {
  case 0:
    lcd.print ("C");
    break;
  case 1:
    lcd.print ("F");
    break;
  }
}

And can you have a switch statement that relates to the first switch statement which I think is happening here. :roll_eyes:

One switch does not relate to the other.

The first switch operates if the button is not pressed, the second one if it is pressed.

It will repeat the "while" loop until the button is pressed. That includes reading the encoder to change the "selected" value.

When the button is pressed it uses the last known selected value to change the display to the right units.

thanks, that helped. karma awarded :smiley: I should have posted this code but your answer still fits. Interestingly, I cant see why I get to "Change Units " select it but dont get to settingsChangeUnits();

Which is the code above.

void displayMenu() {
  int selected = 3;

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Turn the knob to");
  lcd.setCursor(0,1);
  lcd.print("select an option");
  // display options
  while (!buttonR.wasPressed()) {
    selected += readEncoder();// selected = encoder
    if (selected<0) selected=3;
    if (selected>3) selected=0;
    if (moved) {
      lcd.clear();
      lcd.setCursor(0,0);
      switch(selected) {
      case 0:
        lcd.print("Change Units    ");
        break;
      case 1:
        lcd.print("Backlight On/Off");
        break;
      case 2:
        lcd.print("Logging On/Off  ");
        break;
      case 3:
        lcd.print("Exit Settings   ");
        break;
      }
    }
  }
  // when button is pressed...
  //lcd.clear();
  switch(selected) {
  case 0:
    settingsChangeUnits();
    break;
  case 1:
    //settingsChangeBacklight();
    break;
  case 2:
    break;
  }
}

hello,

whats about the if(moved) statement.... I do not see the variable defined.
Anyhow, if the variable 'moved' is false you can enter your while loop again without changing the menu item and if you leave then selected is different from what you see on the display.

whats about the if(moved) statement.... I do not see the variable defined.

boolean moved = false; appears earlier in the sketch. but not in the parts shown here.