Skip to main content

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();
}


Comments

Popular posts from this blog

Thevenin’s Theorem: A Beginner’s Guide

                                        table of content  Introduction History of Thevenin’s theorem Basic circuit analysis concepts Voltage, current, and resistance Ohm’s law Thevenin’s theorem: principles and applications Statement of the theorem Finding the Thevenin equivalent circuit What is Thevenin’s theorem? When should you use Thevenin’s theorem? How do you apply Thevenin’s theorem to a circuit Conclusion . Introduction Electrical engineers can effectively convert complicated circuits into smaller equivalent circuits by using Thevenin's theorem. The French telegraph engineer Léon Charles Thévenin is honored by having his theorem called in his honor. He proposed it in 1883. History of Thevenin’s theorem Hermann von Helmholtz, a German scientist, independently derived Thevenin's theorem in 1853; Léon Charles Thévenin did ...

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 for various purposes, such as market research, price comparison, content analysis, and more. In this blog post, i will show you everything a beginner needs to know about web scraping, from the basics to some advanced tips and tricks.   What is web scraping?   Web scraping is the process of programmatically retrieving information from web pages. It involves sending requests to web servers, parsing the html code of the web pages, and extracting the data you want. Web scraping can be done manually, by copying and pasting data from a website, or automatically, by using a software tool or a programming language.   Why web scrape?   Web scraping can help you access data that is not available through an api or a downloadable file. For example, you may want to scrape product reviews from an e-commerce website, or news ...

Study Linear Regression: A Beginner’s Guide with c++

Table of content: • Introduction • What is Linear Regression? • Simple vs. Multiple Linear Regression • Implementing Linear Regression in C++ • Complex Linear Regression • Conclusion • References Understanding Linear Regression: A Beginner’s Guide Linear regression is a powerful statistical tool that allows us to understand the relationship between two or more variables. It is widely used in many fields, including finance, economics, and engineering, to make predictions and analyze data. In this article, we will explore the basics of linear regression, including what it is, how it works, and how to implement it in C++. We will also discuss the differences between simple and multiple linear regression and provide examples to help you understand these concepts. What is Linear Regression? At its core, linear regression is a method for finding the line of best fit that describes the relationship between two continuous variables. This line can be used to make predictions about o...