Hi,
I'm trying to create a kind of quiz. Depending on the right answer, my arduino will give a clue for the next question.
I'm using the arduino lcd keypad shield and Arduino Uno.
With the up and right button I want a counter to increase (visibleAnswer++).
Down/left should decrease the counter.
But when I press the button, it increases with a random number.
I've tried to implement the debounce example but then only my Up button works.
Can anyone please help?
Here's my code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0);
if (adc_key_in > 1000) return btnNONE;
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE;
}
char* answer[] = { "a", "b", "c"};
int visibleAnswer = 0;
void setup()
{
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("Push the buttons");
}
void loop()
{
lcd.setCursor(9,1);
lcd.print(visibleAnswer);
lcd.setCursor(0,1);
lcd_key = read_LCD_buttons();
switch (lcd_key)
{
case btnRIGHT:
{
lcd.print("RIGHT ");
visibleAnswer++;
break;
}
case btnLEFT:
{
lcd.print("LEFT ");
visibleAnswer--;
break;
}
case btnUP:
{
lcd.print("UP ");
visibleAnswer++;
break;
}
case btnDOWN:
{
lcd.print("DOWN ");
visibleAnswer--;
break;
}
case btnSELECT:
{
lcd.print("SELECT");
CheckAnswer(visibleAnswer);
break;
}
case btnNONE:
{
lcd.print("NONE ");
break;
}
}
}
void CheckAnswer(int answer){
//Do something
}