Maintenance: Project Hub will be unavailable on Monday 24 (9AM to 6PM CET) while we deploy critical improvements
Components and supplies
MAX7219 Module
Arduino Nano
Waterproof Enclosure
4x4 Matrix Membrane Keypad
1Pcs Black 18650 Battery Holder 3.7V Clip Holder Box Case For Rechargeable Li-ion Storage Box 18650 Battery Holder
Tools and machines
Digital Multimeter
drill, screwdriver, soldering iron
Apps and platforms
ChatGPT
KiCad
Project description
Code
Advanced Timer Code
c
Advanced code for commercial timer.
1#include <Keypad.h> 2#include <LedControl.h> 3#include <SPI.h> 4 5const byte ROWS = 4; 6const byte COLS = 4; 7char keys[ROWS][COLS] = { 8 {'1', '2', '3', 'A'}, 9 {'4', '5', '6', 'B'}, 10 {'7', '8', '9', 'C'}, 11 {'*', '0', '#', 'D'} 12}; 13byte rowPins[ROWS] = {6, 7, 8, 9}; 14byte colPins[COLS] = {2, 3, 4, 5}; 15 16Keypad keypadObj = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); 17LedControl lc = LedControl(10, 11, 12, 1); 18 19long csecondsLeft; 20int decrement = 3; 21bool explosed = false; 22bool defused = false; 23 24long hours = 0; 25long minutes = 0; 26long seconds = 0; 27long hundredths = 0; 28 29String code = ""; 30 31char keypadOutput[32] = {0}; 32byte playSound = A0; 33 34 35void setup() { 36 37 csecondsLeft = 0; 38 Serial.begin(19200); 39 lc.shutdown(0, false); 40 lc.setIntensity(0, 10); 41 lc.clearDisplay(0); 42 43 tone(playSound, 3236, 150); delay(300); 44 tone(playSound, 3236, 150); delay(300); 45 tone(playSound, 3236, 150); delay(300); 46 47 lc.setChar(0,7,'-',false); delay(100); 48 lc.setChar(0,6,'-',false); delay(100); 49 lc.setChar(0,5,'-',false); delay(100); 50 lc.setChar(0,4,'-',false); delay(100); 51 lc.setChar(0,3,'-',false); delay(100); 52 lc.setChar(0,2,'-',false); delay(100); 53 lc.setChar(0,1,'-',false); delay(100); 54 lc.setChar(0,0,'-',false); delay(100); 55 56 lc.clearDisplay(0); 57 58 pinMode(A1, OUTPUT); 59 pinMode(13, OUTPUT); 60 pinMode(A0, OUTPUT); 61 analogWrite(A0, 0); 62} 63 64bool isProgrammingTime = false; 65String inputKeys = ""; 66String programmedTime = ""; 67bool isCountingDown = false; 68 69void updateDisplay() { 70 71 long remainingSeconds = csecondsLeft / 60; 72 73 hours = remainingSeconds / 3600; 74 remainingSeconds %= 3600; 75 minutes = remainingSeconds / 60; 76 seconds = remainingSeconds % 60; 77 hundredths = csecondsLeft % 60; 78 79 lc.setDigit(0, 7, hours / 10, false); 80 lc.setDigit(0, 6, hours % 10, false); 81 lc.setDigit(0, 5, minutes / 10, false); 82 lc.setDigit(0, 4, minutes % 10, false); 83 lc.setDigit(0, 3, seconds / 10, false); 84 lc.setDigit(0, 2, seconds % 10, false); 85 lc.setDigit(0, 1, hundredths / 10, false); 86 lc.setDigit(0, 0, hundredths % 10, false); 87} 88 89void programTime() { 90 91 if (inputKeys.length() >= 8) { 92 93 inputKeys = inputKeys.substring(0, 8); 94 95 programmedTime = inputKeys.substring(0, 2) + ":" + inputKeys.substring(2, 4) + ":" + inputKeys.substring(4, 6) + ":" + inputKeys.substring(6, 8); 96 97 Serial.print("inputKeys: "); Serial.println(inputKeys); 98 Serial.print("programmedTime: "); Serial.println(programmedTime); 99 100 lc.clearDisplay(0); 101 102 inputKeys = ""; 103 } 104} 105 106void startCountdown() { 107 108 Serial.println("Starting countdown..."); 109 110 if (programmedTime.length() == 11) { 111 hours = programmedTime.substring(0, 2).toInt(); 112 minutes = programmedTime.substring(3, 5).toInt(); 113 seconds = programmedTime.substring(6, 8).toInt(); 114 hundredths = programmedTime.substring(9, 11).toInt(); 115 116 csecondsLeft = ((hours * 3600) + (minutes * 60) + seconds) * 60 + hundredths; 117 } else { 118 Serial.println("Invalid programmed time. Please enter a time greater than zero."); 119 return; 120 } 121 122 updateDisplay(); 123 isCountingDown = true; 124} 125 126 127void loop() { 128 129 char key = keypadObj.getKey(); 130 131 if (key) { 132 Serial.print("Key Pressed: "); 133 Serial.println(key); 134 135 if (key >= '0' && key <= '9') { 136 int num = key - '0'; 137 int displayIndex = 7 - inputKeys.length(); 138 lc.setDigit(0, displayIndex, num, false); 139 inputKeys += key; 140 tone(playSound, 1000, 50); 141 } 142 143 if (!isProgrammingTime && key == 'A') { 144 145 Serial.println("Programming time..."); 146 isProgrammingTime = true; 147 inputKeys = ""; 148 programmedTime = ""; 149 lc.clearDisplay(0); 150 151 } else if (isProgrammingTime && key == 'D') { 152 153 isProgrammingTime = false; 154 programTime(); 155 Serial.print("Programmed Time: "); 156 Serial.println(programmedTime); 157 startCountdown(); 158 programmedTime = ""; 159 160 } else if (isCountingDown && key == 'B') { 161 162 Serial.println(code); 163 decrement = 0; 164 defused = true; 165 code = ""; 166 167 } else if (isCountingDown && key == 'D') { 168 169 Serial.println(code); 170 decrement = 3; 171 defused = false; 172 code = ""; 173 174 } else if (key == 'C') { 175 176 isProgrammingTime = false; 177 lc.setDigit(0, 7, 0, false); 178 lc.clearDisplay(0); 179 180 } 181 } 182 183 184 else if (isCountingDown) { 185 186 delay(60); 187 csecondsLeft -= decrement; 188 189 if (csecondsLeft < 10) { 190 csecondsLeft = 0; 191 if (!explosed) { 192 tone(playSound, 1000, 50); 193 digitalWrite(13, HIGH); 194 digitalWrite(A1, HIGH); 195 } 196 explosed = true; 197 } 198 updateDisplay(); 199 if (csecondsLeft % 60 == 0) { 200 tone(playSound, 3236, 50); 201 } 202 203 } 204 205}
Comments
Only logged in users can leave comments