Advertisement

Arduino LED blinking with variable speed

Not rated 11,890

#arduino #LED #blinking #variable speed #delay #microcontroller #programming #electronics #tutorial #beginner
Arduino LED blinking with variable speed
Arduino LED blinking with variable speed

Description: To achieve LED blinking at varying speeds, it is essential to modify the delay time for the LED's ON and OFF states. The fundamental approach involves changing the argument of the delay function based on time. Initially, the variable j is set to 1. A loop is implemented to check if the variable j is less than 20; if j reaches or exceeds 20, it resets to 1. The variable l is defined as the absolute value of (10 - j), which is crucial for designing both decreasing and increasing speeds within a single loop. The variable k is calculated as 1000 - (100 * l), allowing k to decrease until j equals 10 and then increase until j reaches 20. The LED is activated by calling the delay function with k as the argument. The code initializes three LED pins on an Arduino board, sets them as output pins, and controls their blinking pattern based on the calculated delays.

int ledPin1 = 3; // Initialize ledPin1 as digital pin 3 of Arduino board.
int ledPin2 = 4;
int ledPin3 = 5;

void setup() {
pinMode(ledPin1, OUTPUT); // Set ledPin1 (digital pin 3) as output pin
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}

int j = 1;

void loop() {
if (j >= 20) { // Check if j is 20 or greater
j = 1; // Reset j to 1 to restart the cycle
}
int l = abs(10 - j); // Calculate absolute value of (10 - j)
int k = 1000 - (100 * l); // Determine delay time based on l

digitalWrite(ledPin1, HIGH); // Turn on ledPin1
delay(k); // Delay for k milliseconds
digitalWrite(ledPin1, LOW); // Turn off ledPin1

digitalWrite(ledPin2, HIGH); // Turn on ledPin2
delay(k); // Delay for k milliseconds
digitalWrite(ledPin2, LOW); // Turn off ledPin2

digitalWrite(ledPin3, HIGH); // Turn on ledPin3
delay(k); // Delay for k milliseconds
digitalWrite(ledPin3, LOW); // Turn off ledPin3

delay(k); // Additional delay before next iteration
j = j + 1; // Increment the value of j
}

The circuit described utilizes an Arduino microcontroller to control three LEDs connected to digital pins 3, 4, and 5. The setup function initializes these pins as output, allowing for control over the LED states. The loop function implements a blinking pattern where the delay between turning the LEDs ON and OFF varies based on the value of j, which cycles from 1 to 20. The calculated delay, k, decreases as j approaches 10, resulting in faster blinking rates, and then increases as j approaches 20, creating a visually appealing effect of increasing and decreasing speeds. This approach can be further enhanced by incorporating additional features such as varying the number of LEDs or introducing different patterns of blinking for more complex visual effects.Now, to perform LED blinking in varying speed, we are sure, that we need to somehow change the delay time of an LED to remain in ON and OFF stat. So the main funda is to change the argument to function delay with change in time that`s it. Here, first we initialize the variable j=1. Then, we are going in the loop. And in loop we will check the c ondition for variable j to be less than 20. If, j is greater or equal to 20, then it will be initialize to 1. Now, we will define int l=abs(10-j). which will give the absolute value of (10-j). This is important thing if you want to design decreasing and increasing speed both by one loop. Then, we will write, int k=1000-(100*l). So, variable k will be in decreasing value function with time till j=10. Then, it will start increasing up to j=20. Now, write the statement to glow LED with calling the delay function with passing variable k as an argument. And the code is done int ledPin1=3; //Initialize ledpin1 = digital pin 3 of arduino board. int ledPin2=4; int ledPin3=5; void setup() { pinMode(ledPin1, OUTPUT); // Set ledpin1 (i. e. digital pin 3) as output pin pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); } int j=1; void loop() { if(j=20) // Check if j=20 { j=1; // if j=20, set j=1.

So that the circuit will start again } int l=abs(10-j); // Absolute value of (10-j) = l. int k=1000-(100*l); // Setting the delay function argument digitalWrite(ledPin1, HIGH); // ledpin1 is set to high i. e. ledpin1 is ON. delay(k); // Delay of k milliseconds digitalWrite(ledPin1, LOW); //ledpin1 is set to low i. e. ledpin1 is OFF. digitalWrite(ledPin2, HIGH); delay(k); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, HIGH); delay(k); digitalWrite(ledPin3, LOW); delay(k); j=j+1; // incrementing the value of j } That`s it buddy.

I hope you will do this little project. And make some innovations like firstdecreasingand thenincreasingfunctions, exactlyopposite to mine.

Related Circuits