Introduction to LEDs 2

LED 101 class

Previously, we looked at how to wire up your LED string to the Arduino and the power supply. Today, we’ll be completing a demo of the first sketch and seeing how computers do what they do.

Turing Machines: How a computer does what it does

Every computer that we know is simply a version of a Turing Machine – a machine that utilizes a table of rules in order to manipulate symbols on a strip of tape. Symbols used nowadays in modern computers are actually electrons, and the tape is a hard drive or memory stick. In the Arduino we’ll be working with, the memory stick is built right into the circuit board!

Every spot on a strip has an address, like houses on a street.  Each spot has room for one symbol. A Turing Machine might have a variety of different rules on its table – say, for instance, one rule might be to “read the symbol X and go to that address X”, or “change the symbol at address X to symbol Y”. In a sense, we are constantly recalling orders by using addresses as the placeholder and the symbol as the stuff we play with.

Every little bit of information used by a computer has to be stored somewhere on the strip – what’s important to note here that it doesn’t necessarily have to be permanent and can be temporary. Want to add two numbers together? We have to store that result somewhere. What if we want to add more than two numbers together? How about adding three? We’d add the first two, store that result somewhere, add the result to the third number, then store it again. We get to see that the only limitations are the length of the strip and the amount of time it takes the computer to process said strip.

Some addresses on the strip are special or different. They vary from computer to computer, but the basis remains the same that they are unique. Some addresses are manipulated outside of the computer: for instance, when you press an external button, a symbol on the strip will change. Some other addresses affect things externally: the Arduino can change a symbol on the strip that makes an LED light on and off.

Code

Let’s move on to coding. In the old days of coding, programmers needed to write symbols directly. Nowadays, we’re able to use an Integrated Developer Environment (IDE, hence Arduino IDE). It integrates four distinct things:

  1. A writing program
  2. A mistake checker
  3. A compiler that translates human symbols to machine symbols
  4. A system to upload machine symbols onto a strip

So, how’s this all play into our LEDs? WS2811 LEDs are lit by sending carefully timed pulses on the “data” wire of the LED string. Pulses are created by changing electric voltage in the wire, and yup, you guessed it – electric voltage can be changed by playing with a symbol at a special address.

Libraries and Open Source

Libraries are a neat stack of human symbols that have already figured out how to teach an important lesson to a machine – just like a human library works on people!

The Adafruit Neopixel library we’ll be installing soon teaches Arduinos how to make pulses to create colours on the LEDs. We’ll be needing that if we want to change any of the colours on the LEDs at all. The folder has been shared with everyone for free so that we can all learn and enjoy. That’s Open Source for you!

What you’ll need:

First, we’re going to rename the Adafruit file to “Adafruit_NeoPixel”. We’ll then run the Arduino IDE and choose Sketch > Include Library > Add .ZIP Library. Choose the Adafruit file, and we’ll have access to the file from our Arduino IDE from now onwards.

Never coded before?

For those just starting to code for the first time, I’ll be leaving lots of “spoiler” notes similar to this one with a more in-depth explanation. You can also head over to the Arduino site.

One thing to note here is that there’s some code initially left for us:

void setup() {
     // put your setup code here, to run once:
}

void loop() {
     // put your main code here, to run repeatedly:
}

Okay, that’s cool. We notice immediately that we seem to have some instructions or tips left here for us, immediately to the right of “//”. These are parts of the code … that actually isn’t code. This means that the computer or program will not read them as code and simply ignore them instead – these are comments made for the user! So, you, basically.

Throughout these tutorials, I’ll be leaving these comments and instructions that will tell you what exactly is going on in each distinct section of the code.

We’ve got two main parts to this initial code: “setup()” and “loop()”. Briefly, code under the setup section refers to code that runs once when the sketch is uploaded. Loop means the code is run an indefinite amount of times.

 

One White Dot

Alright. We’re going to try and get one white pixel at the start of the script. We’re going to use the following code to do so.

// This next line says "use the Adafruit_NeoPixel library".
#include <Adafruit_NeoPixel.h>
// Giving a name to a number makes it easier to talk about. "pi" is easier than 3.1415926...
// This tells the Arduino that every time it reads "PIN" it should think        
// "6".
#define PIN 6
#define STRING_LENGTH 64
// Make one strip please! It's 64 long, it's wired on pin PIN (6), and it 
//understands this 
//"language" of pulses.
Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRING_LENGTH, PIN, NEO_RGB + NEO_KHZ800);
// setup() must be in every Arduino program.
// Imagine this is where the symbol strip in the computer starts.
// It runs once when the Arduino turns on or reboots.

void setup() {
  strip.begin();
  strip.show();      // Initialize all pixels to 'off'
}

// After setup() the Arduino will run loop() over and over, forever.
void loop() {
  strip.setPixelColor( 0, strip.Color(255,255,255) );      // set the first (0) light to color white (strip.Color(255,255,255)).
  strip.show();                                            // display the new colors
  // wait a moment so the humans can see the colors. 
  delay(50); // 50ms = 1/20th of a second. 
}

Notice that I haven’t specified where this stack of code is supposed to go. I’ve incorporated the setup() and loop() into the code already, so we can go ahead and just copy paste over what Arduino has started us off with.

Also – you’ll find a list of all the new terminology and structure used in our code at the bottom of each and every tutorial where we code.

Let’s hit upload and see what happens.

Colors

Let’s change the symbols to affect the colour of our pixel. Note that Arduino adopts the American spelling in their code – this means that writing strip.Colour instead of strip.Color will crash the program.

strip.setPixelColor( 0, strip.Color(255,255,255) ); // set the first (0) light to color WHITE.

Here, the portion influencing the colour of our pixel lies within the brackets of color(). To change the colour, we simply change the numbers; these simply just references the RGB colouring scheme. Want green? Use (0, 255, 0). Red? (255, 0, 0). Here is a colour picking tool. Choose the colour that you want to use and copy paste the RGB code in.

Different pixels

Let’s take a look at the code from before one more time.

strip.setPixelColor(0, strip.Color(255,255,255) ); // set the first (0) light to color WHITE.

We just finished talking about the RGB portion, so let’s take a quick look at what’s before it. If we want to select which pixel gets coloured or changes coloured, we can change the destination location.

strip.setPixelColor(STRING_LENGTH-1, strip.Color(255,255,255) ); // set the last pixel as white

Okay, so today we’ve figured out how to make a particular pixel light up a certain colour. Our next tutorial will be about using loops and conditional statements to colour as many lights as you’d like with only a few lines of code. We’ll also take a look at animations.

Special thanks to Inez Gowsell for improving this post.

Definitions

 

#include <file_name> Tells Arduino to use a particular library. Make sure that the library is already stored in your Arduino IDE.
#define x y This sets a variable x as a certain integral value y.
delay(x) This introduces a delay of x milliseconds.
strip.setPixelColor(x, strip.Color(r,g,b)) This sets a pixel to a certain RGB colour at x location on the strip.