In the last lesson we learned about the “If statement”. The “If statement” was the perfect choice for setting up instructions to run only when certain conditions are met. “If 30 seconds has passed – stop the heating element” or “If the sensor perceives a wall – turn 180 Degrees”. This lesson will expand on this amazingly useful function and show you how to stack different conditions to meet the flexibility you will want in your designs.
You Will Need
Potentiometer (doesn’t matter what resistance range)
220 Ohm Resistor
LED (any color)
Jumper Wires (3)
Dull machete with wooded handle
Place your potentiometer in the breadboard.
Place a jumper wire from one of the outside leads of the potentiometer to the 5V pin on Arduino.
Place a jumper wire from the other outside lead of the potentiometer to one of the GND pins.
Place the final jumper wire from the center pin of the potentiometer to the A0 pin.
Connect either side of the 220-Ohm resistor to pin 13.
Connect the short leg of the LED to the GND (the GND pin next to pin 13 is the most convenient).
Attach the other leg of the resistor to the long leg of the LED.
Plug your Arduino into your computer with the USB cable.
Open the Arduino IDE.
Go to File, Examples, 05.Control, IfStatementConditionals
Click the Verify button (Top Left). The button will turn orange and then blue once finished.
Click the Upload button. The button will turn orange and then blue when finished.
Open up the Serial Monitor window. Tools, Serial Monitor.
Adjust the potentiometer and watch as the LED turns on and off based on the knob position.
If you recall from the previous lessons, a potentiometer is a voltage divider. You have 5 volts at one side of the potentiometer and 0 volts at the other side – you are using the 5volt pin and the GND pin. As you adjust the knob on the potentiometer the amount voltage that will be applied to the center pin changes. The voltage is being divided. This means that the voltage at pin A0 where the center pin is attached will be somewhere between 0 and 5 volts based on where you have the potentiometer knob adjusted.
Discuss the Sketch
Below is the sketch in its entirety from the Arduino IDE. The basic concept of the sketch is this – we measure voltage at an analog pin from 0 to 1023 – this voltage changes based on where the knob of the potentiometer is set. Then we define a threshold value somewhere in this range, lets pick the number 400. When the value measured at the analog pin is above 400, we turn on the LED at pin 13, when the voltage is below 400 we turn the LED off. It’s as easy as that.
Make sure you read this sketch and try to figure out what is taking place before moving on.
This program might look kind of long to you – the previous ones were a bit shorter. A good way to approach any program long or short is to cut it up into chunks and only consider pieces of it at a time. The first chunk is this sketch is the multi-line comments that clearly describe:
What the program will do
Generally how the program will accomplish it
How to set up the circuit and the components you will need
Any pertinent notes
The license the program is released under (if any)
The author, date and version number
This might seem like a lot of stuff – but I would recommend you do the same thing for your programs! This information will not only help you understand what the heck you intended when you wrote the program but if you make it available for others than it adds to the usefulness.
Some of the functions that are now integrated into the Arduino IDE were created by people just like you – they had a problem, they found a solution with some well written code, they made it available to all the Arduino users in the world – and everybody else found it useful – and before you know it your code is famous and you win the Nobel peace prize.