Skip to content

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 assessment requirements and show your work to a GTA to get marked.

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 Building the Robot: Fig. 6. Furthermore, you should ensure that your connections for these components are correct - suggested connections are shown in Building the Robot: Building the Robot: Table. 1.

The following video is a quick demonstration of the final outcome from this exercise:

Video demonstrating the expected 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 Table. 1.

Sequence Index: LED 3 (DIO 13) LED 2 (DIO 12): LED 1 (DIO 11):
0000
1001
2010
3011
4100
5101
6110
7111
Suggested simple repeating sequence, (counting-up in binary).

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 Introductory Exercises or the built-in examples as inspiration.

Procedure:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. Ensure that you save your code when you have completed this part.

Note

We strongly recommend using the suggested connection and layout provided in Building the Robot Document because it will link conveniently with the discussions and the example code provided. Using the suggested layout and connections also makes debugging your work easier because you will have a more familiar structure for the staff and GTAs.

Part 2: Potentiometer Control of the Sequence Rate

Info

It is suggested in the Building the Robot Document that your potentiometer is connected to analogue pin A5.

This part of the exercise is split into two sub sections:

  1. Using the POT.ino test program to read the potentiometer value and display it on the serial monitor.
  2. 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 POT.ino example code is to show you how to read an analogue value from a sensor, or other device, and use its value. In this example code you will read the voltage from a potentiometer, generate a PWM output to change the brightness of an LED, and stream some data to the Serial Monitor.

Procedure:

  1. Copy the POT.ino code into a black Arduino sketch and save it into your working folder.
  2. Ensure the LEDs and potentiometer are connected into your circuit, as described in the Building the Robot Document
  3. 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.

  1. Upload the program to the Arduino, then open the serial monitor and observe data being streamed to the terminal.
  2. Rotate the knob of the potentiometer and observe the range that the data spans.
  3. Observe the rightness of the LED as you rotate the potentiometer.
  4. 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:

https://www.arduino.cc/reference/en/language/functions/math/map/

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:

  1. 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.
  2. The ADC value should be used to control the delay() function time within the sequence loop.
  3. 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:

  1. Starting from your code that you completed in Part 2, section 2, implement a button read from the button on breadboard 1.
  2. 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

POT.ino Arduino 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);
}