Devices & Components
Arduino Uno Rev3
MAX7219 8-Digital Segment Digital LED Display Tube For Arduino
prototype breadboard
Active Buzzer 5V (HXD)
7 Segments Display
4x4 Matrix Membrane Keypad
Hardware & Tools
drill, screwdriver, soldering iron
Software & Tools
ChatGPT
Project description
Code
Countdown timer for MAX7219
c
Complete code for programmable timer MAX7219.
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] = {5, 4, 3, 2}; 14byte colPins[COLS] = {9, 8, 7, 6}; 15 16Keypad keypadObj = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); 17LedControl lc = LedControl(12, 11, 10, 1); 18 19long csecondsLeft; 20int decrement = 3; 21bool explosed = false; 22 23long hours = 0; 24long minutes = 0; 25long seconds = 0; 26long hundredths = 0; 27 28char keypadOutput[32] = {0}; 29byte playSound = 18; 30 31void setup() { 32 csecondsLeft = 0; 33 Serial.begin(9600); 34 lc.shutdown(0, false); 35 lc.setIntensity(0, 8); 36 lc.clearDisplay(0); 37 pinMode(13, OUTPUT); 38 pinMode(18, OUTPUT); 39 analogWrite(18, 0); 40} 41 42bool isProgrammingTime = false; 43String inputKeys = ""; 44String programmedTime = ""; 45bool isCountingDown = false; 46 47void updateDisplay() { 48 49 long remainingSeconds = csecondsLeft / 100; 50 51 hours = remainingSeconds / 3600; 52 remainingSeconds %= 3600; 53 minutes = remainingSeconds / 60; 54 seconds = remainingSeconds % 60; 55 hundredths = csecondsLeft % 100; 56 57 lc.setDigit(0, 7, hours / 10, false); 58 lc.setDigit(0, 6, hours % 10, false); 59 lc.setDigit(0, 5, minutes / 10, false); 60 lc.setDigit(0, 4, minutes % 10, false); 61 lc.setDigit(0, 3, seconds / 10, false); 62 lc.setDigit(0, 2, seconds % 10, false); 63 lc.setDigit(0, 1, hundredths / 10, false); 64 lc.setDigit(0, 0, hundredths % 10, false); 65} 66 67void programTime() { 68 69 if (inputKeys.length() >= 8) { 70 71 inputKeys = inputKeys.substring(0, 8); 72 73 programmedTime = inputKeys.substring(0, 2) + ":" + inputKeys.substring(2, 4) + ":" + inputKeys.substring(4, 6) + ":" + inputKeys.substring(6, 8); 74 75 Serial.print("inputKeys: "); Serial.println(inputKeys); 76 Serial.print("programmedTime: "); Serial.println(programmedTime); 77 78 lc.clearDisplay(0); 79 80 inputKeys = ""; 81 } 82} 83 84void startCountdown() { 85 86 Serial.println("Starting countdown..."); 87 88 if (programmedTime.length() == 11) { 89 hours = programmedTime.substring(0, 2).toInt(); 90 minutes = programmedTime.substring(3, 5).toInt(); 91 seconds = programmedTime.substring(6, 8).toInt(); 92 hundredths = programmedTime.substring(9, 11).toInt(); 93 94 csecondsLeft = ((hours * 3600) + (minutes * 60) + seconds) * 100 + hundredths; 95 } else { 96 Serial.println("Invalid programmed time. Please enter a time greater than zero."); 97 return; 98 } 99 100 updateDisplay(); 101 isCountingDown = true; 102} 103 104 105void loop() { 106 char key = keypadObj.getKey(); 107 108 if (key) { 109 Serial.print("Key Pressed: "); 110 Serial.println(key); 111 112 if (key >= '0' && key <= '9') { 113 int num = key - '0'; 114 int displayIndex = 7 - inputKeys.length(); 115 lc.setDigit(0, displayIndex, num, false); 116 inputKeys += key; 117 tone(playSound, 1000, 50); 118 } 119 120 if (!isProgrammingTime && key == 'A') { 121 122 Serial.println("Programming time..."); 123 isProgrammingTime = true; 124 inputKeys = ""; 125 programmedTime = ""; 126 lc.clearDisplay(0); 127 128 } else if (isProgrammingTime && key == 'D') { 129 130 isProgrammingTime = false; 131 programTime(); 132 Serial.print("Programmed Time: "); 133 Serial.println(programmedTime); 134 startCountdown(); 135 programmedTime = ""; 136} 137 } 138 139 else if (isCountingDown) { 140 141 delay(30); 142 csecondsLeft -= decrement; 143 144 if (csecondsLeft < 0) { 145 csecondsLeft = 0; 146 if (!explosed) { 147 tone(playSound, 1000, 50); 148 digitalWrite(13, HIGH); 149 } 150 explosed = true; 151 } 152 updateDisplay(); 153 if (csecondsLeft % 35 == 0) { 154 tone(playSound, 3236, 50); 155 } 156 157 } 158}
Documentation
Wiring diagram
Wiring diagram with keypad and MAX7219 display
MAX7219-8-Digit-LED-Display_bb.png

Comments
Only logged in users can leave comments