Hi, I've tried making a Gameshow buzzer using code I've found online and adding my own code to make it do more things. Originally it can make a sound, wait for a button to be pressed, detect the button, detect which button was pressed first and end the game. I use 3 different buttons and the idea is that 3 people should be able to play and the person who presses his/her button first gets to answer the question (the question is asked verbally by a "Gameshow host").
The code I've added lets the Gameshow host press one of two buttons printing out if the answer was wrong or right. This works! Although not always. The first part of the game always works but when the host presses the "right answer"- or "wrong answer"-button it doesnt always register and thats the problem. I am to be honest pretty lost in why it only works sometimes and would very much appreciate some help with figuring it out.
*Note: I am 99% sure that the physical assembly is right and that the problem lies in the code.
here is my code:
/**********************************
* name:Building a Quiz Buzzer System
* function: first press button 4 to start. If you press button 1 first, you will see the corresponding LED light up and the buzzer will beep. Then press button 4 again to reset before you press other buttons.
**********************************/
//Email:support@sunfounder.com
//Website:www.sunfounder.com
#define button1 2 //the number of the button 1
#define button2 3 //button2 attach to
#define button3 4 //button3 attach to
#define button4 9 //button4 attach to
#define buzzerPin 5 //the buzzer attach to
#define buttonRight 11 //Rätt Svar knapp
#define buttonWrong 12 // Fel svar knapp
#define LED1 6 //LED 1attach to
#define LED2 7 //LED2attach to
#define LED3 8 //LED3 attach to
#define LED4 10 //LED4 attach to
#define uint8 unsigned char
uint8 flag = 0; //used to indicate the state of button4 key
uint8 b1State,b2State,b3State,b4State,switchStateCorrect,switchStateWrong = 0;
void setup()
{
Serial.begin(9600);
//initialize buzzer,LED1, LED2, LED3 and LED4 as output
pinMode(buzzerPin, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
//initialize button1,button2 andbutton3 as input,combined with pullup
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
//turn all the led off
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
pinMode(buttonRight, INPUT_PULLUP);
pinMode(buttonWrong, INPUT_PULLUP);
}
void loop()
{
//turn all the led off
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
//read the state of the button4
b4State = digitalRead(button4);
//when button4 pressed
if(b4State == 0)
{
if(b4State == 0) //confirm that the button4 is pressed
{
flag = 1; //if so,flag is 1
digitalWrite(LED4, HIGH); //turn the host LED on
delay(200);
}
}
if(1 == flag)
{
//read the state of the button of buttons
b1State = digitalRead(button1);
b2State = digitalRead(button2);
b3State = digitalRead(button3);
//If the button1 press the first
if(b1State == 0)
{
flag = 0;
digitalWrite(LED4, LOW);
Alarm(); //buzzer sound
digitalWrite(LED1,HIGH); //turn the LED1 on only
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
while(digitalRead(button4)); //detect the button4,if pressed,out of the while loop
}
//If the button2 press the first
if(b2State == 0)
{
flag = 0;
digitalWrite(LED4, LOW);
Alarm();
digitalWrite(LED1,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,LOW);
while(digitalRead(button4));
}
//If the button3 press the first
if(b3State == 0)
{
flag = 0;
digitalWrite(LED4, LOW);
Alarm();
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,HIGH);
switchStateCorrect = digitalRead(buttonRight);
switchStateWrong = digitalRead(buttonRight);
while (digitalRead(buttonRight) == HIGH and digitalRead(buttonWrong) == HIGH){
if (digitalRead(buttonRight) == LOW){
AlarmCorrect();
Serial.println("Correct");
}
if (digitalRead(buttonWrong) == LOW){
AlarmWrong();
Serial.println("HENKRIK!");
}
}
while(digitalRead(button4));
switchStateCorrect = 0;
switchStateWrong = 0;
}
}
}
//buzzer sound
void Alarm()
{
for(int i=0;i<1;i++)
{
tone(buzzerPin,1000,2000);
}
}
void AlarmCorrect()
{
for(int i=0;i<1;i++)
{
tone(buzzerPin,2000,2000);
}
}
void AlarmWrong()
{
for(int i=0;i<1;i++)
{
tone(buzzerPin,500,2000);
}
}