/* QUIZ GAME CODE FOR ARDUINO UNO */
/* Board: Arduino Uno / Nano [atmega328p]
* Owner: Subhadeep Dhang
* Creation Date: 07.05.2022
* Lisence: Open Source [ Anybody can Download and use it]
*/
#define LED1 8
#define LED2 9
#define LED3 10
#define LED4 11
#define LED5 12
#define LED6 13
#define SW1 A0
#define SW2 A1
#define SW3 A2
#define SW4 A3
#define SW5 A4
#define SW6 A5
#define SRT 6
void reset (void);
int count = 0;
int Top_scorer[3]; // 0th pos. is unused
int f1 = 0, f2 = 0, f3 = 0, f4 = 0, f5 = 0, f6 = 0; // To make sure one player pressed one time
long int time;
void setup() {
Serial.begin(9600);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(SW1, INPUT_PULLUP);
pinMode(SW2, INPUT_PULLUP);
pinMode(SW3, INPUT_PULLUP);
pinMode(SW4, INPUT_PULLUP);
pinMode(SW5, INPUT_PULLUP);
pinMode(SW6, INPUT_PULLUP);
pinMode(SRT, INPUT_PULLUP);
}
void loop () {
while (digitalRead(SRT) != LOW); // wait for SRT button to be pressed
Serial.println("\n::::[ START ]::::");
time = millis();
while (count != 2 && (millis() - time) < 10000) { // wait 10 sec or [2 player pressed button] , then reset
if (digitalRead(SW1) == LOW && count < 2 && f1 == 0) {
Serial.println("Player 1");
count++;
f1 = 1;
Top_scorer[count] = 1;
}
if (digitalRead(SW2) == LOW && count < 2 && f2 == 0) {
Serial.println("Player 2");
count++;
f2 = 1;
Top_scorer[count] = 2;
}
if (digitalRead(SW3) == LOW && count < 2 && f3 == 0) {
Serial.println("Player 3");
count++;
f3 = 1;
Top_scorer[count] = 3;
}
if (digitalRead(SW4) == LOW && count < 2 && f4 == 0) {
Serial.println("Player 4");
count++;
f4 = 1;
Top_scorer[count] = 4;
}
if (digitalRead(SW5) == LOW && count < 2 && f5 == 0) {
Serial.println("Player 5");
count++;
f5 = 1;
Top_scorer[count] = 5;
}
if (digitalRead(SW6) == LOW && count < 2 && f6 == 0) {
Serial.println("Player 6");
count++;
f6 = 1;
Top_scorer[count] = 6;
}
digitalWrite((7 + Top_scorer[1]), HIGH); // Turn on winner's led
}
Serial.println("::::[ END ]::::");
long duration = 5; // Blinks 5 times then reset
Serial.print("\nAuto Reseting within: ");
while (duration--) {
digitalWrite((7 + Top_scorer[2]), HIGH); // 1st runner up blink [fast]
delay(400);
digitalWrite((7 + Top_scorer[2]), LOW);
delay(400);
Serial.print(duration);
Serial.print(" ");
}
Serial.print("done.\n");
Serial.println("Press SRT button to start again.");
reset();
}
void reset (void) {
PORTB = 0x00; // turn off all led
count = 0;
Top_scorer[1] = 0;
Top_scorer[2] = 0;
f1 = 0;
f2 = 0;
f3 = 0;
f4 = 0;
f5 = 0;
f6 = 0;
}