The Arduino sketch provided in your code is designed to continuously blink an LED connected to the board. The core functions used in this code are:
The code uses the LED built-in to most Arduino boards (referred to by LED_BUILTIN), which simplifies hardware connections. By modifying this code, you can customize the blink rate of the LED.
The delay() function in Arduino code causes the program to pause for a specified number of milliseconds. In the provided code, two delay() functions are used:
After turning the LED on using digitalWrite(LED_BUILTIN, HIGH);
, the code calls delay(1000);
. Here, "1000" represents 1000 milliseconds (or one second). This is the time period during which the LED remains on.
Following the command that turns the LED off (digitalWrite(LED_BUILTIN, LOW);
), there is another call to delay(1000);
. Similarly, this delay keeps the LED off for one second. The combination of these two delays results in a continuous cycle where the LED is on for one second, off for one second, producing a steady blink.
Depending on whether you want a faster or slower blink rate, you need to modify the values within the delay() functions:
To increase the blink speed, you need to shorten the duration for which the LED stays on and off. For example, if you change both delay() calls from 1000 milliseconds to 500 milliseconds, the LED will be on for half a second and off for half a second.
Here is how the modified code would look:
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for 500 milliseconds
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for 500 milliseconds
}
In this version, the LED cycles every 1 second (0.5 second on, 0.5 second off), which is noticeably faster than the original configuration.
Conversely, slowing down the blink rate involves increasing the delay values. For instance, setting the delay to 2000 milliseconds will keep the LED on for two seconds and off for two seconds.
Below is the modified code for a slower blink:
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for 2000 milliseconds, keeping the LED on longer
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(2000); // wait for 2000 milliseconds, keeping the LED off longer
}
This configuration creates a blinking pattern that lasts a total of four seconds per cycle, resulting in a slower blink rate.
While adjusting the delay values is the simplest way to change the blink speed, there are additional modifications you might consider if you want to create more advanced lighting patterns:
You are not restricted to having the same on and off durations. You can set different values for when the LED is on and when it is off. For example, you might want the LED to be on for a short period and remain off longer, or vice versa.
Consider the following code, which keeps the LED on for 300 milliseconds and off for 800 milliseconds:
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
delay(300); // wait for 300 milliseconds (short on period)
digitalWrite(LED_BUILTIN, LOW); // turn the LED off
delay(800); // wait for 800 milliseconds (longer off period)
}
This creates an irregular blink pattern that might be useful for certain signaling or notification applications.
Although using delay() is straightforward, it is a blocking function, meaning that no other processes can run while the delay is active. For more complex projects where you need multitasking or handling other inputs, you might consider using a non-blocking approach such as the BlinkWithoutDelay example provided by Arduino.
The non-blocking approach typically makes use of functions like millis()
, which keeps track of the number of milliseconds since the Arduino board began running the current program. This method allows you to perform tasks concurrently without the need for delay() to halt execution.
Here is a simplified version of how non-blocking code might look:
unsigned long previousMillis = 0;
const long interval = 500; // interval at which to blink (milliseconds)
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// if the LED is off turn it on, and vice versa:
int ledState = digitalRead(LED_BUILTIN); // read current state
digitalWrite(LED_BUILTIN, !ledState); // toggle the LED state
}
}
While the above code is a departure from using delay(), it allows for other operations to run concurrently, which is advantageous in complex projects.
The following table summarizes how different delay values affect the LED blink rate:
LED State | Delay Value (milliseconds) | Total Cycle Duration (milliseconds) | Description |
---|---|---|---|
On | 1000 | 2000 | Standard one second on, one second off blink |
Off | 1000 | ||
On | 500 | 1000 | Faster blink: half a second on and half a second off |
Off | 500 | ||
On | 2000 | 4000 | Slower blink: LED on and off for two seconds each |
Off | 2000 | ||
On | 300 | 1100 | Irregular pattern: quick on period with longer off period |
Off | 800 |
Beyond simply adjusting blink speed, you may want to experiment with other aspects of the LED behavior:
Although your current code uses LED_BUILTIN, you can choose to use another digital pin if your project requires multiple LEDs or different wiring configurations. To do this, change the pinMode() and digitalWrite() calls to reference your chosen pin number. For example, if you use pin 13 explicitly:
void setup() {
pinMode(13, OUTPUT); // use digital pin 13 instead of LED_BUILTIN
}
void loop() {
digitalWrite(13, HIGH);
delay(500); // adjust this value to customize blink rate
digitalWrite(13, LOW);
delay(500);
}
Make sure that the chosen pin is capable of digital output and that it is not being used by other parts of your project.
Creativity in programming often leads to more interactive and dynamic systems. For instance:
These approaches can provide more engaging user experiences and are useful in various applications ranging from simple household projects to more advanced interactive installations.