/*
16x2 LCD Keypad Shield - Button Modes (Basic)
by Saeed Hosseini @ Electropeak
https://electropeak.com/learn/
modified on 25 Aug 2021 by Oliver Kuy
*/
#include <LiquidCrystal.h>
//LCD pin to Arduino
const int pin_RS = 8;
const int pin_EN = 9;
const int pin_d4 = 4;
const int pin_d5 = 5;
const int pin_d6 = 6;
const int pin_d7 = 7;
const int pin_BL = 10;
LiquidCrystal lcd( pin_RS, pin_EN, pin_d4, pin_d5, pin_d6, pin_d7);
int R = 0;
int U = 0;
int D = 0;
int L = 0;
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Select a button:");
}
void loop() {
int x;
x = analogRead (0);
if (x < 60) {
R = 1; //Right;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print ("Right Pressed");
lcd.setCursor(0, 1);
lcd.print ("Press Select");
U = 0;
D = 0;
L = 0;
}
else if (x < 200) {
U = 1; //Up;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print ("Up Pressed");
lcd.setCursor(0, 1);
lcd.print ("Press Select");
R = 0;
D = 0;
L = 0;
}
else if (x < 400) {
D = 1; //Down;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print ("Down Pressed");
lcd.setCursor(0, 1);
lcd.print ("Press Select");
R = 0;
U = 0;
L = 0;
}
else if (x < 600) {
L = 1; //Left;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print ("Left Pressed");
lcd.setCursor(0, 1);
lcd.print ("Press Select");
R = 0;
U = 0;
D = 0;
}
else if (x < 800) {
if (R == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print ("Right Button");
lcd.setCursor(0, 1);
lcd.print ("Selected");
R = 0;
// Your code here
}
if (U == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print ("Up Button");
lcd.setCursor(0, 1);
lcd.print ("Selected");
U = 0;
// Your code here
}
if (D == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print ("Down Button");
lcd.setCursor(0, 1);
lcd.print ("Selected");
D = 0;
// Your code here
}
if (L == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print ("Left Button");
lcd.setCursor(0, 1);
lcd.print ("Selected");
L = 0;
// Your code here
}
}
}
Comments