# Many LEDs

In this lab, you’ll play with 8 LEDs in a row. Each LED can represent a “bit” and together form an 8 bit byte. You can use this array to show levels, for example, to show how loud an amplifier is, or to indicate the temperature.

1. Start with the following circuit. Make sure to orient the LED correctly – the flat side should be on top as in the diagram below. Use resistors close in value to 330Ω, as shown below. Connect the LED legs to pins 2 through 9 respectively: Fritzing for LEDs on pins 2 to 9 | full size » Fritzing for LEDs on pins 2 to 9 | full size »

The LEDs can be arranged in any color combination you choose. For example, you could emulate a signal meter that is green until you get to a certain level, and then turns red.

2. Write code to turn all the LEDs on, then turn them all off.

``````// Blink 8 LEDS

int Pin0 = 2;   // led0 is connected to digital pin 2
int Pin1 = 3;   // led1 is connected to digital pin 3
int Pin2 = 4;   // led2 is connected to digital pin 4
int Pin3 = 5;   // led3 is connected to digital pin 5
int Pin4 = 6;   // led4 is connected to digital pin 6
int Pin5 = 7;   // led5 is connected to digital pin 7
int Pin6 = 8;   // led6 is connected to digital pin 8
int Pin7 = 9;   // led7 is connected to digital pin 9

void setup()
{
pinMode (Pin0, OUTPUT);   // sets 0 pin as output
pinMode (Pin1, OUTPUT);   // sets 1 pin as output
pinMode (Pin2, OUTPUT);   // sets 2 pin as output
pinMode (Pin3, OUTPUT);   // sets 3 pin as output
pinMode (Pin4, OUTPUT);   // sets 4 pin as output
pinMode (Pin5, OUTPUT);   // sets 5 pin as output
pinMode (Pin6, OUTPUT);   // sets 6 pin as output
pinMode (Pin7, OUTPUT);   // sets 7 pin as output
}

void loop()                         //repeats the following steps
{
digitalWrite (Pin0, HIGH);   //turn the 0led on
digitalWrite (Pin1, HIGH);   //turn the 1led on
digitalWrite (Pin2, HIGH);   //turn the 2led on
digitalWrite (Pin3, HIGH);   //turn the 3led on
digitalWrite (Pin4, HIGH);   //turn the 4led on
digitalWrite (Pin5, HIGH);   //turn the 5led on
digitalWrite (Pin6, HIGH);   //turn the 6led on
digitalWrite (Pin7, HIGH);   //turn the 7led on

delay (500);                 //keep the leds this way for 0.5 seconds

digitalWrite (Pin0, LOW);    //turn the 0led off
digitalWrite (Pin1, LOW);    //turn the 1led off
digitalWrite (Pin2, LOW);    //turn the 2led off
digitalWrite (Pin3, LOW);    //turn the 3led off
digitalWrite (Pin4, LOW);    //turn the 4led off
digitalWrite (Pin5, LOW);    //turn the 5led off
digitalWrite (Pin6, LOW);    //turn the 6led off
digitalWrite (Pin7, LOW);    //turn the 7led off

delay (500);                 //keep the leds this way for 0.5 seconds
}
``````
3. Now turn them on one at a time from one end to the other, and then off one at a time from one end to the other.

4. Using this code you can create a “Knight Rider” like chase from one end to the other.

``````/* Knight Rider 1
* --------------
*
* Basically an extension of Blink_LED.
*
*
* (cleft) 2005 K3, Malmo University
* @author: David Cuartielles
* @hardware: David Cuartielles, Aaron Hallborg
*/

int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pin6 = 6;
int pin7 = 7;
int pin8 = 8;
int pin9 = 9;
int timer = 100;

void setup(){
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);
pinMode(pin6, OUTPUT);
pinMode(pin7, OUTPUT);
pinMode(pin8, OUTPUT);
pinMode(pin9, OUTPUT);
}

void loop() {
//first set
digitalWrite(pin2, HIGH);
delay(timer);
digitalWrite(pin2, LOW);
delay(timer);

digitalWrite(pin3, HIGH);
delay(timer);
digitalWrite(pin3, LOW);
delay(timer);

digitalWrite(pin4, HIGH);
delay(timer);
digitalWrite(pin4, LOW);
delay(timer);

digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin5, LOW);
delay(timer);

digitalWrite(pin6, HIGH);
delay(timer);
digitalWrite(pin6, LOW);
delay(timer);

digitalWrite(pin7, HIGH);
delay(timer);
digitalWrite(pin7, LOW);
delay(timer);

// second set
digitalWrite(pin8, HIGH);
delay(timer);
digitalWrite(pin8, LOW);
delay(timer);

digitalWrite(pin9, HIGH);
delay(timer);
digitalWrite(pin9, LOW);
delay(timer);

digitalWrite(pin8, HIGH);
delay(timer);
digitalWrite(pin8, LOW);
delay(timer);

