#include <Arduino.h>
// Pin definitions
const int dataPin = 2;
const int clockPin = 3;
const int latchPin = 4;
const int increaseTimePin = 5;
const int decreaseTimePin = 6;
const int resetTimePin = 7;
const int attractionClosedPin = 8;
const int openLightPin = 9; // Renamed from redLEDPin
// Variables to hold the current time and special state
int waitingTime = 0;
bool specialState = false;
bool ledOn = true;
bool isDisabled = false; // Flag to track if the counter functions are disabled
// Seven-segment display digit patterns (common cathode)
const byte digitPatterns[10] = {
0b11000000, // 0
0b11111001, // 1
0b10100100, // 2
0b10110000, // 3
0b10011001, // 4
0b10010010, // 5
0b10000010, // 6
0b11111000, // 7
0b10000000, // 8
0b10010000 // 9
};
// Patterns for letters (common cathode)
const byte letterC = 0b11000110; // C
const byte letterL = 0b11000111; // L
const byte letterO = 0b11000000; // O
const byte blankPattern = 0b11111111; // Blank pattern for leading zeros
// Animation sequence for circular flashing
const byte animationSequence[6] = {
0b11111101, // Segment a
0b11111011, // Segment f
0b11110111, // Segment e
0b11101111, // Segment d
0b11011111, // Segment c
0b11111110, // Segment b
};
void showAnimation() {
for (int loop = 0; loop < 3; loop++) { // Loop the animation 3 times
for (int i = 0; i < 6; i++) {
shiftOut(dataPin, clockPin, MSBFIRST, animationSequence[i]);
shiftOut(dataPin, clockPin, MSBFIRST, animationSequence[i]);
shiftOut(dataPin, clockPin, MSBFIRST, animationSequence[i]);
digitalWrite(latchPin, LOW);
digitalWrite(latchPin, HIGH);
delay(40); // Animation speed
}
}
}
// Function to display a number or message on the seven-segment displays
void displayNumber(int number) {
if (specialState) {
shiftOut(dataPin, clockPin, MSBFIRST, letterO); // O
shiftOut(dataPin, clockPin, MSBFIRST, letterL); // L
shiftOut(dataPin, clockPin, MSBFIRST, letterC); // C
} else {
int hundreds = number / 100;
int tens = (number / 10) % 10;
int units = number % 10;
if (hundreds > 0) {
shiftOut(dataPin, clockPin, MSBFIRST, digitPatterns[units]);
shiftOut(dataPin, clockPin, MSBFIRST, digitPatterns[tens]);
shiftOut(dataPin, clockPin, MSBFIRST, digitPatterns[hundreds]);
} else if (tens > 0) {
shiftOut(dataPin, clockPin, MSBFIRST, digitPatterns[units]);
shiftOut(dataPin, clockPin, MSBFIRST, digitPatterns[tens]);
shiftOut(dataPin, clockPin, MSBFIRST, blankPattern);
} else {
shiftOut(dataPin, clockPin, MSBFIRST, digitPatterns[units]);
shiftOut(dataPin, clockPin, MSBFIRST, blankPattern);
shiftOut(dataPin, clockPin, MSBFIRST, blankPattern);
}
}
digitalWrite(latchPin, LOW);
digitalWrite(latchPin, HIGH);
}
void setup() {
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(increaseTimePin, INPUT_PULLUP);
pinMode(decreaseTimePin, INPUT_PULLUP);
pinMode(resetTimePin, INPUT_PULLUP);
pinMode(attractionClosedPin, INPUT_PULLUP);
pinMode(openLightPin, OUTPUT); // Renamed from redLEDPin
digitalWrite(openLightPin, HIGH); // Renamed from redLEDPin
displayNumber(waitingTime);
}
void loop() {
if (!isDisabled) {
if (digitalRead(increaseTimePin) == LOW) {
showAnimation();
if (waitingTime < 60) {
waitingTime += 5;
} else if (waitingTime < 240) {
waitingTime += 10;
if (waitingTime > 240) waitingTime = 240; // Ensure we don't exceed 240
}
specialState = false;
displayNumber(waitingTime);
delay(200); // debounce delay
}
if (digitalRead(decreaseTimePin) == LOW) {
showAnimation();
if (waitingTime > 61) {
waitingTime -= 10;
} else {
waitingTime -= 5;
}
if (waitingTime < 0) waitingTime = 0;
specialState = false;
displayNumber(waitingTime);
delay(200); // debounce delay
}
}
if (digitalRead(resetTimePin) == LOW) {
waitingTime = 0;
specialState = false;
ledOn = true;
isDisabled = false; // Enable the counter functions
digitalWrite(openLightPin, HIGH); // Renamed from redLEDPin
displayNumber(waitingTime);
delay(200); // debounce delay
}
if (digitalRead(attractionClosedPin) == LOW) {
specialState = true;
ledOn = false;
isDisabled = true; // Disable the counter functions
digitalWrite(openLightPin, LOW); // Renamed from redLEDPin
displayNumber(waitingTime);
delay(200); // debounce delay
}
}