Blinking LEDs with Arduino Using FOR Loop: A Beginner’s Guide

Blinking LEDs with Arduino: A Guide to Using For Loops.

A picture of electronic componenets with books and a text that says  blinking led with arduino


Get ready to create your own light show with Arduino! In this tutorial, you’ll learn how to use for loops to blink LEDs with your Arduino board. This project is perfect for beginners who want to get started with Arduino programming and create something truly amazing in the process. You’ll be amazed at what you can do with just a few lines of code. Let’s get started!

components to be used:

1.arduino uno
2.red led.
3.yellow led
4. resistor (330 ohms)
5.battery

circuit diagram of blinking led using for loop

circuit diagram of the blink led circuit with arduino


int yellowled = 6;

int redled= 9;

int redde=500;

int yellowde=100;

int i;

This code block is written in C++ and is used to control two LEDs (yellow and red) using an Arduino board. The first four lines of code define some variables that are used later in the code.

yellowled and redled are the pin numbers for the yellow and red LEDs, respectively.

redde and yellowde are the delay times for the red and yellow LEDs, respectively.

void setup() {

  // put your setup code here, to run once:

  Serial.begin(9600);

  pinMode(yellowled,OUTPUT);

  pinMode(redled,OUTPUT);

}

 The `setup()` function runs once when the board is powered up or reset and initializes some settings. The pinMode() function is used to set the mode of a pin on an Arduino board. In this code block, it is used to set the mode of yellowled and redled to OUTPUT, which means that they will be used to output voltage to the LEDs.

void loop() {

  // put your main code here, to run repeatedly:

 

  // This for loop will blink the red LED three times

  for(i=1;i<=3;i++){

    digitalWrite(redled,HIGH); // Turn on the red LED

    delay(redde); // Wait for redde milliseconds

    digitalWrite(redled,LOW); // Turn off the red LED

    delay(redde); // Wait for redde milliseconds

  }

 

  // This for loop will blink the yellow LED two times

  for(i=1;i<=2;i++){

    digitalWrite(yellowled,HIGH); // Turn on the yellow LED

    delay(yellowde); // Wait for yellowde milliseconds

    digitalWrite(yellowled,LOW); // Turn off the yellow LED

    delay(yellowde); // Wait for yellowde milliseconds

  }

 

  Serial.println(); // Print a blank line to the serial monitor

}


 The `loop()` function runs repeatedly as long as the board has power.

  for(i=1;i<=3;i++){

    digitalWrite(redled,HIGH); // Turn on the red LED

    delay(redde); // Wait for redde milliseconds

    digitalWrite(redled,LOW); // Turn off the red LED

    delay(redde); // Wait for redde milliseconds

  }

The first for loop will blink the red LED three times with a delay of `redde` milliseconds between each blink. 

for(i=1;i<=2;i++){

    digitalWrite(yellowled,HIGH); // Turn on the yellow LED

    delay(yellowde); // Wait for yellowde milliseconds

    digitalWrite(yellowled,LOW); // Turn off the yellow LED

    delay(yellowde); // Wait for yellowde milliseconds

  }

The second for loop will blink the yellow LED two times with a delay of `yellowde` milliseconds between each blink.

Serial.println(); // Print a blank line to the serial monitor

The last line of code prints a blank line to the serial monitor. You can use this line to debug your code by printing out values of variables or other information.


int yellowled = 6;
int redled= 9;
int redde=500;
int yellowde=100;
int i;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(yellowled,OUTPUT);
pinMode(redled,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
for(i=1;i<=3;i++){
digitalWrite(redled,HIGH);
delay(redled);
digitalWrite(redled,LOW);
delay(redled);
}

for(i=1;i<=2;i++){
digitalWrite(yellowled,HIGH);
delay(yellowled);
digitalWrite(yellowled,LOW);
delay(yellowled);
}
Serial.println();
}


No comments:

Featured

WHAT IS WEBSCRAPING IN PYTHON

  Web scraping is a technique that allows you to extract data from websites and store it in a format of your choice. It can be useful fo...

Powered by Blogger.