LedOnPin13.jpg This exercise is about sequential programming and good programming techniques for the I/O board.

  1. Wire your board so that pins 2-9 are outputs with LEDs attached to them.

  2. Each LED needs a 220 resistor.

  3. Place a pull-down resistor switch on pin 10.

Exercise1

  1. Write code to make the LEDs blink in a sequence, one by one using only digitalWrite(pinNum,HIGH/LOW) and delay(time).
    The lights should turn on and off after the button is pressed and stop when the button is pressed again.


  2. Write code to make the LEDs blink in a sequence, one by one using two for loops—One loop to turn on the lights and another to turn them off.
    This is the syntax for a for loop
    for(init;condition;next){
    
    }
    init is where you set a variable, like int led=2;
    condition is how long the loop will continue, like led<=9;
    next is where you increment or decrement the variable, like led++;



Arrays

©Chris Collins/CORBIS
© Chris Collins/CORBIS
An array is another type of variable that keeps track of multiple pieces of information at once.



Where a variable can only hold one piece of information, an array can hold a whole bunch of information and it holds them in a given order.



Basically, an array is like giving a single name to a whole list of variables, where each variable can be affected separately by the programmer without having to give it a separate name. Each separate variable within the array is referred to as a cell or element of the array.

Think of an array as a sequence of mailboxes in an apartment building. Each box may or may not have content within it. Each mailbox corresponds to an element of the array. Each mailbox is labeled with a number. In array speak, that number is called the index. The first index of a array in most programming languages is 0.

Remember: An array with five elements has the indices 0, 1, 2, 3, and 4.

myArray
indices
0 1 2 3 4


To access an array element at a given index, write:
myArray[index]


Given the array colors=["0xff000","0x00ff00","0x0000ff"];

What is the value of colors[1];

What is the value of colors[color.length-1];


To assign a value to an array element at a given index, write:
myArray[index]=value;


Used in this way the square brackets are pronounced “sub”. myArray[1] is pronounced myArray sub 1.

If you wanted to assign the value 8 to every element in an array, use a loop:
int arrayLen=8;
for(i=0;i<arrayLen;i++){
    myArray[i]=8;
}

Using a loop to access the values of an array is called looping over or looping through the array.




Exercise 2—Back to Arduino

Rewrite the last program using an array:
int pinArray[] = {2, 3, 4, 5, 6, 7, 8, 9};
int timer = 100;

Look at your solution to the bike light (you'll have to remove the interval code and add a timer variable)

While this works with the delay, it's not great, you'll have to hold the button down until the delay is complete and the chip can poll the button again.

Exercise 3—Just 1 at a time

By lighting the next led shortly after the current led is turned off, creates a different effect.
Using the last program modify the loops(). Here is the modification for the first loop:
   digitalWrite(pinArray[your_var], HIGH);
   delay(timer);
   
   digitalWrite(pinArray[your_var], LOW);
   delay(timer*2);


Exercise 4—Shooting Star

Connect 11 LEDs to 11 arduino digital pins, through a 220 Ohm resistance.

The program should light up LEDs until it reaches the number of LEDs equal to the size you have established for the tail. Then it will continue lighting LEDs towards the left to make the line keep moving, and will start erasing from the right, to make sure you see the tail (otherwise you would just light up the whole line of leds, this will happen also if the tail size is equal or bigger than 11).

The tail size should be relatively small in comparison with the number of LEDs in order to see it.

Copy, paste and complete the following:
// Variable declaration
  int pinArray [] = ___________; // Array of LED pins
  int timer = 100;    // delay
  int tailLength = 4; // Num of LEDs that stay lit before you start turning them off
  int lineSize = 11;  // Number of LEDs connected 
 
  void setup(){
  //turn all your leds to OUTPUTS
   pinMode(pinArray[0],OUTPUT);
   ______________________
   ______________________
   ______________________
   ______________________
   ______________________
   ...
  }
  // Main loop 
  void loop(){
    
    int tailCounter = tailLength;   // set up the tail length in a counter
    for (int i=0; i<length_of_array; i++){
    // light up  the LEDs consecutively
     __________________________ 
   // Add a delay. This time variable controls how fast you light them up
    ______________           
     if (tailCounter == 0){
     
     // turn off the LEDs depending on  tailLength
        digitalWrite(pinArray[i-tailLength],___); 
        
      }else if (tailCounter > 0)
         tailCounter--;
   }
    for (int i=0; i<length_of_array; i++){
    
    // turn off the LEDs
     digitalWrite(________);
     //pause
     _____________ 
   }
}

Combine the code above with your toggle switch.