0% found this document useful (0 votes)
108 views30 pages

LCD KeyPad Shield

The document describes an Arduino project that uses a LCD keypad shield to create a menu interface for navigating and selecting different options. It includes code to initialize the LCD display, define menu items and submenus, read button input and handle menu navigation and selection using a menu backend library.

Uploaded by

Baiquny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views30 pages

LCD KeyPad Shield

The document describes an Arduino project that uses a LCD keypad shield to create a menu interface for navigating and selecting different options. It includes code to initialize the LCD display, define menu items and submenus, read button input and handle menu navigation and selection using a menu backend library.

Uploaded by

Baiquny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

http://www.instructables.

com/id/Arduino-Timer-With-OnOff-Set-Point/

LCD KeyPad Shield


//Sample using LiquidCrystal library
#include <LiquidCrystal.h>
/*******************************************************
* This program will test the LCD panel and the buttons
* Mark Bramwell, July 2010
********************************************************/

// select the pins used on the LCD panel


LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// define some values used by the panel and buttons


int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for
speed reasons since it will be the most likely result
// For V1.1 us this threshold
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
// For V1.0 comment the other threshold and use the one below:
/*
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
*/
return btnNONE; // when all others fail, return this...
}

void setup()
{
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0);
lcd.print("Push the buttons"); // print a simple message
}

void loop()
{
lcd.setCursor(9,1); // move cursor to second line "1" and 9 spaces
over
lcd.print(millis()/1000); // display seconds elapsed since power-up
lcd.setCursor(0,1); // move to the begining of the second line
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform
an action
{
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;
}
}
}
/*
Copyright Giuseppe Di Cillo (www.coagula.org)
Contact: [email protected]

This program is free software: you can redistribute it and/or modify


it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,


but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/*
IMPORTANT: to use the menubackend library by Alexander Brevig download it at
http://www.arduino.cc/playground/uploads/Profiles/MenuBackend_1-4.zip and add the next code
at line 195
void toRoot() {
setCurrent( &getRoot() );
}
*/
#include <MenuBackend.h> //MenuBackend library - copyright by Alexander Brevig
#include <LiquidCrystal.h> //this library is included in the Arduino IDE

const int buttonPinLeft = 10; // pin for the Up button


const int buttonPinRight = 11; // pin for the Down button
const int buttonPinEsc = 12; // pin for the Esc button
const int buttonPinEnter = 13; // pin for the Enter button

int lastButtonPushed = 0;

int lastButtonEnterState = LOW; // the previous reading from the Enter input pin
int lastButtonEscState = LOW; // the previous reading from the Esc input pin
int lastButtonLeftState = LOW; // the previous reading from the Left input pin
int lastButtonRightState = LOW; // the previous reading from the Right input pin

long lastEnterDebounceTime = 0; // the last time the output pin was toggled
long lastEscDebounceTime = 0; // the last time the output pin was toggled
long lastLeftDebounceTime = 0; // the last time the output pin was toggled
long lastRightDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 500; // the debounce time
// LiquidCrystal display with:
// rs on pin 7
// rw on ground
// enable on pin 6
// d4, d5, d6, d7 on pins 5, 4, 3, 2
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

//Menu variables
MenuBackend menu = MenuBackend(menuUsed,menuChanged);
//initialize menuitems
MenuItem menu1Item1 = MenuItem("1. Sensors");
MenuItem menuItem1SubItem1 = MenuItem("1.1 Temperature");
MenuItem menuItem1SubItem2 = MenuItem("1.2 AmbientLight");
MenuItem menu1Item2 = MenuItem("2. LEDs");
MenuItem menuItem2SubItem1 = MenuItem("2.1 Test");
MenuItem menuItem2SubItem2 = MenuItem("2.2 Test");
MenuItem menuItem2SubItem3 = MenuItem("2.3 Test");
MenuItem menu1Item3 = MenuItem("3. Test - Empty");

