Two External LEDs Experiments

After completing the Two External LEDs project, test yourself by trying these experiments.

1 Modify the code to turn on one LED at a time. The net effect is that you'll see the two LEDs flash back and forth. Two External LEDs
2 Connect five external LEDs.

Modify the code to controll these five LEDs by turning all of them on for 1 second and then all of them off for another second.
Five External LEDs
3 Modify the code for the five LEDs so that only one LED is turned on at any one time. The five LEDs will turn on in sequence. Five External LEDs
4 Write a program to make the LEDs flash randomly.

To make the LEDs flash randomly, we need to use a random number to determine which LED is to turn on and off. Since our five LEDs are connected to pins 2 to 6, therefore, we want a random number between 2 and 6 inclusive.

The command random(2, 7) tells the computer to generate a random number between the two values specified where the lower number is included but the upper number is not included, in this case between 2 and 6 inclusive.
void setup() {
   pinMode(2, OUTPUT);
   pinMode(3, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(5, OUTPUT);
   pinMode(6, OUTPUT);

void loop() {
   int pinNumber = random(2, 7);   // generate a random number between 2 and 6	
   digitalWrite(pinNumber, HIGH);  // turn on a random LED
   digitalWrite(pinNumber, LOW);   // turn off the same LED
5 Write a function call TurnOnLed(int led_number) so that given a number from 1 to 5, the function will turn on that led number.

A function allows a programmer to create a modular piece of code that performs a specific task. The main routine (caller) calls this function to execute the task when needed, and when the function finishes, it then returns to the caller at the line after where the function was called.

The void keyword tells the computer that this function does not return a value.

The name given to this function is turnOnLed.

The parameter (int led_number) specifies that the function takes a one-digit integer value led_number. The function will use the number stored in led_number to determine which LED to turn on.

The body of the function is bracketed by the open and close { } braces.

The if command is used to check the value that is stored in the variable led_number. The double-equal sign == is used to test for equality. If led_number is equal to 1 then LED number 1 is turned on with the digitalWrite command.

Otherwise, if led_number is not equal to 1 then the else if part is executed to test if led_number is equal to 2. If it is equal to 2 then another digitalWrite command is used to turn on LED 2.

This else if pattern is repeated for the remaining three LEDs.

Notice the use of the open and close { } braces to bracket each of the body of the if command.
void TurnOnLed(int led_number) {
  // first turn off all the leds
  digitalWrite(led_1, LOW);
  digitalWrite(led_2, LOW);
  digitalWrite(led_3, LOW);
  digitalWrite(led_4, LOW);
  digitalWrite(led_5, LOW);
  // see what the led number is and turn on the appropriate led
  if (led_number == 1) {
    digitalWrite(led_1, HIGH);
  } else if (led_number == 2) {
    digitalWrite(led_2, HIGH);
  } else if (led_number == 3) {
    digitalWrite(led_3, HIGH);
  } else if (led_number == 4) {
    digitalWrite(led_4, HIGH);
  } else if (led_number == 5) {
    digitalWrite(led_5, HIGH);

void setup() {
  pinMode(led_1, OUTPUT);
  pinMode(led_2, OUTPUT);
  pinMode(led_3, OUTPUT);
  pinMode(led_4, OUTPUT);
  pinMode(led_5, OUTPUT);
  TurnOnLed(3);  // call the function to turn on led number 3
6 Repeat Experiment 3 but use the TurnOnLed() function that you created in Experiment 5.
// remember to include the function TurnOnLed() here

void loop() {
    TurnOnLed(1);  // call the function to turn on led number 1
    TurnOnLed(2);  // call the function to turn on led number 1
    TurnOnLed(3);  // call the function to turn on led number 1
    TurnOnLed(4);  // call the function to turn on led number 1
    TurnOnLed(5);  // call the function to turn on led number 1
7 Be creative and make some interesting light show. You may also want to add some music to your light show!