I'm new to Arduino and am struggling with switching menus. I want my code to display a value and show this until a button is pressed, there are three menus to cycle through in till it has to return to the first one, I'm not sure how to really approach this, any help will be greatly appreciated
When I go to a restaurant, I get handed a menu - a list of choices. I do not see that your code has anything remotely resembling a menu.
What I see is that, apparently, you have 3 values to display. I presume that what you want to do is display one value until something (a switch press) tells you to display something else.
The state change detection example shows how to determine that a switch has become pressed, and how to count the number of times that that has happened.
You can then use a switch statement, with cases 0, 1, and 2, to display the three different values.
So I've started to add the switch statements as previously mentioned using tutorials, but I have seemed to come to another stopping point as I haven't found a tutorial which uses a single button to switch between the options. I don't know if the code I have written is right but any tutorial videos you can suggest, feel free to edit the code if I'm just missing something really simple. Thanks
switch (button);You never change the value of button, nor should you, so this will not work.
Do as suggested and look at the StateChangeDetection example in the IDE to see how determine how many times a button has been pressed in order to get the value to be used in the switch() command.
// Include the LCD Library
#include <LiquidCrystal.h>
//initialize variables for sensor pins
int moistPin = 0;
int tempPin = 1;
int lightPin = 2;
//change mode input
int button = 6;
//initialize variables to store readings from sensors
int moistVal = 0;
int tempVal = 0;
int lightVal = 0;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup()
{
//initialize the serial port
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
pinMode(button, INPUT);
}
void loop ()
switch(button);
case 0;
//display moisture reading on lcd
moistVal = analogRead(moistPin);
lcd.clear();
lcd.print("moisture ");
lcd.print(moistVal);
delay(4000);
break;
case 1;
//display temperature reading on lcd
tempVal = analogRead(tempPin);
lcd.clear();
lcd.print("temperature ");
lcd.print(tempVal);
delay(4000);
break;
case 2;
//display light reading on lcd
lightVal = analogRead(lightPin);
lcd.clear();
lcd.print("light ");
lcd.print(lightVal);
delay(4000);
break;
Default;
//display moisture reading on lcd
moistVal = analogRead(moistPin);
lcd.clear();
lcd.print("moisture ");
lcd.print(moistVal);
delay(4000);
break;
}
Switch statements STILL do not end with ;. They have a body, delimited by curly braces that contain all the case statements.
case 0;
Case statements end with a colon, not a semicolon.
Why do you have pin in the name of all the variables that contain pin numbers except the one that is causing you problems? Why are you trying to switch on the pin number?
Where are you reading the state of the switch pin? Where are you comparing that state to the previous state of the switch? Where are you counting the number of times the switch has become pressed?