Hello, I'm just learning.
I have a little project, and having trouble
This is what I need to do
- Display: Counts down from 15 to 0 on two 7-segment displays
- LED-1: On by default, turns off when Button 1 is pressed
- LED-2: Blinks during countdown, stays on after reaching 0
- Button 1: Starts countdown
- Button 2: Resets everything
- No multiplexing: Each segment wired directly
I'm working with 2 seven segment displays common anode.
2 leds
These will be eventually replaced by relays to power a big display because of power requirements.
I cheated and used ChatGPT to work up a sketch.
The sketch works but displays the numbers like a predator gauntlet.
I cannot decipher in the code how the 7 segment displays are being illuminated.
Any help would be appreciated.
May have to hire someone for this.
code below
Thanks for looking
// Segment mapping for numbers 0-9 on common anode 7-segment display
const byte digits[10] = {
B11000000, // 0
B11111001, // 5
B10100100, // 2
B10110000, // 8
B10011001, // 4
B10010010, // 1
B10000010, // 6
B11111000, // 7
B10000000, // 3
B10010000 // 9
};
// Pins for segments a-g of both displays (no multiplexing)
const int segPinsTens[7] = {2, 3, 4, 5, 6, 7, 8}; // Tens display
const int segPinsOnes[7] = {9, 10, 11, 12, 13, A0, A1}; // Ones display
// Button and LED pins
const int buttonStart = A2;
const int buttonReset = A3;
const int led1 = A4;
const int led2 = A5;
bool counting = false;
int count = 15;
unsigned long lastBlink = 0;
bool led2State = false;
unsigned long countStartTime = 0;
bool displayOff = false;
void setup() {
// Setup segment pins
for (int i = 0; i < 7; i++) {
pinMode(segPinsTens[i], OUTPUT);
pinMode(segPinsOnes[i], OUTPUT);
}
pinMode(buttonStart, INPUT_PULLUP);
pinMode(buttonReset, INPUT_PULLUP);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
digitalWrite(led1, HIGH); // LED-1 initially ON
clearDisplays();
}
void loop() {
// Reset logic
if (digitalRead(buttonReset) == LOW) {
resetSystem();
delay(300); // Debounce
}
// Start countdown on Button 1 press
if (!counting && digitalRead(buttonStart) == LOW) {
digitalWrite(led1, LOW); // Turn off LED-1
counting = true;
countStartTime = millis();
displayOff = false;
delay(300); // Debounce
}
if (counting) {
unsigned long currentMillis = millis();
// Countdown logic (1 second per decrement)
if (count > 0 && currentMillis - countStartTime >= 1000) {
count--;
countStartTime = currentMillis;
}
// Flash LED-2 every 1 second during countdown
if (count > 0 && currentMillis - lastBlink >= 500) {
led2State = !led2State;
digitalWrite(led2, led2State);
lastBlink = currentMillis;
}
// At 0: keep LED-2 on, turn off displays after 1s
if (count == 0) {
digitalWrite(led2, HIGH);
if (!displayOff && currentMillis - countStartTime >= 1000) {
clearDisplays();
displayOff = true;
}
}
// Update display
if (!displayOff) {
showNumber(count);
}
}
}
void showNumber(int num) {
int tens = num / 10;
int ones = num % 10;
displayDigit(tens, segPinsTens);
displayDigit(ones, segPinsOnes);
}
void displayDigit(int digit, const int segPins[7]) {
byte segs = digits[digit];
for (int i = 0; i < 7; i++) {
digitalWrite(segPins[i], bitRead(segs, 7 - i)); // Common anode: HIGH = off
}
}
void clearDisplays() {
for (int i = 0; i < 7; i++) {
digitalWrite(segPinsTens[i], HIGH); // Turn off all segments
digitalWrite(segPinsOnes[i], HIGH);
}
}
void resetSystem() {
counting = false;
count = 15;
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
led2State = false;
displayOff = false;
clearDisplays();
}