hello!
Im creating a reactiongame. Each player have their own button. What im strugling with is the function to read if the button is puched down at any given time and not only when it passes in the loop. Or is there a way to pause the program to wait for one of the buttons to be pressed before it continues? Is case an option?
many thanks!
const int ButtonPin1 = 3;
const int ButtonPin2 = 2;
const int BuzzPin = 5;
const int LedPin1 = 6;
const int LedPin2 = 4;
const int BuzzDuration = 20000;
const int ToneHz = 3000;
volatile int state = LOW;
void setup() {
pinMode(ButtonPin1, INPUT);
pinMode(ButtonPin2, INPUT);
pinMode(BuzzPin, OUTPUT);
pinMode(LedPin1, OUTPUT);
pinMode(LedPin2, OUTPUT);
//Test
digitalWrite(LedPin1, HIGH);
delay(200);
digitalWrite(LedPin1, LOW);
digitalWrite(LedPin2, HIGH);
delay(200);
digitalWrite(LedPin2, LOW);
digitalWrite(BuzzPin, HIGH);
delay(400);
digitalWrite(BuzzPin, LOW);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(ButtonPin1),TurnOff, CHANGE);
attachInterrupt(digitalPinToInterrupt(ButtonPin2),TurnOff, CHANGE);
}
int Seier = random(1,3);
void loop(){
int VinnerLedPin;
int VinnerButtonPin;
if (Seier == 1){
VinnerLedPin = LedPin1;
}
else {
VinnerLedPin = LedPin2;
}
if(Seier == 1){
VinnerButtonPin = ButtonPin1;
}
else {
VinnerButtonPin = ButtonPin2;
}
delay(random(1000,8000));
digitalWrite(BuzzPin, HIGH);
int Spiller1;
int Spiller2;
Spiller1 = digitalRead(ButtonPin1);
Spiller2 = digitalRead(ButtonPin2);
if(Spiller1 || Spiller2){
Seier = Spiller1? 1 : 2;
Serial.print(Seier);
digitalWrite(BuzzPin,LOW);
}
}
void TurnOff(){
state = !state;
digitalWrite(BuzzPin,state);
}
[code]