So I'm making a project that plays a tone when you press a button and stops when you hit the pressure sensor. After hitting the pressure sensor, a green or red LED will turn on depending on if your reaction time is "good" or "bad". I have set up the project and coded, but it's just beeping when I press the button and the noise is staying on/not doing anything when I hit the pressure sensor.
Here is my code:
const int buttonPin = 2;
int sensePin = 1;
int Buzzer1 = 9;
int ledRed = 8;
int ledGreen = 7;
int buttonState;
int pressure;
long initialTime;
long finalTime;
long measuredTime;
void setup() {
pinMode(Buzzer1, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(sensePin, INPUT);
}
void loop(){
buttonState = digitalRead(buttonPin);
pressure = analogRead(sensePin);
if (buttonState == HIGH) {
tone(Buzzer1,400);
if (pressure == 0) {
initialTime = millis();
}
if (pressure >= 25) {
digitalWrite(Buzzer1, LOW);
finalTime = millis();
}
}
measuredTime = (finalTime - initialTime);
if (measuredTime > 0) {
digitalWrite(ledGreen, HIGH);
}
if (measuredTime >= 400) {
digitalWrite(ledRed, HIGH);
}
}