Table of Contents
External Components
This stage involves the following new components.
More Blinkenlights!
One LED was fun, but how about five? Replicate the LED/resistor circuits on your breadboard 4 times, using a different pin on the Arduino for each LED.
Controlling direction
We're now going to use the push button to control the direction of the LEDs going through their sequence. When the button is not pressed the lights should go 0,1,2,3,4,0,1,2,3…, when it is pressed the lights should go 4,3,2,1,0,4,3,2…
Note: the buttons in your kit have bent legs. It's worth using a pair of pliers to straighten them out.
- Put one of the switches into the breadboard and wire to pin 7 as shown in this photo.
Below is the code for controlling LEDs with the push button. It stops the LEDs when the button is pressed.
// Button is on pin 7 int button = 7; int time = 100; // the setup routine runs once when you press reset: void setup() { // initialize the digital pins as outputs. for(int led = 9; led < 14; led++) { pinMode(led, OUTPUT); } // set the button pin as an input pinMode(button, INPUT); // enable the internal pull up resistor digitalWrite(button, HIGH); } // the loop routine runs over and over again forever: void loop() { int buttonReading = digitalRead(button); if(buttonReading == 1) { // If the button is not pressed // Do a sequence for(int led = 9; led < 14; led = led + 1) { digitalWrite(led, HIGH); delay(time); digitalWrite(led, LOW); } for(int led = 12; led > 9; led = led - 1) { digitalWrite(led, HIGH); delay(time); digitalWrite(led, LOW); } } }
Controlling Speed
We are now going to try to use the rotation sensor, otherwise known as a potentiometer or variable resistor, to control the speed of the LEDs. First, have a look at this video which explains the theory of voltage dividers.
Our rotation sensor is simple a voltage divider circuit in which the resistance of the two resistors change when you turn the knob. This means the voltage between the two resistors changes when you turn the knob. We are going to use the Arduino's analog inputs to measure this voltage accurately, then use the measurement to control the length of the delay we have in our loop, and hence the speed of the LED flashing sequence.
The Arduino's analog inputs are on the left side of the board and are labelled A0 through to A5. You can read the voltage on pin A0 as follows:
// The reading will be a number between 0 and 1023, where 0 is 0V, 1023 is 5V, and all the numbers in between are the voltages inbetween // For example a reading of 512 (which is right in the middle of the range) would be a voltage of 2.5V. int reading = analogRead(0);
Place the rotation sensor on the breadboard and wire it up to the Arduino as follows:
Remember using the serial communication in Lesson 1 to allow the Arduino to send data back to your laptop? We can use that to see how the value output by our rotation sensor varies as we rotate it.
void setup() { // initialize serial communication at a speed of 9600 bits per second: Serial.begin(9600); } void loop() { int reading = analogRead(0); Serial.print("Analog reading: "); Serial.println(reading); delay(500); // wait for half second }
Next Stage
Congratulations. Now you can go onto Stage 3!