Hi all,
I am trying to make a reaction timer with a sw420 vibration sensor. however, I am stuck my programming.
Here is my code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int Sensor = A4;
int count = 0;
int val;
long measurement = pulseIn (Sensor, HIGH); //wait for the pin to get HIGH and returns measurement
const int buttonPin1 = A2;
const int ledPin2 = 7;
const int ledPin = 6;
const int buzzerPin = 10;
float reaction = 0;
float reaction2 = 0;
int timeDelay, timeNumber;
void setup() {
// put your setup code here, to run once:
pinMode(Sensor, INPUT);
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(buttonPin1, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(Sensor, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Boxing");
lcd.setCursor(0, 1);
lcd.print("Punch Reaction");
delay(2000);
lcd.clear();
delay(1000);
}
void loop() {
lcd.clear();
lcd.print("Please set the");
lcd.setCursor(0, 1);
lcd.print("Countdown");
delay(500);
lcd.clear();
while (digitalRead(buttonPin1) == HIGH) //while you not press START
{
if (digitalRead(buttonPin1) == LOW) //if start is pressed
{
analogWrite(10, 50);
delay(100);
analogWrite(10, 0);
delay(100);
analogWrite(10, 50);
delay(200);
analogWrite(10, 0);
lcd.print("Get ready");
delay(2000);
lcd.clear();
timeNumber = random(1, 5);//generates a number between 1 and 5
timeDelay = timeNumber * 1000;//times this number by 1000 to get the time in milliseconds
delay(timeDelay);//delay the program by this time period
lcd.print("Punch Me");
digitalWrite(ledPin, HIGH);//turn on the led
//tone(buzzerPin, 1000, 1);//trigger the buzzer
analogWrite(10, 50);
delay(500);
analogWrite(10, 0);
lcd.clear();
do {
delay(1);
reaction++;
long measurement = pulseIn (Sensor, HIGH);
Serial.println(measurement);
} while (measurement > 1000);
{
reaction = reaction / 1000;
digitalWrite(ledPin, LOW);
lcd.setCursor(0, 0);
lcd.print(reaction);
delay(3000);
lcd.clear();
}
}
}
}
- The button won’t react directly, I need to hold it for around 5 second to start the timer.
- the (measurement>1000) part seems to be not working, the timer stops right after the “punch me” part finishes.
Please help me… This is my first project with Arduino and I really can’t find what is wrong, been trying it for days.