Skip to content

Instantly share code, notes, and snippets.

@robinmanuelthiel
Last active August 17, 2016 22:19
Show Gist options
  • Save robinmanuelthiel/e64748f1c133e00c4641746da410bb38 to your computer and use it in GitHub Desktop.
Save robinmanuelthiel/e64748f1c133e00c4641746da410bb38 to your computer and use it in GitHub Desktop.
A simple Arduino Sketch to control the LCD Keypad Shield (SKU: DFR0009)
// Use LiquidCrystal library
#include <LiquidCrystal.h>
// Define button values
#define btnNONE 0
#define btnRIGHT 1
#define btnUP 2
#define btnDOWN 3
#define btnLEFT 4
#define btnSELECT 5
// Select the pins used on the LCD panel
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Define variable for selected key
int lcdKey = 0;
// Function to read in keys
int readLcdButtons() {
// Read the analog key value
int keyIn = analogRead(0);
// Match the value with the according buttons
if (keyIn > 1000) return btnNONE;
if (keyIn < 50) return btnRIGHT;
if (keyIn < 250) return btnUP;
if (keyIn < 450) return btnDOWN;
if (keyIn < 650) return btnLEFT;
if (keyIn < 850) return btnSELECT;
// Return 'none' if no match was found
return btnNONE;
}
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
void loop() {
// Reset the cursor
lcd.setCursor(0,0);
// Read the button value
lcdKey = readLcdButtons();
// Depending on which button was pushed, perform an action
switch (lcdKey)
{
case btnRIGHT:
{
lcd.print("RIGHT ");
break;
}
case btnLEFT:
{
lcd.print("LEFT ");
break;
}
case btnUP:
{
lcd.print("UP ");
break;
}
case btnDOWN:
{
lcd.print("DOWN ");
break;
}
case btnSELECT:
{
lcd.print("SELECT");
break;
}
case btnNONE:
{
lcd.print("NONE ");
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment