Programming a reaction timer with a buzzer and sensor

Hello,

I am very new to the world of programming, I've been researching and watching many videos and still a bit confused. My goal is to code a reaction time timer with a buzzer and sensor. In theory, you press the start button which will trigger a 5 second delay, then the buzzer will sound off, the timer will start and the sensor will turn on simultaneously. When the sensor senses movement in front of it, it will stop the timer and display the time.

Items using:
-Arduino Uno
-16x2 LCD Module Display
-Buzzer
-HC-SR0 Ranging Detector Ultrasonic Distance Sensor
-Push Button

So far I wrote out this code but I'm not sure if it's correct or if I'm going in the right direction, attached below.
I'm open to any tips or suggestions, thank you.

void setup() {
  // put your setup code here, to run once:


pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
  
Serial.begin(9600); // initialize the serial port:
  myservo.attach(SERVO_PWM_PIN); //set servo to Arduino's pin 9
}

void loop() {
  // put your main code here, to run repeatedly:

if (buttonpressed);                    //Press button 
    delay(5000);                       //Delay for 5sec
    lcd.begin(16, 2);                  //Display turn on and say Ready? :)
    lcd.print("Ready? :) :");
    
if (mode == 0)  {                      //Start timer
   currentTimerTime = millis () - startTimerTime;


   tone(buzzer, 1000);                 //Start buzzer after 5sec
   delay(5000); 
   noTone(buzzer);
   delay(20000)
   
delay(5000);
  myservo.write(angle + ANGLE_BOUNDS);
  
  // read distance from sensor and send to serial
  getDistanceAndSend2Serial(angle);
  
  // calculate angle 
  if (angle >= ANGLE_BOUNDS || angle <= -ANGLE_BOUNDS) {
    dir = -dir;
  }
  angle += (dir * ANGLE_STEP);  
}
 
int getDistanceAndSend2Serial(int angle) {
  int cm = sonar.ping_cm();
  Serial.print(angle, DEC);
  Serial.print(",");
  Serial.println(cm, DEC);
}

There going to beat you up in here for posting pix of the code.

Post your code as text. Use "code tags" and they look like </> above.

Good luck!

-jim lee

I cannot see any code of reading button state in your code.

kxhlai:
I'm not sure if it's correct

It can't be, since it won't compile: I don't see where buttonpressed is declared or set.
Even if it was, you don't want the ; after that if, since that prematurely ends the if and the lines that follow will always run.
An if should look like this:

if (condition) //no ; here
{
//these lines are conditional
}
//these lines  are not part of the if

Hi kxhlai,

except for the bitmapped code your first post is pretty good. Giving a description of what your code shall do and giving information about your knowledge-level. And you started to write code.

Your code is more of less structured in functions.
So the basic direction is "right". Though there is no digital "right" or "wrong" in coding a functionality. There are always mutliple ways for coding any functionalities.

Using an ultra-sonic sensor means there is a short delay between the object has reached a certain position and the sensor receives the signal. So depending on how accurate your application shall be you maybe need a different sensor-technology. Explain more on that aspect. In your description you wrote

When the sensor senses movement in front of it,

So what kind of movement? Does "movement" mean a physical object moves right towards the ultra-sonic sensor or sidewards?, any direction?

printing out on the serial interface means some additional delay especially at 9600 baud.
9600 is some old-fashioned standard-baudrate used in a lot of sketches.
Any kind of arduino is able to use the serial interface at 115200 baud which means the time for sending a byte reduces is 115200/9600 = 116 times faster.

Your making use of the function delay() several times. So far you can use delay().
If it comes to measure reaction-time you have to avoid the function delay() in the loop that does measure reaction-time.

There is a very easy way hot to post code.
inside the Arduino-IDE press Ctrl-T to autoformat indention of your code
then do a right-click with the mouse and choose copy for forum
paste clipboard content into a posting
done

best regards Stefan

Thank you for correcting me for the formatting, I was unsure if I attached the code only or copy paste, I hope it looks better now. The movement aspect I'm thinking is when a body part is passed through the sensor. I'm hoping that the sensor stops when it senses the body movement and stops the timer. My goal is to help measure reaction time for wrestling techniques accurately. My idea is that when the person is doing a wrestling technique, their body part that passes through the sensor range will be detected and stop the timer.

For the delay function, I want to be able to press the start button and there'll be a 5 second delay for a buzzer sound to go off, timer and sensor to start simultaneously.

Thank you for the feedback so far!

you should get rid of the delay(5000)
My tutorial on Multi-tasking in Arduino and How to write Timers and Delays in Arduino are applicable to your case
The times tutorial has small millisDelay class that simplifies running multiple timers/delays

As noted above Serial.print's can also 'delay' your loop. Using 115200 is a good start, but see my tutorial on
Arduino Serial I/O for the Real World for a complete solution if 115200 does not solve that problem

The Multi-tasking in Arduino has a simple loopTimer that will let you see how fast/slow your loop is running.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.