digitalWrite(pin7, HIGH);
delay(timer);
digitalWrite(pin7, LOW);
delay(timer);

digitalWrite(pin6, HIGH);
delay(timer);
digitalWrite(pin6, LOW);
delay(timer);

digitalWrite(pin5, HIGH);
delay(timer);
digitalWrite(pin5, LOW);
delay(timer);

digitalWrite(pin4, HIGH);
delay(timer);
digitalWrite(pin4, LOW);
delay(timer);

digitalWrite(pin3, HIGH);
delay(timer);
digitalWrite(pin3, LOW);
delay(timer);
}
``````
5. But as you can see, it does not scale well… If you doubled the LEDs you’d double the size of your code, and modifications are tedious. Instead create an array with containing 8 bits. You can think of an array as a set of drawers (Indexes) numbered 0 through 7. You can then store a 1 or a 0 in each of the indicies… and when you “draw” the array to the set of LEDs they turn on when the index = high (1) or low (0).

``````/* Knight Rider 2
* --------------
*
* Reducing the amount of code using for(;;).
*
*
* (cleft) 2005 K3, Malmo University
* @author: David Cuartielles
* @hardware: David Cuartielles, Aaron Hallborg
*/

int pinArray[] = {2, 3, 4, 5, 6, 7, 8, 9};
int count = 0;
int timer = 100;

void setup(){
// we make all the declarations at once
for (count=0;count<8;count++) {
pinMode(pinArray[count], OUTPUT);
}
}

void loop() {
for (count=0;count<8;count++) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer);
}
for (count=7;count>=0;count--) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer);
}
}
``````
6. Use `analogRead` on pins `A0` to set the delay values. Note that `analogRead` returns a number between `0` and `1023`.

``````/* Knight Rider 2
* --------------
*
* Reducing the amount of code using for(;;).
*
*
* (cleft) 2005 K3, Malmo University
* @author: David Cuartielles
* @hardware: David Cuartielles, Aaron Hallborg
*/

int pinArray[] = {2, 3, 4, 5, 6, 7, 8, 9};
int count = 0;
int timer = 10;
int levelInput = A0;

void setup(){
// we make all the declarations at once
for (count=0;count<8;count++) {
pinMode(pinArray[count], OUTPUT);
}
// Serial.begin(9600); // use this to activate the Serial Monitor
}

void loop() {
//Serial.println(levelValue);  // Use this to Debug the pot as needed
for (count=0;count<=7;count++) {
digitalWrite(pinArray[count], HIGH);
}
delay(timer);
for (count=0;count<=7;count++) {
digitalWrite(pinArray[count], LOW);
}
delay(timer);
}
``````
7. Use `analogRead` on pins `A0`, `A1`, respectively, to set the values. Note that `analogRead` returns a number between `0` and `1023`. Divide them by 128 to get values from 0 to 7 from each pin.

8. Use the potentiometer to make a level meter… lighting more LEDs as you rotate the potentiometer.

``````/* Knight Rider 2
* --------------
*
* Reducing the amount of code using for(;;).
*
*
* (cleft) 2005 K3, Malmo University
* @author: David Cuartielles
* @hardware: David Cuartielles, Aaron Hallborg
*/

int pinArray[] = {2, 3, 4, 5, 6, 7, 8, 9};
int count = 0;
int timer = 10;
int levelInput = A0;

void setup(){
// we make all the declarations at once
for (count=0;count<8;count++) {
pinMode(pinArray[count], OUTPUT);
}
// Serial.begin(9600); // use this to activate the Serial Monitor
}

void loop() {
//Serial.println(levelValue);  // Use this to Debug the pot as needed
for (count=0;count<=levelValue;count++) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
}
delay(timer);
for (count=levelValue;count<8;count++) {
digitalWrite(pinArray[count], LOW);
delay(timer);
}
}
``````
9. Modify your code to use the `map` function to convert the input value to the output value, instead of dividing by 128.

## Challenges:

1. Experiment with turning potentiometers to create dark margins on the left and right that get narrower and wider as you turn the pots.
2. Use `random()` or another algorithm to slowly move a single “pixel?”
3. Connect a button to a digital input pin.
• Instead of moving LEDs with the potentiometers, increase the count whenever the button is pressed.
4. Look at this version of the code… why do you think they wrote it this way?

``````/* Knight Rider 3
* --------------
*
* This example concentrates on making the visuals fluid.
*
*
* (cleft) 2005 K3, Malmo University
* @author: David Cuartielles
* @hardware: David Cuartielles, Aaron Hallborg
*/

int pinArray[] = {2, 3, 4, 5, 6, 7, 8, 9};
int count = 0;
int timer = 30;

void setup(){
for (count=0;count<8;count++) {
pinMode(pinArray[count], OUTPUT);
}
}

void loop() {
for (count=0;count<7;count++) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count + 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer*2);
}
for (count=7;count>0;count--) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count - 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer*2);
}
}
``````