Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Oliver Kuy
Published © GPL3+

Four Mode Selection (16x2 LCD Keypad Shield)

A very simple Sketch to use the buttons of a 16x2 LCD Keypad Shield. You will be able to do 4 different instructions.

BeginnerProtip2,362
Four Mode Selection (16x2 LCD Keypad Shield)

Things used in this project

Hardware components

RGB LCD Shield Kit, 16x2 Character Display
RGB LCD Shield Kit, 16x2 Character Display
×1
Arduino UNO
Arduino UNO
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

16x2 LCD Keypad Shield

Code

16x2 LCD Keypad Shield - Button Modes (Basic)

C/C++
/*
  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
    }
  }
}

Credits

Oliver Kuy
0 projects • 0 followers

Comments