void setup()
{
pinMode(buttonPinLeft, INPUT);
pinMode(buttonPinRight, INPUT);
pinMode(buttonPinEnter, INPUT);
pinMode(buttonPinEsc, INPUT);

lcd.begin(16, 2);

//configure menu
menu.getRoot().add(menu1Item1);
menu1Item1.addRight(menu1Item2).addRight(menu1Item3);
menu1Item1.add(menuItem1SubItem1).addRight(menuItem1SubItem2);
menu1Item2.add(menuItem2SubItem1).addRight(menuItem2SubItem2).addRight(menuItem2Su
bItem3);
menu.toRoot();
lcd.setCursor(0,0);
lcd.print("MenuControl v1.0");

} // setup()...

void loop()
{

readButtons(); //I splitted button reading and navigation in two procedures because
navigateMenus(); //in some situations I want to use the button for other purpose (eg. to change
some settings)
} //loop()...

void menuChanged(MenuChangeEvent changed){

MenuItem newMenuItem=changed.to; //get the destination menu

lcd.setCursor(0,1); //set the start position for lcd printing to the second row

if(newMenuItem.getName()==menu.getRoot()){
lcd.print("Main Menu ");
}else if(newMenuItem.getName()=="1. Sensors"){
lcd.print("1. Sensors ");
}else if(newMenuItem.getName()=="1.1 Temperature"){
lcd.print("1.1 Temperature ");
}else if(newMenuItem.getName()=="1.2 AmbientLight"){
lcd.print("1.2 AmbientLight");
}else if(newMenuItem.getName()=="2. LEDs"){
lcd.print("2. LEDs ");
}else if(newMenuItem.getName()=="2.1 Test"){
lcd.print("2.1 Test ");
}else if(newMenuItem.getName()=="2.2 Test"){
lcd.print("2.2 Test ");
}else if(newMenuItem.getName()=="2.3 Test"){
lcd.print("2.3 Test ");
}else if(newMenuItem.getName()=="3. Test - Empty"){
lcd.print("3. Test - Empty ");
}
}

