// Arduino LCD 16x2 Shield
// include the library
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// initialize the LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// setting the pin number
// for the LCD display backlight
unsigned long currentTime;
unsigned long previousTime = millis();
long timeInterval = 10000;
void setup() {
// start communication with the display,
// 16 characters, 2 rows
lcd.init();
// set the pin for controlling the backlight as output
lcd.backlight();
lcd.clear();
//lcd.setCursor(0,0);
//lcd.print("^ - AUTOMATIC");
//lcd.setCursor(0,1);
//lcd.print("v - BACK TO MANUAL");
}
void loop() {
currentTime = millis();
if(currentTime - previousTime > timeInterval) {
lcd.noBacklight();
}
// read data from analog pin A0 into the variable
int analogData = analogRead(0);
// turn on the display backlight
//digitalWrite(lcdSvit, HIGH);
// set cursor to the first character, first row
lcd.setCursor(0, 0);
// print text to the display
lcd.print("Pressed: ");
// set cursor to the thirteenth character, first row
lcd.setCursor(12, 0);
// print text to the display
lcd.print(analogData);
// set cursor to the third character, second row
lcd.setCursor(2, 1);
// print text to the display using the function
// with the currently measured value as an argument
lcd.print(readButtons(analogData));
// pause to read the data on the display
//delay(1000);
// turn off the display backlight
//digitalWrite(lcdSvit, LOW);
// pause before the end of the loop
//delay(100);
}
String readButtons(int analog) {
// variable to store the text for output
String text;
// sequentially check using if conditions,
// each button has a range of values
// in which it is detected and then sets the text for output
if (analog < 750) {
previousTime = currentTime;
lcd.backlight();
}
if (analog < 50) text = "Right(RIGHT)";
if ((analog > 700) && (analog < 1024)) text = " ";
if ((analog > 95) && (analog < 150)) text = "Up(UP)";
if ((analog > 250) && (analog < 350)) text = "Down(DOWN)";
if ((analog > 400) && (analog < 500)) text = "Left(LEFT)";
if ((analog > 600) && (analog < 750)) text = "Select(SELECT)";
// return the text as the function output
return text;
}