Ok, the clarify bits of my code
void Menu(){
//starts the menu subroutine from being called in setup LCDclear();
//clears the LCD LCDserial.print("Program Menu");
//prints to the lcd on first line LCDsecond();
//prints to lcd on second line etc... switch (programnumber) {
//reads the program number (set as 1 in setup) case 1:
//displays the first menu item LCDserial.print("Race Timer");
LCDthird();
LCDserial.print("Track: ");
LCDserial.print(distance);
LCDserial.print("m");
break;
case 2:
//displays the second menu item LCDserial.print("Reaction Tester");
break;
case 3:
//displays the third menu item LCDserial.print("Calibration Mode");
break;
default:
//if errors then it goes to default item is the race code. LCDserial.print("Race Timer");
LCDthird();
LCDserial.print("Track: ");
LCDserial.print(distance);
LCDserial.print("m");
}
LCDserial.print(0xFE, BYTE);
//Command code LCDserial.print(226, BYTE);
//Goes to specific point to display code LCDserial.print("Next >");
//next code in specific position while(digitalRead(starterbutton) == HIGH){}
//while button is high do nothing: {} (helps with debounce after button has been pressed) delay(5);
while(digitalRead(starterbutton) == LOW){}
//wait until button has been pressed (taken high). menudelaycount = 0; //clear menu delay
while(menudelaycount <500){
//do this code within the 500 limit (over 500ms because of other stuff that is done and takes time) delay(1);
menudelaycount++;
if(digitalRead(starterbutton) == LOW){
//if the button goes low (button released) within this loop then it advances one menu option programnumber++; //increase the menu option
if(programnumber > 3){
programnumber = 1;
}
Menu();
//then run menu code from start: the change in program number makes it change the option it is on. }
}
switch (programnumber) {
//if the button is not let go within the menu time stated above then it does not loop back and then selects the subroutine from this switch case: case 1:
Race();
break;
case 2:
Reaction();
break;
case 3:
Calibration();
break;
default:
Race();
}
}
The switch is held low by a pulldown resistor, it waits until it is low (in case someone is holding the button), then it then waits for a button press.
When it senses a button press, it starts a looping routine for 500 (not milliseconds due to other things that happen apart from the delay(1); ).
If the button is let go within that time (normal quick press), it advances the menu item on. If it is held the whole time then it takes the program number, gets to the end of the code and runs the selected program.
You probably want to do it with an up/down menu on your 16x2 rather than the 20x4 serial LCD I did it on but the code could be pretty similar.
If you wanted to do it up/down/select then that should not be too difficult either.
Assign each program part a seperate sub-routine and a number, then display the first 2 programs (numbers i and i+1), if you press the down button, advance the i value and run the display routine again. If you press the up button, decrease the i value (unless it is 1) and run again. Upon select button press, read the program number and run a switch case routine to select the corresponding sub-routine.
Hope that helps you,
Mowcius
