Pages: [1]   Go Down
Author Topic: SainSmart LCD Shield  (Read 748 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all I am new to the arduino world and I am having a terrible time with this code any input would be great! I am just trying to make a simple quiz and I have the LCD working the way I want it to the only problem I am having is on the sheild there are 6 buttons all mapped to analog pin 0. I am wanting the if statement to use the left button for yes and the right button for no all other buttons give a funny error message. What i am having issues with is when I press the left button the display shows Q1? and then immediately moves to YAY! when I have not pushed the left button a second time yet. any help would be much appreciated!

Code:
//Sample using LiquidCrystal library
#include <LiquidCrystal.h>
 

 
// select the pins used on the LCD panel
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);      // read the value from the sensor
 // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
 // we add approx 50 to those values and check to see if we are close
 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;
}
 
void setup()
{
 lcd.begin(16, 2);              // start the library
 lcd.setCursor(0,0);
 lcd.print("greetings"); // print a simple message
 delay (2000);
 lcd.clear();
 lcd.print ("Q0?");
}
 
void loop()
{

 lcd.setCursor(9,1);            // move cursor to second line "1" and 9 spaces over
 lcd.setCursor(0,1);            // move to the begining of the second line
 lcd_key = read_LCD_buttons();  // read the buttons
 
   if (lcd_key == btnLEFT)
     {
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print("Q1?");
         if (lcd_key==btnLEFT)
           {
            lcd.clear();
            lcd.setCursor(0,0);
           lcd.print("YAY!");
            }
     }
   }
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 74
Posts: 2219
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
when I press the left button the display shows Q1? and then immediately moves to YAY! when I have not pushed the left button a second time

You have not changed the value of lcd_key, so you cannot expect it to be any different.
Have a look how lcd_key is initialised, then what you compare it with in the if statements.

Sorry I can't elaborate, I have to head back to work.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 502
Posts: 19080
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
if (lcd_key == btnLEFT)
     {
...
     lcd.print("Q1?");
         if (lcd_key==btnLEFT)   // which it WILL be
           {
 ...
           lcd.print("YAY!");
            }
     }
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I guess what im asking is how do I reset the button so that I can have the left button used again for the nested if statement. I want the left button to equal yes and the right button to equal no.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 502
Posts: 19080
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You need a state machine. They won't hit the next button in the few microseconds those instructions take to execute.

http://en.wikipedia.org/wiki/State_machine
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im not sure why i did not think of using a finite state machine. I learned how to implement them last semester in my digital logic class. Thank you for pointing my mind in the right direction!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just in case someone else wants to make a quiz using analog pin for the buttons I found this for help!
http://playground.arduino.cc/code/FiniteStateMachine
Logged

Pages: [1]   Go Up
Jump to: