Thursday, August 25, 2016

Arduino LCD Button Shield

The Sparkfun LCD shield works very well.  It has five buttons wired to a single analogue input, which is a neat pin saver. 

However, the example code is much too complex to my liking.  Granted, it can handle multiple simultaneous button clicks, but who on earth will ever do that?  So I made something simpler:

#include <LiquidCrystal.h>

// LCD uses D4 to D9 and A0 for buttons

LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );

void setup()
{
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("C'mon click it!");
}

void loop()
{
  unsigned char key;
  unsigned int sensorValue;
 
  sensorValue = analogRead(A0);
 
  if(sensorValue < 1000)
  {
     sensorValue = analogRead(A0);
    
     lcd.setCursor(0, 1);
     lcd.print("                ");
    
     lcd.setCursor(0, 1);
     lcd.print(sensorValue);

     if ((sensorValue > 600) && (sensorValue < 620))
     {
       lcd.print(" Select");
     }

     if ((sensorValue > 840) && (sensorValue < 870))
     {
       lcd.print(" Left");
     }

     if ((sensorValue > 920) && (sensorValue < 940))
     {
       lcd.print(" Up");
     }

     if ((sensorValue > 890) && (sensorValue < 910))
     {
       lcd.print(" Down");
     }

     if ((sensorValue > 800) && (sensorValue < 830))
     {
       lcd.print(" Right");
     }

  }
}


Have fun!

Herman

No comments:

Post a Comment

On topic comments are welcome. Junk will be deleted.