LED Pattern Exercise
Introduction
During the LED Pattern exercise you will generate a repeating sequence on 3 LEDs, use a potentiometer to change the cycle rate of the sequence, and use a push button to pause the sequence.
GTA Marking
This is an assessed Exercise. When you have completed part 3, you should ensure that you have completed all the
Note
Before starting these exercises, you should ensure that you have added the LEDs, the button and the potentiometer to the breadboards on the robot chassis, as suggested on
The following video is a quick demonstration of the final outcome from this exercise:
Part 1: LED Sequence
During this part, you will write a program to create a repeating sequence of LEDs, with a fixed sequence time step, e.g. say every ½ second. A suggested LED sequence is shown in
| Sequence Index: | LED 3 (DIO 13) | LED 2 (DIO 12): | LED 1 (DIO 11): |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 |
| 2 | 0 | 1 | 0 |
| 3 | 0 | 1 | 1 |
| 4 | 1 | 0 | 0 |
| 5 | 1 | 0 | 1 |
| 6 | 1 | 1 | 0 |
| 7 | 1 | 1 | 1 |
We will not provide any example code to facilitate this exercise, you must create this code from scratch, possibly using some of the example code provided in the
Procedure:
- Ensure that the circuit is correctly connected:
- We suggest the anode of the LED be connected to Arduino pins DIO11 to 13.
- Each of the cathodes of the LEDs should be connected through a 470Ω resistor to ground.
- Write a program to change the pattern of the LEDs at a fixed rate, say every ½ second, and loop through the sequence continuously. We suggest using the sequence from
Table 1 . - If you do not use the sequence provided in
Table 1 , your sequence should have at least 8 patterns, some but not most may repeat. - For this we suggest using a delay function to control the timing of the sequence. The rationale for this is that we will replace the bracket value on the
delay()function with the ADC read value, to control the speed of the sequence. - Ensure that you save your code when you have completed this part.
Note
We strongly recommend using the suggested connection and layout provided in
Part 2: Potentiometer Control of the Sequence Rate
Info
It is suggested in the
This part of the exercise is split into two sub sections:
- Using the
POT.ino test program to read the potentiometer value and display it on the serial monitor. - Modifying the LED sequence code so that the potentiometer value is used to control the pattern change rate of the LED sequence.
Section 1: The Read the Potentiometer Test Program, POT.ino
The aim of the
Procedure:
- Copy the
POT.ino code into a black Arduino sketch and save it into your working folder. - Ensure the LEDs and potentiometer are connected into your circuit, as described in the
Building the Robot Document - Ensure the potentiometer is connected into the circuit.
- Connect left hand pin of the potentiometer to GND and the 5V and the right hand pin to +5V.
- It is suggested that the middle pin of the potentiometer is connected to the analogue A5 pin.
The analogue to digital converter, ADC, inputs to the Arduino have 10 bits of resolution (i.e. 1024 different values). By default, they measure from ground (0V) to 5 volts. That means that the 0-5V range of the potentiometer will be proportionally mapped to the 0-1023 digital values range in the microcontroller.
- Upload the program to the Arduino, then open the serial monitor and observe data being streamed to the terminal.
- Rotate the knob of the potentiometer and observe the range that the data spans.
- Observe the rightness of the LED as you rotate the potentiometer.
- Ensure that you save your code when you have completed this section.
This example program uses the map() function to ‘map’ the ADC measurement values between 0 and 1023, to a range of 0 to 255. This new range is required to write the PWM signal to change the LED brightness – PWM will be covered in the next lab session, so for this example, just assume this is an analogue output from the Arduino.
Look at the code and see how the map() command is used. The Arduino language reference for the map command can be found at:
Section 2: Integrate the Potentiometer Value Into Your LED Sequence Program
During this section you will integrate the reading of the potentiometer into the program for your LED sequence, and use the ADC value to control the delay function in the main loop.
Procedure:
- Starting with the code you wrote in
Part 1 , modify your code to read the voltage value of the potentiometer, using the ADC, each time the sequence value changes. - The ADC value should be used to control the
delay()function time within the sequence loop. - Ensure that you save your code when you have completed this section.
When you rotate the potentiometer to the right, the sequence should speed up, and when you rotate the potentiometer to the left, the sequence should slow down. There should be a continuous variation in sequence speed proportional to the position of the potentiometer between the left and right extremes.
Part 3: Addition of a Pause Button
During this part, you will implement the reading of the button on breadboard 1 and use the button value to pause the execution of the sequence.
Procedure:
- Starting from your code that you completed in
Part 2, section 2 , implement a button read from the button on breadboard 1. - Use this the pressed value of the button to hold the flow of execution until the button is released.
Exercise Assessment
What do we expect to see from the demonstration?
- The three LEDs should be flashing in a continuously repeating sequence of at least 8 different patterns.
- When you rotate the potentiometer to the right, the sequence should speed up, and when you rotate the potentiometer to the left, the sequence should slow down. There should be a continuous variation in sequence speed proportional to the position of the potentiometer between the left and right extremes.
- When the button is pressed, the sequence should pause, but not reset.
Now Get Your Work Marked by a GTA
Once you have completed your code and are satisfied with its operation, you should show your work to a GTA for marking.
Appendix: POT.INO Code
// Global Name Space
int potAnalogPin = 5; // FSR is connected to analog 0
int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int potReading; // the analog reading from the FSR resistor divider
int LEDbrightness;
void setup(void) {
// We'll send debugging information via the serial monitor
Serial.begin(9600);
pinMode(LEDpin, OUTPUT);
}
void loop(void) {
potReading = analogRead(potAnalogPin);
Serial.print("Analog reading = ");
Serial.println(potReading);
// we'll need to change the range from the analog reading (0-1023) down to
//the range used by analogWrite (0-255) with map!
LEDbrightness = map(potReading*1, 0, 1023, 0, 255);
// LED gets brighter the harder you press
analogWrite(LEDpin, LEDbrightness); // here you are using PWM !!!
delay(10);
}