Components and supplies
Liquid Crystal Display
Arduino UNO
Breadboard (generic)
Tactile Switch, Top Actuated
Single Turn Potentiometer- 10k ohms
Resistor 10k ohm
Jumper wires (generic)
Resistor 220 ohm
SG90 Micro-servo motor
Project description
Code
Code for Arduino
arduino
1 #include <Time.h> 2#include <TimeLib.h> 3 4#include <LiquidCrystal.h> 5 6#include <EEPROM.h> 7#include <Servo.h> 8 9LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 10 11Servo myServo; 12 13const int BackButtonPin = 6; 14const int StartButtonPin = 7; 15const int MinusButtonPin = 8; 16const int PlusButtonPin = 9; 17 18int settingHours = 0; 19int settingMinutes = 0; 20int settingSeconds = 0; 21time_t settingTime = 0; 22 23int activeHours = 0; 24int activeMinutes = 0; 25int activeSeconds = 0; 26time_t activeTime = 0; 27 28 29time_t startTime = 0; 30time_t elapsedTime = 0; 31 32int BackButtonState = LOW; 33long BackButtonLongPressTime = 0; 34int StartButtonState = LOW; 35int PlusButtonState = LOW; 36int MinusButtonState = LOW; 37int BackButtonPrevState = LOW; 38int StartButtonPrevState = LOW; 39int PlusButtonPrevState = LOW; 40int MinusButtonPrevState = LOW; 41bool BackButtonPress = false; 42bool BackButtonLongPress = false; 43bool StartButtonPress = false; 44bool PlusButtonPress = false; 45bool MinusButtonPress = false; 46 47bool locked = false; 48 49const int READY = 0; 50const int SETTINGS = 1; 51const int LOCKED = 2; 52const int UNLOCKED = 3; 53 54int activeState = READY; 55 56 57int timeData = 0; 58 59void setup() { 60 61 lcd.begin(16, 2); 62 pinMode(BackButtonPin, INPUT); 63 pinMode(StartButtonPin, INPUT); 64 pinMode(PlusButtonPin, INPUT); 65 pinMode(MinusButtonPin, INPUT); 66 myServo.attach(10); 67 Serial.begin(9600); 68 myServo.write(90); 69 70} 71 72void loop() { 73 74 StartButtonPress = false; 75 PlusButtonPress = false; 76 MinusButtonPress = false; 77 78 BackButtonPress = false; 79 BackButtonLongPress = false; 80 BackButtonState = digitalRead(BackButtonPin); 81 if(BackButtonState != BackButtonPrevState) 82 { 83 BackButtonPress = BackButtonState == HIGH; 84 BackButtonPrevState = BackButtonState; 85 86 } 87 else 88 { 89 if(BackButtonState == HIGH) 90 { 91 BackButtonLongPressTime++; 92 if(BackButtonLongPressTime == 100) 93 { 94 BackButtonPress = false; 95 BackButtonLongPress = true; 96 BackButtonLongPressTime = 0; 97 } 98 } 99 else 100 { 101 BackButtonLongPressTime = 0; 102 BackButtonPress = false; 103 BackButtonLongPress = false; 104 } 105 } 106 107 StartButtonPress = false; 108 StartButtonState = digitalRead(StartButtonPin); 109 if(StartButtonState != StartButtonPrevState) 110 { 111 StartButtonPress = StartButtonState == HIGH; 112 StartButtonPrevState = StartButtonState; 113 } 114 115 116 MinusButtonPress = false; 117 MinusButtonState = digitalRead(MinusButtonPin); 118 if(MinusButtonState != MinusButtonPrevState) 119 { 120 MinusButtonPress = MinusButtonState == HIGH; 121 MinusButtonPrevState = MinusButtonState; 122 } 123 124 PlusButtonPress = false; 125 PlusButtonState = digitalRead(PlusButtonPin); 126 if(PlusButtonState != PlusButtonPrevState) 127 { 128 PlusButtonPress = PlusButtonState == HIGH; 129 PlusButtonPrevState = PlusButtonState; 130 } 131 132 switch(activeState) 133 { 134 case READY: 135 myServo.write(90); 136 if(BackButtonPress) 137 { 138 Reset(); 139 } 140 if(BackButtonLongPress) 141 { 142 activeState = SETTINGS; 143 144 } 145 if(StartButtonPress) 146 { 147 activeState = activeState == READY ? LOCKED : READY; 148 if(activeState == LOCKED) 149 { 150 startTime = now(); 151 } 152 } 153 break; 154 155 case SETTINGS: 156 myServo.write(90); 157 if(BackButtonPress) 158 { 159 settingTime = settingSeconds + (60 * settingMinutes) + (3600 * settingHours); 160 activeHours = settingHours; 161 activeMinutes = settingMinutes; 162 activeSeconds = settingSeconds; 163 timeData = 0; 164 activeState = READY; 165 } 166 if(StartButtonPress) 167 { 168 timeData++; 169 if(timeData == 3) 170 { 171 timeData = 0; 172 173 } 174 } 175 if(MinusButtonPress) 176 { 177 switch(timeData) 178 { 179 case 0: 180 settingHours--; 181 if(settingHours == -1) 182 { 183 settingHours = 99; 184 } 185 break; 186 case 1: 187 settingMinutes--; 188 if(settingMinutes == -1) 189 { 190 settingMinutes = 59; 191 } 192 break; 193 case 2: 194 settingSeconds--; 195 if(settingSeconds == -1) 196 { 197 settingSeconds = 59; 198 } 199 break; 200 } 201 202 } 203 if(PlusButtonPress) 204 { 205 switch(timeData) 206 { 207 case 0: 208 settingHours++; 209 if(settingHours == 100) 210 { 211 settingHours = 0; 212 } 213 break; 214 case 1: 215 settingMinutes++; 216 if(settingMinutes == 60) 217 { 218 settingMinutes = 0; 219 } 220 break; 221 case 2: 222 settingSeconds++; 223 if(settingSeconds == 60) 224 { 225 settingSeconds = 0; 226 } 227 break; 228 } 229 } 230 231 break; 232 233 case LOCKED: 234 myServo.write(0); 235 if(StartButtonPress) 236 { 237 activeState = READY; 238 } 239 if(BackButtonPress) 240 { 241 Reset(); 242 activeState = READY; 243 } 244 break; 245 246 case UNLOCKED: 247 myServo.write(90); 248 if(BackButtonPress || StartButtonPress || MinusButtonPress || PlusButtonPress) 249 { 250 activeState = READY; 251 } 252 break; 253 } 254 255 switch(activeState) 256 { 257 case READY: 258 case SETTINGS: 259 260 break; 261 case LOCKED: 262 activeTime = settingTime - (now() - startTime); 263 if(activeTime <= 0) 264 { 265 activeState = UNLOCKED; 266 } 267 break; 268 } 269 270 lcd.setCursor(0, 0); 271 switch(activeState) 272 { 273 case READY: 274 lcd.print("Ready: Start "); 275 lcd.setCursor(0, 1); 276 lcd.print(activeHours); 277 lcd.print(" "); 278 lcd.print(activeMinutes); 279 lcd.print(" "); 280 lcd.print(activeSeconds); 281 lcd.print(" "); 282 break; 283 case SETTINGS: 284 lcd.print("Set Timer: "); 285 switch(timeData) 286 { 287 case 0: 288 289 lcd.print("HRS "); 290 break; 291 case 1: 292 lcd.print("MINS"); 293 break; 294 case 2: 295 lcd.print("SECS"); 296 break; 297 } 298 lcd.setCursor(0, 1); 299 lcd.print(settingHours); 300 lcd.print(" "); 301 lcd.print(settingMinutes); 302 lcd.print(" "); 303 lcd.print(settingSeconds); 304 lcd.print(" "); 305 break; 306 case LOCKED: 307 lcd.print("Waiting... "); 308 lcd.setCursor(0, 1); 309 if(hour(activeTime) < 10) lcd.print("0"); 310 lcd.print(hour(activeTime)); 311 lcd.print(": "); 312 if(minute(activeTime) < 10) lcd.print("0"); 313 lcd.print(minute(activeTime)); 314 lcd.print(": "); 315 if(second(activeTime) < 10) lcd.print("0"); 316 lcd.print(second(activeTime)); 317 Logan Ritten 318 319 break; 320 case UNLOCKED: 321 lcd.print(" UNLOCKED! "); 322 lcd.setCursor(0, 1); 323 lcd.print(" "); 324 break; 325 } 326 delay(10); 327} 328 329void Reset() 330{ 331 activeState = READY; 332 activeHours = settingHours; 333 activeMinutes = settingMinutes; 334 activeSeconds = settingSeconds; 335} 336
Downloadable files
Flow Chart
This just shows how the code is thinking and which buttons result in which actions.
Flow Chart
Flow Chart
This just shows how the code is thinking and which buttons result in which actions.
Flow Chart
Wiring Diagram
Wiring Diagram
Wiring Diagram
Wiring Diagram
Comments
Only logged in users can leave comments
LoganSpace42
0 Followers
•0 Projects
Table of contents
Intro
25
0