MAKE THE LED BLINK FASTER

Copy and paste the following example code into your Arduino window, replacing all of the code in your window with the code below:

/*
 Blink
 Turns on an LED for one second, then off for one second, repeatedly.
 
 This example code is in the public domain.
*/

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;    // set the variable “led” to the value 13

// the setup routine runs once when you press reset:
void setup() {
    // initialize the digital pin as an output.
    pinMode(led, OUTPUT);
}

void loop() {
    digitalWrite(led, HIGH);  // turn the LED on
    delay(1000);              // wait for a second
    digitalWrite(led, LOW);   // turn the LED off
    delay(1000);              // wait for a second
}

Click on the check mark button in the Toolbar to compile the code. A “Done compiling” message should appear in the Status Bar. If you receive an error message instead, make sure your code matches the code above exactly. Continue to edit and compile until you receive a “Done compiling” message.

Take a closer look at the example code. The four lines of code between the curly brackets highlighted below are the heart of the Blink program. When the program runs, these lines execute one at a time in the order that they’re written.

• The first line, digitalWrite(led, HIGH); is the code that turns the LilyPad’s LED on.
• The second line, delay(1000);, tells the LilyPad to do nothing for one second (1000 milliseconds).
• The third line, digitalWrite(led, LOW);, is the code that turns the LED off.
• The fourth line, delay(1000);, tells the LilyPad to do nothing for another second.

void loop() {
    digitalWrite(led, HIGH);  // turn the LED on
    delay(1000);              // wait for a second
    digitalWrite(led, LOW);   // turn the LED off
    delay(1000);              // wait for a second
}

Notice how comments at the end of each line (in grey) describe what the code is doing. Your comments may look slightly different than the ones shown here. Don’t worry, your program will still work the same way the example does.

void loop() {
    digitalWrite(led, HIGH);  // turn the LED on
    delay(500);               // wait for half a second
    digitalWrite(led, LOW);   // turn the LED off
    delay(500);               // wait for half a second
}

See if you can change this code to get the LED on the LilyPad to blink faster. To get your LED to blink twice as fast, change each delay(1000); line to delay(500);. Make the changes, compile your code, and upload the new code to your LilyPad.

Experiment with different blinking speeds. What happens when you try a very short delay? Note: the shortest delay time you can use is 1. Decimal numbers will not work here. Upload the code to your LilyPad for each new delay value.

Once you’ve gotten your LED to blink quickly, see if you can get it to blink very slowly. Next, try getting it to blink in an uneven pattern, like a heartbeat.

 

SAVE YOUR CODE

Once you’ve created a blinking behavior that you like, click on the downward pointing arrow in the Toolbar to save your code. When you scroll over this button you’ll see the word “Save”. Click “OK” on the popup window that appears. Choose a good name for your file and click on the “Save” button to complete the process.

Save

To make sure that the file saved properly, click on the upward pointing arrow in the Toolbar. This is the “Open” icon in Arduino; when you scroll over it you’ll see the word “Open”.

Open

The file you just saved should be at the top of the popup menu that appears. All of the programs that you save in Arduino will show up in this menu. This allows you to find and open them easily.

<< PREVIOUS      NEXT >>