Exercise 1 - Digital LED strip 'for' loop

The Circuit

  • For additional information there is a very good Adafruit guide to all things digital LED strip related here
  • Refer to this link about installing additional libraries and install the FastLED and Adafruit Neopixel libraries. You should now have some extra examples in your Arduino 'File > Examples' menu. Try some of them out, you may need to change the code a little to say what pin you are using to control the LEDs, or how many LEDs are in your strip. 
  • Paste the following code into the Arduino IDE and follow the instructions:

/*  The code below shows you how to set individual pixels with a colour on digital LED strip using the 
 *  Adafruit Neopixel library (though you could use the fastLED library instead!)
 *  Upload it and give it a go!
 *  Look at the Arduino site reference page and the description of
 *  the 'for' statement under the 'Structure' section. Use this control
 *  structure to light up each LED on your strip in sequence one colour, 
 *  and then another. 
 *  https://www.arduino.cc/en/Reference/HomePage
 *  A little clue for you:
The 'for' example from the reference page can be broken down as follows: 
  for (int i=0; i <= 255; i++){
for...{ - stop the code and do this 'for' loop until it has finished
int i=0 - set an integer (whole number) variable called 'i' to 0,
i <= 255 - while the 'i' variable is less than or equal to 255... 
i++ - increment the 'i' variable by one on each loop of the 'for' statement"
    analogWrite(PWMpin, i);
- set the output value of a pin to the one contained in the 'i' variable
- wait 10 milliseconds, close the 'for' loop and carry on with the other code
 * Luke Woodbury 31 March 2016

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>

// Which pin on the Arduino is connected to the NeoPixels?
#define PIN            6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      60

// When we setup the NeoPixel library, we tell it how many pixels (LEDs) we have on our strip, 
//and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

const int wait = 500; //constant to hold delay time in milliseconds
int pixel = 0; //variable to hold current pixel number

void setup() {
  pixels.begin(); // This initializes the NeoPixel library...
  pixels.show(); // and clears the LEDs

void loop() {

    pixel = 0;  //sets our 'pixel' variable to the first pixel, on a 60 LED strip they are numbered 0 - 59
    pixels.setPixelColor(pixel, pixels.Color(200,50,0));  //set the pixel number with orange
    pixels.show(); //send the updated pixel color to the strip
    delay(wait);  //use our 'wait' constant as the delay time

    pixels.setPixelColor(pixel, pixels.Color(50,100,200));  //set the pixel number with blue
    pixels.show(); //send the updated pixel color to the strip
    delay(wait);  //use our 'wait' constant as the delay time


Extension exercises

  • try using a button to make the LED strip go one colour when it is pressed and another when it is released
  • try using a pot or an LDR to make pixels light up depending on the incoming value
  • how would you reverse the direction of the LEDs lighting up?  

Click on a link

Exercise 2 - Servo motor and the 'if' statement

Exercise 3 - 7 segment display and the 'switch case' statement