#include <Time.h>
int buttonA = 2;
int buttonB = 3;
int buttonC = 4;
int buttonD = 5;
int buzzerA = 6;
int input=0;
int randNumber=0;
int iTime=0;
int lTime=0;
int score=0;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the button's pin an input:
pinMode(buttonA, INPUT);
pinMode(buttonC, INPUT);
pinMode(buttonD, INPUT);
// make the buzzer's pin an input:
pinMode(buzzerA, OUTPUT);
//seed random by using noise voltage from and pluged analogue pin
randomSeed(analogRead(0));
//wait for 5 seconds
delay(5000);
}
// the loop routine runs over and over again forever:
void loop() {
for(;;){
// print a random number from 1 to 4
randNumber = random(1, 1);
if(randNumber==1){
//produce sound
tone(buzzerA, 100, 200);
delay(1000);
iTime=now();
while(digitalRead(buttonA) == LOW || digitalRead(buttonB) == LOW || digitalRead(buttonC) == LOW || digitalRead(buttonD) == LOW){};
Serial.print(score); //for testing purposes
if(digitalRead(buttonA) == HIGH){
if( now()-iTime <6 ){
score+=6-iTime;
}else break;
}else break;
}
}
Serial.print(score);
}
Goal:
buzzer buzzes
Wait for button press
Calculate how long it took to press button since the buzz
According to duration add to score or exit loop and display score
Problem:
The program never goes beyond the while loop. I know the button is working for sure because LED.
Note:
May find things with seemingly no use such as ButtonB,C and D. Will need to use them later so please just ignore those.
Since your already here. Using tone functions makes a lot softer noise then if I simply provide 5 volt to buzzer. Anyways to make it sound just as loud while with different tones?
Thank You