Reaction Timer

Goal

Today’s goal is to create a simple reaction timer using the Arduino. You will learn how to use a button as an input, how to use while loops, how to declare and use variables and how to use the Serial Monitor.

Screen Shot 2017-09-02 at 11.01.40 AM

Circuits

For the reaction timer you will build 2 separate circuits: one circuit with a button and one circuit with an LED. Both circuits can be seen in the picture. You know how the LED circuit works be we are going to have a closer look at the button circuit.

The two left legs of the button are connected together internally and the two right legs of the button are connected together internally. This means that when the button isn’t pressed pin 3 is connect to GND through a 10K resistor. The resistor is important because it prevents a short-circuit when you press the button. When the button is pressed pin 3 gets connected to 5V and you can use the Arduino software to detect this.

Screen Shot 2017-09-03 at 10.52.19 AM

Programming

When declaring a variable you have to include 2 things: the type and the name of the variable, all of our variables are integers.

Serial.begin(9600) allows us to use the serial monitor to display messages on the computer screen. Serial.println(” …”) is used to actually print messages on the screen.

All the while statements are used to wait for something to happen with the button. The empty curly braces {} after the while statement tell the Arduino to do nothing as long as the condition in the while statement is true.

The millis() command gives the time since the Arduino was turned on. We record this time when the LED turns off and again when the button is pressed. The difference between those 2 times gives us the reaction time.

 

Advertisements

Your first Arduino program

Screen Shot 2017-08-23 at 8.07.50 AMPrograms in Arduino are know as “sketches”, to create a new sketch go to File –> New. You should now have a blank sketch that looks like the picture on the left. The first part of a sketch is setup. The setup code runs only once at the start, this is where you set up variables, ports, sensors, … The second part of a sketch is loop, the loop code runs continuously.

Screen Shot 2017-08-23 at 8.16.41 AM.pngOn the left you can see an example of a simple sketch, let’s look at all the code … Let’s start with the setup, in setup we are telling Arduino that we are going to use pin 10 as an output, that means that we can tell the Arduino to send information to pin 10. Whenever you want to use a port you have to tell Arduino which ports you want to use and if they will be inputs or outputs.

The first command in loop tells Arduino to turn on port 10 (digitalWrite), what this means is that the port will go from 0V to 5V (HIGH) and if you have an LED connected to this port it will light up. The next command (delay) tells the Arduino to do nothing for 1000 milliseconds or 1 second. Then we turn port 10 off, it will go back to 0V (LOW) and the LED will turn off as well. Lastly we have another wait command. After the wait command the program will go back to the start of loop and the program will start again. The result is an LED that will blink with a 1 second interval.

Things to try:

  • Change the delays and see what happens
  • Connect an LED to port 7 and change the code accordingly
  • Connect 2 LEDs to 2 different ports and make both blink at the same time
  • Create different blinking patterns for both LEDs

Some basic Electronics

 

You should have a basic idea of the different components in your bag and what they do. If you’re still looking for information use these websites:

Now you’re ready to start building your own circuits …

breadboard
Breadboard Connections

You need to know how a breadboard works. This image shows how all the holes in the breadboard are connected. Breadboards provide an easy way to build circuits without having to do any soldering.

 

circuit
Basic Circuit

This is an example of a basic electronic circuit. On the left you see the power source, the long line represents + and the short line represents -. If you would connect the + directly to the – you would create a short-circuit and could damage your power source. The resistor “resists” current flowing through the circuit and prevents a short-circuit. More resistance = less current.

In any circuit you need to have an uninterrupted path from + to – before any current can flow.

33631118582_99d191de3d_m
LED Circuit

 

Here is an example of a circuit with an LED. You can see a power source, an LED and a resistor in the circuit. You can also see that there is an uninterrupted path for the current to flow, the current will pass through the LED and through the resistor. You should be able to build this circuit using a breadboard and components.

 

Questions:

  • What will happen if you change the value of the resistor?
  • What will happen if you put the resistor in front of the LED?
  • What will happen if you turn around your LED (switch + and -)?
  • What will happen if you add a second LED after the first one?