int buttonState = 0;
int buttonPin = 8;
int LEDpin = 7;
int LEDon = 0;
double reaction = 0;
int press = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
press = 0;
pinMode(buttonPin,INPUT);
pinMode(LEDpin, OUTPUT);
buttonState = digitalRead(buttonPin);
digitalWrite(LEDpin, LOW);
LEDon = millis();
// Serial.println(millis());
// Serial.println("millis");
// Serial.println(buttonState);
//Serial.println("before while");
while(press<1){
buttonState = digitalRead(buttonPin);
// Serial.println("while");
if(buttonState==0){
// Serial.println("if");
reaction = millis() - LEDon;
// Serial.println("reaction");
// Serial.print(reaction);
Serial.print("Your reaction time was ");
Serial.print(reaction);
Serial.print(" milliseconds.");
Serial.println();
// Serial.println(buttonState);
digitalWrite(LEDpin, HIGH);
delay(5000);
press = 1;
}
// else if(buttonState>0){
// digitalWrite(LEDpin, LOW);
// Serial.println("Button is not pressed");
}
}
int buttonPin = 8;
int LEDpin = 7;
int LEDon = 0;
double reaction = 0;
int press = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
press = 0;
pinMode(buttonPin,INPUT);
pinMode(LEDpin, OUTPUT);
buttonState = digitalRead(buttonPin);
digitalWrite(LEDpin, LOW);
LEDon = millis();
// Serial.println(millis());
// Serial.println("millis");
// Serial.println(buttonState);
//Serial.println("before while");
while(press<1){
buttonState = digitalRead(buttonPin);
// Serial.println("while");
if(buttonState==0){
// Serial.println("if");
reaction = millis() - LEDon;
// Serial.println("reaction");
// Serial.print(reaction);
Serial.print("Your reaction time was ");
Serial.print(reaction);
Serial.print(" milliseconds.");
Serial.println();
// Serial.println(buttonState);
digitalWrite(LEDpin, HIGH);
delay(5000);
press = 1;
}
// else if(buttonState>0){
// digitalWrite(LEDpin, LOW);
// Serial.println("Button is not pressed");
}
}
Circuit (same as read button press - LED + button)
No comments:
Post a Comment