Example Code for Arduino-Button Control
This article offers a detailed tutorial on controlling an Arduino using button inputs, involving hardware setup, software preparation, library installation, and sample coding for button interactions with an LCD Keypad Shield.
Hardware Preparation
- Arduino UNO x1
- LCD Keypad Shield V2.0 x1
- M-M/F-M/F-F Jumper wires
Software Preparation
- Arduino IDE Click to Download Arduino IDE
Wiring Diagram
Other Preparation Work
If your Arduino IDE does not have this library, please follow the steps below to install it.
-
Click the link to download library and reference files.
-
Unzip this folder and copy it to the libraries folder under Arduino installation directory.
About how to install the library?
- Next, open the Arduino IDE and copy the following code to the IDE window. (Delete all contents in the IDE just opened!)
- Select the correct serial port and board (Arduino UNO).
Sample Code
/*!
* @file Button.ino
* @brief Button.
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @maintainer DFRobot
* @version V1.0
* @date 2023-07-03
* @url https://github.com/DFRobot/DFRobot_RGBLCD1602
*/
#include "DFRobot_RGBLCD1602.h"
/*
Change the RGBaddr value based on the hardware version
-----------------------------------------
Moudule | Version| RGBAddr|
-----------------------------------------
LCD1602 Module | V1.0 | 0x60 |
-----------------------------------------
LCD1602 Module | V1.1 | 0x6B |
-----------------------------------------
LCD1602 RGB Module | V1.0 | 0x60 |
-----------------------------------------
LCD1602 RGB Module | V2.0 | 0x2D |
-----------------------------------------
*/
DFRobot_RGBLCD1602 lcd(/*RGBAddr*/0x60 ,/*lcdCols*/16,/*lcdRows*/2); //16 characters and 2 lines of show
// Define keys
int lcdKey = 0;
int adcKeyIN = 0;
int r=255; //Set red brightness (Range: 0-255)
int g=255; //Set green brightness
int b=0; //Set blue brightness
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.setRGB(r,g,b); // Set RGB
}
int readLcdButtons() // Determine which button is pressed
{
adcKeyIN = analogRead(0); // 0 read the ADC from analog values
// By setting different threshold, you can read the corresponding to the appropriate buttons
if (adcKeyIN > 1000) return btnNONE;
if (adcKeyIN < 50) return btnRIGHT;
if (adcKeyIN < 250) return btnUP;
if (adcKeyIN < 450) return btnDOWN;
if (adcKeyIN < 650) return btnLEFT;
if (adcKeyIN < 850) return btnSELECT;
return btnNONE;
}
void loop()
{
lcd.setCursor(9,1); //Set display output position
lcd.print(millis()/1000); //Output time(second)
lcd.setRGB(r,g,b); //Set RGB
lcd.setCursor(0,1);
lcdKey = readLcdButtons(); // Read button content
switch (lcdKey) // Select to output content according to the pressed button
{
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;
}
}
}
Result
Yellow background, output NONE when no button pressed, otherwise, output the corresponding button.
Was this article helpful?