void menuUsed(MenuUseEvent used){


lcd.setCursor(0,0);
lcd.print("You used ");

if ((used.item.getName()) == "1.1 Temperature")


{

menu.toRoot(); //back to Main


}

if ((used.item.getName()) == "1.2 AmbientLight")


{
lcd.setCursor(0,1);
lcd.print("Juppiii Ambient");
// lcd.print(used.item.getName());
delay(3000); //delay to allow message reading
lcd.setCursor(0,0);
lcd.print("MenuControl v1.0");
menu.toRoot(); //back to Main
}

void readButtons(){ //read buttons status


int reading;
int buttonEnterState=LOW; // the current reading from the Enter input pin
int buttonEscState=LOW; // the current reading from the input pin
int buttonLeftState=LOW; // the current reading from the input pin
int buttonRightState=LOW; // the current reading from the input pin

//Enter button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinEnter);

// check to see if you just pressed the enter button


// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:


if (reading != lastButtonEnterState) {
// reset the debouncing timer
lastEnterDebounceTime = millis();
}

if ((millis() - lastEnterDebounceTime) > debounceDelay) {


// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonEnterState=reading;
lastEnterDebounceTime=millis();
}

// save the reading. Next time through the loop,


// it'll be the lastButtonState:
lastButtonEnterState = reading;
//Esc button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinEsc);

// check to see if you just pressed the Down button


// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:


if (reading != lastButtonEscState) {
// reset the debouncing timer
lastEscDebounceTime = millis();
}

if ((millis() - lastEscDebounceTime) > debounceDelay) {


// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonEscState = reading;
lastEscDebounceTime=millis();
}

// save the reading. Next time through the loop,


// it'll be the lastButtonState:
lastButtonEscState = reading;

//Down button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinRight);

// check to see if you just pressed the Down button


// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:


if (reading != lastButtonRightState) {
// reset the debouncing timer
lastRightDebounceTime = millis();
}

if ((millis() - lastRightDebounceTime) > debounceDelay) {


// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonRightState = reading;
lastRightDebounceTime =millis();
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonRightState = reading;

//Up button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinLeft);

// check to see if you just pressed the Down button


// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:


if (reading != lastButtonLeftState) {
// reset the debouncing timer
lastLeftDebounceTime = millis();
}

if ((millis() - lastLeftDebounceTime) > debounceDelay) {


// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonLeftState = reading;
lastLeftDebounceTime=millis();;
}

// save the reading. Next time through the loop,


// it'll be the lastButtonState:
lastButtonLeftState = reading;

//records which button has been pressed


if (buttonEnterState==HIGH){
lastButtonPushed=buttonPinEnter;

}else if(buttonEscState==HIGH){
lastButtonPushed=buttonPinEsc;

}else if(buttonRightState==HIGH){
lastButtonPushed=buttonPinRight;

}else if(buttonLeftState==HIGH){
lastButtonPushed=buttonPinLeft;

}else{
lastButtonPushed=0;
}
}

void navigateMenus() {
MenuItem currentMenu=menu.getCurrent();

switch (lastButtonPushed){
case buttonPinEnter:
if(!(currentMenu.moveDown())){ //if the current menu has a child and has been pressed enter
then menu navigate to item below
menu.use();
}else{ //otherwise, if menu has no child and has been pressed enter the current menu is used
menu.moveDown();
}
break;
case buttonPinEsc:
menu.toRoot(); //back to main
break;
case buttonPinRight:
menu.moveRight();
break;
case buttonPinLeft:
menu.moveLeft();
break;
}

lastButtonPushed=0; //reset the lastButtonPushed variable


}

Program 3 : http://blog.rkflyer.co.uk/2014/02/0-cursorposition-move-cursor-up-one.html

//Define Various Variables used throughout

int timer = 0;

byte totalRows = 2; // total rows of LCD

byte totalCols = 16; // total columns of LCD

int returndata = 0; // Used for return of button presses

unsigned long timeoutTime = 0; // this is set and compared to millis to see when the user last did
something.

const int menuTimeout = 20000; // time to timeout in a menu when user doesn't do anything.

unsigned long lastButtonPressed; // this is when the last button was pressed. It's used to debounce.
const int debounceTime = 150; // this is the debounce and hold delay. Otherwise, you will FLY
through the menu by touching the button.

const int buttonUp = 8; // Set pin for UP Button

const int buttonDown = 9; // Set pin for DOWN Button

const int buttonSelect = 11; // Set pin for SLELECT Button

const int buttonCancel = 12; // Set pun for CANCEL Button (No currently used)

int buttonStateUp = 0; // Initalise ButtonStates

int buttonStateDown = 0;

int buttonState;

int count = 0; // Temp variable for void demo

// constants for indicating whether cursor should be redrawn

#define MOVECURSOR 1

// constants for indicating whether cursor should be redrawn

#define MOVELIST 2

// Main setup routine

void setup()

// Setup stuff that needs to be done regardless of whether entering setup mode or not

// set up the LCD's number of columns and rows:

lcd.begin(totalCols, totalRows);

// Turn on the LCD Backlight

lcd.setLED1Pin(1);

// initialize the serial communications port:

Serial.begin(9600);

// Set Buttons as input for testing whether to enter setup mode

pinMode(buttonUp, INPUT);
pinMode(buttonDown, INPUT);

// Read the Button States

buttonStateUp = digitalRead(buttonUp);

buttonStateDown = digitalRead(buttonDown);

// Start Setup Mode if both up and down buttons pressed together

if (buttonStateUp == HIGH && buttonStateDown == HIGH) {

byte topItemDisplayed = 0; // stores menu item displayed at top of LCD screen

byte cursorPosition = 0; // where cursor is on screen, from 0 --> totalRows.

// redraw = 0 - don't redraw

// redraw = 1 - redraw cursor

// redraw = 2 - redraw list

byte redraw = MOVELIST; // triggers whether menu is redrawn after cursor move.

byte i=0; // temp variable for loops.

byte totalMenuItems = 0; //a while loop below will set this to the # of menu items.

// Create list of Menu Items

char* menuItems[]={

"Menu Item 1",

"Menu Item 2",

"Menu Item 3",

"Menu Item 4 ",

"Menu Item 5",

"Menu Item 6",

"",
};

// count how many items are in list.

while (menuItems[totalMenuItems] != ""){

totalMenuItems++;

//subtract 1 so we know total items in array.

totalMenuItems--;

lcd.clear(); // clear the screen so we can paint the menu.

boolean stillSelecting = true; // set because user is still selecting.

timeoutTime = millis() + menuTimeout; // set initial timeout limit.

do // Run a loop while waiting for user to select menu option.

// Call any other setup actions required and/or process anything else required whilst in setup mode as
opposed to things setup regardless of setup mode

// Call read buttons routine which analyzes buttons and gets a response. Default response is 0.

switch(read_buttons())

// Case responses depending on what is returned from read buttons routine

case 1: // 'UP' BUTTON PUSHED

timeoutTime = millis()+menuTimeout; // reset timeout timer

// if cursor is at top and menu is NOT at top

// move menu up one.

if(cursorPosition == 0 && topItemDisplayed > 0) // Cursor is at top of LCD, and there are higher
menu items still to be displayed.

{
topItemDisplayed--; // move top menu item displayed up one.

redraw = MOVELIST; // redraw the entire menu

// if cursor not at top, move it up one.

if(cursorPosition>0)

cursorPosition--; // move cursor up one.

redraw = MOVECURSOR; // redraw just cursor.

break;

case 2: // 'DOWN' BUTTON PUSHED

timeoutTime = millis()+menuTimeout; // reset timeout timer

// this sees if there are menu items below the bottom of the LCD screen & sees if cursor is at bottom
of LCD

if((topItemDisplayed + (totalRows-1)) < totalMenuItems && cursorPosition == (totalRows-1))

topItemDisplayed++; // move menu down one

redraw = MOVELIST; // redraw entire menu

if(cursorPosition<(totalRows-1)) // cursor is not at bottom of LCD, so move it down one.

cursorPosition++; // move cursor down one

redraw = MOVECURSOR; // redraw just cursor.

break;
case 4: // SELECT BUTTON PUSHED

timeoutTime = millis()+menuTimeout; // reset timeout timer

switch(topItemDisplayed + cursorPosition) // adding these values together = where on menuItems


cursor is.

case 0: // menu item 1 selected

lcd.clear();

lcd.print("Run Item1 code");

lcd.setCursor(0,1);

lcd.print("from here");

Serial.print("Menu item ");

Serial.print(topItemDisplayed + cursorPosition);

Serial.print(" selected - ");

Serial.println(menuItems[topItemDisplayed + cursorPosition]);

delay(2000);

stillSelecting = false;

break;

case 1: // menu item 2 selected

lcd.clear();

lcd.print("Run Item2 code");

lcd.setCursor(0,1);

lcd.print("from here");

Serial.print("Menu item ");


Serial.print(topItemDisplayed + cursorPosition);

Serial.print(" selected - ");

Serial.println(menuItems[topItemDisplayed + cursorPosition]);

break;

case 2: // menu item 3 selected

lcd.clear();

lcd.print("Run Item3 code");

lcd.setCursor(0,1);

lcd.print("from here");Serial.print("Menu item ");

Serial.print(topItemDisplayed + cursorPosition);

Serial.print(" selected - ");

Serial.println(menuItems[topItemDisplayed + cursorPosition]);

break;

case 3: // menu item 4 selected

lcd.clear();

lcd.print("Run Item4 code");

lcd.setCursor(0,1);

lcd.print("from here");Serial.print("Menu item "); Serial.print("Menu item ");

Serial.print(topItemDisplayed + cursorPosition);

Serial.print(" selected - ");

Serial.println(menuItems[topItemDisplayed + cursorPosition]);

break;
case 4: // menu item 5 selected

lcd.clear();

lcd.print("Run Item5 code");

lcd.setCursor(0,1);

lcd.print("from here");Serial.print("Menu item ");Serial.print("Menu item ");

Serial.print(topItemDisplayed + cursorPosition);

Serial.print(" selected - ");

Serial.println(menuItems[topItemDisplayed + cursorPosition]);

break;

case 5: // menu item 6 selected

lcd.clear();

lcd.print("Run Item6 code");

lcd.setCursor(0,1);

lcd.print("from here");Serial.print("Menu item "); Serial.print("Menu item ");

Serial.print(topItemDisplayed + cursorPosition);

Serial.print(" selected - ");

Serial.println(menuItems[topItemDisplayed + cursorPosition]);

break;

break;

//case 8: // CANCEL BUTTON PUSHED - Not currently used

// stillSelecting = false;

// Serial.println("Button held for a long time");


// break;

Program 4 : http://moodle.oside.us/mod/page/view.php?id=105

*
VerticalMenu - Simple vertical menu for the Arduino LCD Shield

Created 1 May 2013

Copyright (C) 2012 Will Kostelecky <[email protected]>

This program is free software; you can redistribute it and/or


modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,


but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/

// Some constants
const int TRUE = 1, FALSE = 0;
const int ON = 1, OFF = 0;
// Include and initialize the library for the LCD
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
String lcdMessage = " ";

// Some stuff used by the lcd


#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5

int relayPin1 = 13; // IN1 connected to digital pin 7


int relayPin2 = 12; // IN2 connected to digital pin 8
int relayPin3 = 11; // IN3 connected to digital pin 9
int relayPin4 = 3; // IN4 connected to digital pin 10

boolean relay1 = OFF; // used to keep the state of relay 1 - ON(1) or OFF(0)
boolean relay2= OFF; // used to keep the state of relay 2 - ON(1) or OFF(0)
boolean relay3= OFF; // used to keep the state of relay 3 - ON(1) or OFF(0)
boolean relay4= OFF; // used to keep the state of relay 4 - ON(1) or OFF(0)

// Enable storage of literals in program space rather than RAM


#include <avr/pgmspace.h>

/* ************************************************************************
Initialization
************************************************************************ */
void setup() {
// For debugging messages
Serial.begin(9600);
// Set the size of the lcd
lcd.begin(16, 2);
pinMode(relayPin1, OUTPUT); // sets the digital pin as output
pinMode(relayPin2, OUTPUT); // sets the digital pin as output
pinMode(relayPin3, OUTPUT); // sets the digital pin as output
pinMode(relayPin4, OUTPUT); // sets the digital pin as output
digitalWrite(relayPin1, HIGH); // Prevents relays from starting up engaged - HIGH turn relay off
digitalWrite(relayPin2, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin3, HIGH); // Prevents relays from starting up engaged
digitalWrite(relayPin4, HIGH); // Prevents relays from starting up engaged
}

/* ************************************************************************
Process Loop
************************************************************************ */
void loop()
{
// The menu entries list needs to end with a blank entry and each item
// can only be 15 characters long:
// [ ]
PROGMEM char* menu1[]={ "Relay 1",
"Relay 2",
"Relay 3",
"Relay 4",
""};

int selection = menuSelection(menu1); // menuSelection returns the value of the relay selected

switch (selection) // selection can be for relay 1 to 4


{
case 1:
relay1 = !relay1; // reverse the state of relay, if OFF turn ON, if ON turn OFF
if (relay1 == ON)
{
digitalWrite(relayPin1, LOW); // LOW Energizes the relay and LED is ON
debugDisplay("Relay " + String(selection) + " ON", "Press any key"); // prints a statement to LCD
saying relay is ON or OFF
}

if (relay1 == OFF)
{
digitalWrite(relayPin1, HIGH); // de-energizes the relay and LED is off
debugDisplay("Relay " + String(selection) + " OFF", "Press any key"); //prints a statement to LCD
saying relay is ON or OFF
}
break;

case 2:
relay2 = !relay2; // reverse the state of relay, if OFF turn ON, if ON turn OFF
if (relay2 == ON)
{
digitalWrite(relayPin2, LOW); // Energizes the relay and LED is ON
debugDisplay("Relay " + String(selection) + " ON", "Press any key"); //prints a statement to LCD
saying relay is ON or OFF
}

if (relay2 == OFF)
{
digitalWrite(relayPin2, HIGH); // de-energizes the relay and LED is off
debugDisplay("Relay " + String(selection) + " OFF", "Press any key"); //prints a statement to LCD
saying relay is ON or OFF
}
break;
case 3:
relay3 = !relay3; // reverse the state of relay, if OFF turn ON, if ON turn OFF
if (relay3 == ON)
{
digitalWrite(relayPin3, LOW); // Energizes the relay and LED is ON
debugDisplay("Relay " + String(selection) + " ON", "Press any key"); //prints a statement to LCD
saying relay is ON or OFF
}

if (relay3 == OFF)
{
digitalWrite(relayPin3, HIGH); // de-energizes the relay and LED is off
debugDisplay("Relay " + String(selection) + " OFF", "Press any key"); //prints a statement to LCD
saying relay is ON or OFF
}
break;

case 4:
relay4 = !relay4; // reverse the state of relay, if OFF turn ON, if ON turn OFF
if (relay4 == ON)
{
digitalWrite(relayPin4, LOW); // Energizes the relay and LED is ON
debugDisplay("Relay " + String(selection) + " ON", "Press any key"); //prints a statement to LCD
saying relay is ON or OFF
}

if (relay4 == OFF)
{
digitalWrite(relayPin4, HIGH); // de-energizes the relay and LED is off
debugDisplay("Relay " + String(selection) + " OFF", "Press any key"); //prints a statement to LCD
saying relay is ON or OFF
}
break;
}

/* ************************************************************************
Simple vertical menu. Top line displayed with symbol indicating it will be
selected. Up and down buttons to navigate. Menu terminated with a blank
entry.
************************************************************************ */
int menuSelection(char *menu[]) {
String temp=" ";
int selection = 0;
while (1 == 1) {
// First char of top line to indicates it will be selected
temp = String(menu[selection]);
temp = String(char(126) + temp);
lcdText(0, temp);
// Empty line means we are at the end
if (menu[selection + 1][0] != 0) {
temp = String(menu[selection + 1]);
temp = String(" " + temp);
lcdText(1, temp);
}
else {
lcdText(1, "");
}

// Read the keypad buttons


int lcdKey = readButtons();
switch (lcdKey) {
case btnUP:
{
// Decr our selection if we are not at the top
if (selection > 0) {
selection--;
}
break;
}
case btnDOWN:
{
// Incr our selection if we are not at the bottom
if ((selection < 5) && (menu[selection + 1][0] != 0)) {
selection++;
}
break;
}
case btnSELECT:
{
delay(250);
return(selection+1); //add 1 so case statements in void loop start at 1 not 0
break;
}
}
delay(150);
}
}

/* ************************************************************************
Update the LCD (if a message line has changed)
************************************************************************ */
String lastMessage0 = "";
String lastMessage1 = "";

// Displays the given text on the specified line of the lcd


void lcdText(int line, String lcdMessage) {
// Don't display an unchanged message to eliminate flicker
if ((line == 0) && (lastMessage0 == lcdMessage)) {
return;
}
if ((line == 1) && (lastMessage1 == lcdMessage)) {
return;
}
lcd.setCursor(0, line);
lcd.print(" ");
lcd.setCursor(0, line);
lcd.print(lcdMessage);
if (line == 0) {
lastMessage0 = lcdMessage;
}
if (line == 1) {
lastMessage1 = lcdMessage;
}
return;
}

/* ************************************************************************
Read the buttons on the lcd shield
************************************************************************ */
int readButtons()
{
int keyIn = 0;
keyIn = analogRead(0); // read the value from the sensor
// Buttons when read are centered at these values: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (keyIn > 1000) return btnNONE; // 1st option for efficiency
if (keyIn < 50) return btnRIGHT;
if (keyIn < 195) return btnUP;
if (keyIn < 380) return btnDOWN;
if (keyIn < 555) return btnLEFT;
if (keyIn < 790) return btnSELECT;
return btnNONE; // when all others fail
}

/* ************************************************************************
Use the LCD as a debugging display.
************************************************************************ */
void debugDisplay(String line0, String line1) {
lcdText(0, line0);
lcdText(1, line1);
// Wait for any button to be pressed
while (readButtons() == btnNONE) {
delay(100);
}
delay(100); // Little delay to let the key pad settle
}

Program 5 : https://circuitdigest.com/microcontroller-projects/arduino-alarm-clock

#include <Wire.h>
#include<EEPROM.h>
#include <RTClib.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(3, 2, 4, 5, 6, 7);


RTC_DS1307 RTC;
int temp,inc,hours1,minut,add=11;

int next=10;
int INC=11;
int set_mad=12;

#define buzzer 13

int HOUR,MINUT,SECOND;
void setup()
{
Wire.begin();
RTC.begin();
lcd.begin(16,2);
pinMode(INC, INPUT);
pinMode(next, INPUT);
pinMode(set_mad, INPUT);
pinMode(buzzer, OUTPUT);
digitalWrite(next, HIGH);
digitalWrite(set_mad, HIGH);
digitalWrite(INC, HIGH);

lcd.setCursor(0,0);
lcd.print("Real Time Clock");
lcd.setCursor(0,1);
lcd.print("Circuit Digest ");
delay(2000);

if(!RTC.isrunning())
{
RTC.adjust(DateTime(__DATE__,__TIME__));
}
}

void loop()
{
int temp=0,val=1,temp4;
DateTime now = RTC.now();
if(digitalRead(set_mad) == 0) //set Alarm time
{
lcd.setCursor(0,0);
lcd.print(" Set Alarm ");
delay(2000);
defualt();
time();
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Alarm time ");
lcd.setCursor(0,1);
lcd.print(" has been set ");
delay(2000);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time:");
lcd.setCursor(6,0);
lcd.print(HOUR=now.hour(),DEC);
lcd.print(":");
lcd.print(MINUT=now.minute(),DEC);
lcd.print(":");
lcd.print(SECOND=now.second(),DEC);
lcd.setCursor(0,1);
lcd.print("Date: ");
lcd.print(now.day(),DEC);
lcd.print("/");
lcd.print(now.month(),DEC);
lcd.print("/");
lcd.print(now.year(),DEC);
match();
delay(200);
}

void defualt()
{
lcd.setCursor(0,1);
lcd.print(HOUR);
lcd.print(":");
lcd.print(MINUT);
lcd.print(":");
lcd.print(SECOND);
}

/*Function to set alarm time and feed time into Internal eeprom*/

void time()
{
int temp=1,minuts=0,hours=0,seconds=0;
while(temp==1)
{
if(digitalRead(INC)==0)
{
HOUR++;
if(HOUR==24)
{
HOUR=0;
}
while(digitalRead(INC)==0);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set Alarm Time ");
//lcd.print(x);
lcd.setCursor(0,1);
lcd.print(HOUR);
lcd.print(":");
lcd.print(MINUT);
lcd.print(":");
lcd.print(SECOND);
delay(100);
if(digitalRead(next)==0)
{
hours1=HOUR;
EEPROM.write(add++,hours1);
temp=2;
while(digitalRead(next)==0);
}
}

while(temp==2)
{
if(digitalRead(INC)==0)
{
MINUT++;
if(MINUT==60)
{MINUT=0;}
while(digitalRead(INC)==0);
}
// lcd.clear();
lcd.setCursor(0,1);
lcd.print(HOUR);
lcd.print(":");
lcd.print(MINUT);
lcd.print(":");
lcd.print(SECOND);
delay(100);
if(digitalRead(next)==0)
{
minut=MINUT;
EEPROM.write(add++, minut);
temp=0;
while(digitalRead(next)==0);
}
}
delay(1000);
}
/* Function to chack medication time */

void match()
{
int tem[17];
for(int i=11;i<17;i++)
{
tem[i]=EEPROM.read(i);
}
if(HOUR == tem[11] && MINUT == tem[12])
{
beep();
beep();
beep();
beep();
lcd.clear();
lcd.print("Wake Up........");
lcd.setCursor(0,1);
lcd.print("Wake Up.......");
beep();
beep();
beep();
beep();
}

/* function to buzzer indication */

void beep()
{
digitalWrite(buzzer,HIGH);
delay(500);
digitalWrite(buzzer, LOW);
delay(500);
}

https://leetsacademy.blogspot.co.id/2017/05/arduino-based-countdown-timer.html

You might also like