Submenu

const int  switchOnePin = 29;
const int  switchTwoPin = 27;
const int  switchThreePin = 25;
const int  switchFourPin = 23;

int switchOneState = 0;
int switchTwoState = 0;
int switchThreeState = 0;
int switchFourState = 0;

void setup() {
  pinMode(switchOnePin, INPUT);
  pinMode(switchTwoPin, INPUT);
  pinMode(switchThreePin, INPUT);
  pinMode(switchFourPin, INPUT);
}

void loop() {
  switchOneState = digitalRead(switchOnePin);
  switchTwoState   = digitalRead(switchTwoPin);
  switchThreeState = digitalRead(switchThreePin);
  switchFourState = digitalRead(switchFourPin);

  int x= 0;
  
  if (switchOneState == HIGH) {
  x++;
  tft.println("Press again");
  tft.println(x);
            
  if (switchOneState == HIGH && x >= 2){
    tft.println("test");
    }
  }
  delay(50);
}

I want to create a submenu. If I press my button once, it's supposed to say "Press again". If I press that same button again I want it to output something else, in this case "test".

I'm trying to achieve this by creating the variable x, that starts out with a value of 0 and gets increased by 1 whenever I press my button.

When x = 1 (first button press) I want it to output the "Press again" screen When x = 2 (second button press) I want it to output the "test" screen

My problem here is that x will only increase a single time, no matter how often I press my button. To my understanding everything in the void loop is supposed to loop.

Magnat: To my understanding everything in the void loop is supposed to loop.

But where is your code placed? Aka, you forgot to post all, we're not Snippets R Us

My problem here is that x will only increase a single time, no matter how often I press my button.

 int x = 0;

resets x back to 0 each time through loop()

Try

 static int x = 0;

to prevent it being reset to zero or make x a global variable

Put int x before loop(). Your code probably doesn't work because x gets reset every time loop starts over again.

septillion: But where is your code placed? Aka, you forgot to post all, we're not Snippets R Us

Sorry, updated my code.

const int  switchOnePin = 29;
const int  switchTwoPin = 27;
const int  switchThreePin = 25;
const int  switchFourPin = 23;

int switchOneState = 0;
int switchTwoState = 0;
int switchThreeState = 0;
int switchFourState = 0;

Points for using const. But why int for everything? And this is SCREAMING for an array or more useful names if the keys are not related.

But people gave you the answer, x only exists from the moment you define it till the end of loop() after it's redefined again (and re-initiated to 0) the next run through loop().

PS And you see, this turned out to be a perfect Snippets R Us example. The error is in the part you did not post ;)

UKHeliBob: int x = 0;

resets x back to 0 each time through loop()

Try

 static int x = 0;

to prevent it being reset to zero or make x a global variable

Silly me! I've tried to figure this out for like 3 hours and just didn't think of using constant variables. Thanks a lot!

septillion: ``` const int  switchOnePin = 29; const int  switchTwoPin = 27; const int  switchThreePin = 25; const int  switchFourPin = 23;

int switchOneState = 0; int switchTwoState = 0; int switchThreeState = 0; int switchFourState = 0;



Points for using const. But why int for everything? And this is SCREAMING for an array or more useful names if the keys are not related.

But people gave you the answer, x only exists from the moment you define it till the end of loop() after it's redefined again (and re-initiated to 0) the next run through loop().

PS And you see, this turned out to be a perfect Snippets R Us example. The error is in the part you did not post ;)

I've got to admit that I've barely ever worked with Arrays before, that's why I'm clinging onto Integers. Should've definitely posted the whole code straight away. Thanks for the heads up.

const int  switchOnePin = 29;
const int  switchTwoPin = 27;
const int  switchThreePin = 25;
const int  switchFourPin = 23;

int switchOneState = 0;
int switchTwoState = 0;
int switchThreeState = 0;
int switchFourState = 0;


void setup() {
  pinMode(switchOnePin, INPUT);
  pinMode(switchTwoPin, INPUT);
  pinMode(switchThreePin, INPUT);
  pinMode(switchFourPin, INPUT);

  Serial.println("1. Weather");
  Serial.println("2. Date");
}

void loop() {
  switchOneState = digitalRead(switchOnePin);
  switchTwoState   = digitalRead(switchTwoPin);
  switchThreeState = digitalRead(switchThreePin);
  switchFourState = digitalRead(switchFourPin);

  const int x = 0;
  const int y = 0

  //Subemenu for button 1
  if (switchOneState == HIGH){
    x++;
    Serial.println("1. Temperature");
    Serial.println("2. Humidity");
    }
    
  if (switchOneState == HIGH && x == 2){
    Serial.println("Temperature Output");
    } else if (switchTwoState == HIGH && x == 1){
      Serial.println("Humidity Output");
      }

  //Sunmenu for button 2
  if (switchTwoState == HIGH){
    y++;
    Serial.println("1. Time");
    Serial.println("2. Date");
    }
    
  if (switchTwoState == HIGH && y == 2){
    x++;
    Serial.println("Date Output");
    } else if (switchOneState == HIGH && y == 1){
      Serial.println("Time Output");
    }
  delay(50);
}

I've run into a new problem. Is there a way to make the two submenus completely independent from each other? I've created the y variable for the second submenu. The issue now is, since it runs through the code of submenu 1 as well, that the x variable also gets increased, even though I'm not even in submenu 1. This leads to the 2 menus mixing up. Do I have to create a completely new method to get 2 independent menus?

Magnat: Silly me! I've tried to figure this out for like 3 hours and just didn't think of using constant variables. Thanks a lot!

Constant variables would not fix a thing. Static is the trick ;) static != const :)

Magnat: I've got to admit that I've barely ever worked with Arrays before, that's why I'm clinging onto Integers.

An array is NOT a type ;) And array is just a list of some type like an int. The declaration gives it away:

int myInt = 5;
int myIntArray[3] = {1, 2, 3}; //also of type int :)

But becasue you define pins you should use a byte (or do you expect to use pin -1234 as well?) and for a state a bool (unless you define more than two states, then a byte or char would be more logical). A micro has limited memory aka it's a pity to use it all because by using 2 bytes for something that fit's a single bit :)

Just defining x and y variables is vague. Just give them a name, what do they really mean?