LCD KeyPad Shield
LCD KeyPad Shield
com/id/Arduino-Timer-With-OnOff-Set-Point/
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]
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
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()...
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 ");
}
}
//Enter button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinEnter);
//Down button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinRight);
//Up button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinLeft);
}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;
}
Program 3 : http://blog.rkflyer.co.uk/2014/02/0-cursorposition-move-cursor-up-one.html
int timer = 0;
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 buttonCancel = 12; // Set pun for CANCEL Button (No currently used)
int buttonStateDown = 0;
int buttonState;
#define MOVECURSOR 1
#define MOVELIST 2
void setup()
// Setup stuff that needs to be done regardless of whether entering setup mode or not
lcd.begin(totalCols, totalRows);
lcd.setLED1Pin(1);
Serial.begin(9600);
pinMode(buttonUp, INPUT);
pinMode(buttonDown, INPUT);
buttonStateUp = digitalRead(buttonUp);
buttonStateDown = digitalRead(buttonDown);
byte redraw = MOVELIST; // triggers whether menu is redrawn after cursor move.
byte totalMenuItems = 0; //a while loop below will set this to the # of menu items.
char* menuItems[]={
"",
};
totalMenuItems++;
totalMenuItems--;
// 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())
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.
if(cursorPosition>0)
break;
// this sees if there are menu items below the bottom of the LCD screen & sees if cursor is at bottom
of LCD
break;
case 4: // SELECT BUTTON PUSHED
lcd.clear();
lcd.setCursor(0,1);
lcd.print("from here");
Serial.print(topItemDisplayed + cursorPosition);
Serial.println(menuItems[topItemDisplayed + cursorPosition]);
delay(2000);
stillSelecting = false;
break;
lcd.clear();
lcd.setCursor(0,1);
lcd.print("from here");
Serial.println(menuItems[topItemDisplayed + cursorPosition]);
break;
lcd.clear();
lcd.setCursor(0,1);
Serial.print(topItemDisplayed + cursorPosition);
Serial.println(menuItems[topItemDisplayed + cursorPosition]);
break;
lcd.clear();
lcd.setCursor(0,1);
Serial.print(topItemDisplayed + cursorPosition);
Serial.println(menuItems[topItemDisplayed + cursorPosition]);
break;
case 4: // menu item 5 selected
lcd.clear();
lcd.setCursor(0,1);
Serial.print(topItemDisplayed + cursorPosition);
Serial.println(menuItems[topItemDisplayed + cursorPosition]);
break;
lcd.clear();
lcd.setCursor(0,1);
Serial.print(topItemDisplayed + cursorPosition);
Serial.println(menuItems[topItemDisplayed + cursorPosition]);
break;
break;
// stillSelecting = false;
Program 4 : http://moodle.oside.us/mod/page/view.php?id=105
*
VerticalMenu - Simple vertical menu for the Arduino LCD Shield
// 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 = " ";
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)
/* ************************************************************************
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
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, "");
}
/* ************************************************************************
Update the LCD (if a message line has changed)
************************************************************************ */
String lastMessage0 = "";
String lastMessage1 = "";
/* ************************************************************************
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>
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();
}
void beep()
{
digitalWrite(buzzer,HIGH);
delay(500);
digitalWrite(buzzer, LOW);
delay(500);
}
https://leetsacademy.blogspot.co.id/2017/05/arduino-based-countdown-timer.html