Hello. Im new to this forum, but I've been using Arduino for some long time now.
For my project now i need a working menu system using "switch" statement. I hooked up 3 buttons using the tutorial (so resitors are included) and an LCD.
Im using "enum" as returns from button function. My problem lies somewhere within the code. When I start the Arduino the text on LCD flickers going trough "Effect1" to "Effect3" all the time. The only button that works is "button1" so the "DRIVE_BUTTON" event. When its pressed program goes to "DRIVE" state and stays there.
I've been going through this code for more then a week and I still cant find the answer or a mistake. I can only say that the problem lies within the code, because I checked button inputs with scope and they work fine.
Maybe a fresh look from someone that hasn't been looking at this code for some much time will help to find a mistake. Or maybe someone can upload this code to his/hers Arduino and see if it works. Because the problem may be my chip that has broken somehow.
Here is the code:
#include "LiquidCrystal.h"
LiquidCrystal lcd(12, 10, 5, 4, 3, 2); //init LCD
//button variables:
int buttonPin = 6;
int buttonPin2 = 9;
int buttonPin3 = 13;
int state1=0;
int state2=0;
int state3=0;
//menu variables:
enum events { NEXT, DRIVE_BUTTON, PREV, CLEAN_BUTTON, TICK, MAXEVENT };
enum states { CLEAN, DRIVE, EFFECT1, EFFECT2, EFFECT3};
int Program_state = CLEAN;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
lcd.begin(16, 2); // set up the LCD's number of columns and rows
}
void loop()
{
menu( read_button1() );
} // loop
//-----------------------------------------
int menu_display(int index)
{
switch(index){
case 0:
lcd.print("Clean");
break;
case 1:
lcd.print("Effect 1");
break;
case 2:
lcd.print("Effect 2");
break;
case 3:
lcd.print("Effect 3");
break;
case 4:
lcd.print("Overdrive");
break;
}
}
//-----------------------------------------
int read_button1(){ // button 1
if (digitalRead(buttonPin) == HIGH){
if (state1 == 0){
state1=1;
return DRIVE_BUTTON;
}
}
else
{
state1=0;
}
if (digitalRead(buttonPin2) == HIGH){ //button2
if (state2 == 0){
state2=1;
return NEXT;
}
}
else
{
state2=0;
}
if (digitalRead(buttonPin3) == HIGH){ //button3
if (state3 == 0){
state3=1;
return PREV;
}
}
else
{
state3=0;
}
}
//-----------------------------------------
int menu(int event){
int NextState = Program_state;
switch( Program_state )
{
case CLEAN:
switch (event) //change effect
{
case DRIVE_BUTTON:
Program_state = DRIVE;
lcd.clear();
menu_display(4);
break;
case NEXT:
Program_state = EFFECT1;
lcd.clear();
menu_display(1);
break;
case PREV:
Program_state = EFFECT3;
lcd.clear();
menu_display(3);
break;
}
break;
case DRIVE:
switch (event) //change effect
{
case DRIVE_BUTTON:
Program_state = CLEAN;
lcd.clear();
menu_display(0);
break;
}
break;
case EFFECT1: ////---------------------------------------------------------------------------------------EFFECT1
switch (event) //change effect
{
case DRIVE_BUTTON:
Program_state = DRIVE;
lcd.clear();
menu_display(4);
break;
case NEXT:
Program_state = EFFECT2;
lcd.clear();
menu_display(2);
break;
case PREV:
Program_state = EFFECT3;
lcd.clear();
menu_display(3);
break;
}
break;
case EFFECT2: ////---------------------------------------------------------------------------------------EFFECT2
switch (event) //change effect
{
case DRIVE_BUTTON:
Program_state = DRIVE;
lcd.clear();
menu_display(4);
break;
case NEXT:
Program_state = EFFECT3;
lcd.clear();
menu_display(3);
break;
case PREV:
Program_state = EFFECT1;
lcd.clear();
menu_display(1);
break;
}
break;
case EFFECT3: ////---------------------------------------------------------------------------------------EFFECT3
switch (event) //change effect
{
case DRIVE_BUTTON:
Program_state = DRIVE;
lcd.clear();
menu_display(4);
break;
case NEXT:
Program_state = EFFECT1;
lcd.clear();
menu_display(1);
break;
case PREV:
Program_state = EFFECT2;
lcd.clear();
menu_display(2);
break;
}
break;
}
}
Im using Arduino 1.0 software.