/*
Britans got tallent buzzer simulator
4 Buzzers, 4 red buttons
and a golden buzzer
By Jeff Paewai
*/
#define bounceDelay 20 // Button Debounce delay
#define minButtonPress 3 // min time to cound as a button press
#define noOfButtons 6 // Number Of buttons
const int buttonPins[] = {4, 5, 6, 7, 8, 3}; // no 8 is the reset, 4-7 relay+buzzer controlls
uint8_t previousMillis[noOfButtons]; // Timers to time out bounce duration for each button
uint8_t pressCount[noOfButtons]; // Counts the number of times the button is detected as pressed, when this count reaches minButtonPress button is regared as debounced
const int RelayPins[] = {9, 10, 11, 12,13}; // Relay setup
#define noOfRelays 5
const int BuzzerPins[] = {A3, A2, A1, A0}; // Buzzer Setup
#define noOfBuzzers 4 // Number of Relays
#define BuzzerTimer 2000 // Buzzer activation timers
long BuzzerStatus[] ={0,0,0,0}; // keep track of each buzzers on time
#define ConfettiTimer 2000 // Confetti cannon activation relay time
long ConfettiStatus = 0; // Track
void setup()
{
uint8_t i;
//Serial.begin(115200);
for (i = 0; i < noOfButtons; ++i) pinMode(buttonPins[i], INPUT); // Buttons
for (i = 0; i < noOfRelays; ++i) pinMode(RelayPins[i], OUTPUT); // Relays
for (i = 0; i < noOfBuzzers; ++i) pinMode(BuzzerPins[i], OUTPUT); // Buzzers
}
void ResetRelays()
{
for (byte v = 0; v < noOfRelays; ++v) digitalWrite(RelayPins[v], LOW); // Relays
}
void ResetBuzzers()
{
for (byte v = 0; v < noOfBuzzers; ++v) noTone(BuzzerPins[v]); // Buzzers reset
}
void checkConettiTimmer(void)
{
if(millis() - ConfittiStatus > ConfettiTimer) digitalWrite(RelayPins[4], LOW);
}
void loop()
{
debounce();
checkBuzzerTimmers();
checkConettiTimmer();
}
void activateRelay(byte rly)
{
digitalWrite(RelayPins[rly], HIGH);
}
void activateBuzzer (byte buz)
{
tone(BuzzerPins[buz],100);
}
void activateConfetti(void)
{
ResetRelays();
digitalWrite(RelayPins[4], HIGH);
ConfittiStatus = millis(); //Start Timmer
}
void checkBuzzerTimmers(void)
{
for (byte v = 0; v < noOfBuzzers; ++v)
{
if (millis() - BuzzerStatus[v] > BuzzerTimer ) noTone(BuzzerPins[v]);
}
}
void debounce()
{
uint8_t i;
uint32_t currentMillis = millis();
for (i = 0; i < noOfButtons; ++i) {
if (digitalRead(buttonPins[i])) { //Input is high, button not pressed or in the middle of bouncing and happens to be high
previousMillis[i] = currentMillis; //Set previousMillis to millis to reset timeout
pressCount[i] = 0; //Set the number of times the button has been detected as pressed to 0
} else {
if (currentMillis - previousMillis[i] > bounceDelay) {
previousMillis[i] = currentMillis; //Set previousMillis to millis to reset timeout
++pressCount[i];
if (pressCount[i] == minButtonPress) {
doStuff(i); //Button has been debounced. Call function to do whatever you want done.
}
}
}
}
}
void doStuff(uint8_t buttonNumber)
{
// Serial.print("Button "); Serial.print(buttonNumber);
switch (buttonNumber) {
case 0:activateRelay(0); activateBuzzer(0);break;
case 1:activateRelay(1); activateBuzzer(1);break;
case 2:activateRelay(2); activateBuzzer(2);break;
case 3:activateRelay(3); activateBuzzer(3);break;
case 4:ResetRelays(); ResetBuzzers(); break;
case 5:activateConfetti();
}
}B1
RESET
B2
B3
B4
Rly4
Rly1
Rly3
Rly2
Golden Buzzer
Confetti Cannon Relay