Devices & Components
Arduino Uno Rev3
Single Turn Potentiometer- 10k ohms
Resistor 10k ohm
Pushbutton switch 12mm
Alphanumeric LCD, 16 x 2
Resistor 220 ohm
Project description
Code
Code for Arduino
arduino
This code allows the clock to constantly count seconds while allowing you to press buttons to change the hour or the minute.
1unsigned long seconds; 2int hour = 1; // what hour the program starts with 3int minute = 0; 4 5 6#include <LiquidCrystal.h> 7LiquidCrystal lcd(12,11,5,4,3,2); 8 9 10const int pinone = 8; // the pin that the arduino is reading 11const int pintwo = 13; // the pin that the arduino is reading 12const int pinthree = 7; // the pin that the arduino is reading 13int state = 0; 14int statetwo = 0; 15int statethree = 0; 16 17void setup() { 18 // put your setup code here, to run once: 19Serial.begin(9600); 20 21pinMode(pinthree,INPUT); 22 23lcd.begin(16, 2); 24lcd.setCursor (0, 0); 25lcd.print("This is a clock"); 26lcd.setCursor (0, 1); 27lcd.print("Time:"); 28seconds = 0; 29 30} 31 32void loop() { 33 34 state = digitalRead(pinone); 35 statetwo = digitalRead(pintwo); 36 statethree = digitalRead(pinthree); 37 38 // put your main code here, to run repeatedly: 39Serial.print("Time: "); 40 41Serial.println(seconds); 42delay(1000); // 1000 milliseconds = 1 second 43seconds=seconds+1; 44 45 46lcd.setCursor(13,1); 47lcd.print(seconds); // display seconds 48lcd.print("s"); // 1 second passes 49 50 51lcd.setCursor(10,1); 52lcd.print(minute); // display minutes 53lcd.print(":"); 54 55 56lcd.setCursor(7,1); 57lcd.print(hour); // display hour 58lcd.print(":"); 59 60 61 62if(statethree == HIGH){ // When switch is pressed it will read HIGH 63 64 hour++; // LEFT BUTTON - add a hour 65 66 67 lcd.setCursor(0,0); 68 lcd.print("Adjust Hour "); 69} 70 else if(state == HIGH){ // When switch is pressed it will read HIGH 71 72 minute++; //MIDDLE BUTTON - add a minute 73 74 75 lcd.setCursor(0,0); 76 lcd.print("Adjust Minute "); 77 } 78 79 80 else if (statetwo == HIGH){ // When switch is pressed it will read HIGH 81 82 lcd.setCursor(0,0); 83 lcd.print("[HOUR][MIN] INFO"); //RIGHT BUTTON - show info 84 } 85 86 87if (seconds >= 60){ 88 seconds = 0; 89 minute = minute + 1; // count up to a minute 90 91 92 93if (minute >= 60){ 94minute=0; 95hour = hour + 1; // count up to a hour 96 97 98 99if (hour == 13){ // clock does not pass 12, after 12, the next number should be 1 100hour = 1; 101 102 103} 104} 105} 106} 107
Code for Arduino
arduino
This code allows the clock to constantly count seconds while allowing you to press buttons to change the hour or the minute.
1unsigned long seconds; 2int hour = 1; // what hour the program starts 3 with 4int minute = 0; 5 6 7#include <LiquidCrystal.h> 8LiquidCrystal lcd(12,11,5,4,3,2); 9 10 11const 12 int pinone = 8; // the pin that the arduino is reading 13const int pintwo = 13; 14 // the pin that the arduino is reading 15const int pinthree = 7; // the pin that 16 the arduino is reading 17int state = 0; 18int statetwo = 0; 19int statethree 20 = 0; 21 22void setup() { 23 // put your setup code here, to run once: 24Serial.begin(9600); 25 26pinMode(pinthree,INPUT); 27 28lcd.begin(16, 29 2); 30lcd.setCursor (0, 0); 31lcd.print("This is a clock"); 32lcd.setCursor 33 (0, 1); 34lcd.print("Time:"); 35seconds = 0; 36 37} 38 39void loop() { 40 41 42 state = digitalRead(pinone); 43 statetwo = digitalRead(pintwo); 44 statethree 45 = digitalRead(pinthree); 46 47 // put your main code here, to run repeatedly: 48Serial.print("Time: 49 "); 50 51Serial.println(seconds); 52delay(1000); // 1000 milliseconds = 1 second 53seconds=seconds+1; 54 55 56lcd.setCursor(13,1); 57lcd.print(seconds); 58 // display seconds 59lcd.print("s"); // 1 second passes 60 61 62lcd.setCursor(10,1); 63lcd.print(minute); 64 // display minutes 65lcd.print(":"); 66 67 68lcd.setCursor(7,1); 69lcd.print(hour); 70 // display hour 71lcd.print(":"); 72 73 74 75if(statethree == HIGH){ // When 76 switch is pressed it will read HIGH 77 78 hour++; // LEFT BUTTON - add 79 a hour 80 81 82 lcd.setCursor(0,0); 83 lcd.print("Adjust Hour "); 84} 85 86 else if(state == HIGH){ // When switch is pressed it will read HIGH 87 88 89 minute++; //MIDDLE BUTTON - add a minute 90 91 92 lcd.setCursor(0,0); 93 94 lcd.print("Adjust Minute "); 95 } 96 97 98 else if (statetwo 99 == HIGH){ // When switch is pressed it will read HIGH 100 101 lcd.setCursor(0,0); 102 103 lcd.print("[HOUR][MIN] INFO"); //RIGHT BUTTON - show info 104 } 105 106 107if 108 (seconds >= 60){ 109 seconds = 0; 110 minute = minute + 1; // count up to a minute 111 112 113 114 115if (minute >= 60){ 116minute=0; 117hour = hour + 1; // count up 118 to a hour 119 120 121 122if (hour == 13){ // clock does not pass 12, after 12, 123 the next number should be 1 124hour = 1; 125 126 127} 128} 129} 130} 131
Downloadable files
Replace the tilt sensor in project 11 with the switch from project 9
Looking at the code, the way the button works in project 9 will allow you to use a switch, for this project the switch will adjust hour and minutes.
Replace the tilt sensor in project 11 with the switch from project 9

Use project 9 and 11 from projects book
Use project 9 and 11 from projects book

Replace the tilt sensor in project 11 with the switch from project 9
Looking at the code, the way the button works in project 9 will allow you to use a switch, for this project the switch will adjust hour and minutes.
Replace the tilt sensor in project 11 with the switch from project 9

Comments
Only logged in users can leave comments