Arduino Projects: Asynchronous LEDs blink

Making Arduino perform asynchronous tasks is not an easy job and such Project requires you to practice implementation of clean and optimized programs. It is very useful to know how to program AVR in C and how to read the datasheets in order to implement timers.

This project will shed some light on the how to build an asynchronous set of blinking LEDs using the Arduino Uno board.

A while ago, one of my friends built a pinball machine from scratch and he asked that I help him create a program with the Arduino that will make LEDs blinks completely asynchronously. And this was the perfect project for his pinball machine. Hope you will the project useful. Let’s dive into it.

Blinking LEDs with the Arduino board

Here is the list of the parts we will need for this project.  You can use less than 6 LEDs if you need to.

This project uses the most basic hardware component one can imagine while working with the Arduino. The most important part of this project is the program, not the hardware part. Use the image below to connect the parts together.

Remember that the longer leg of the LED goes to the positive pin and the shorter goes to the negative one, so you have to plug the longer leg to the resistors, and shorter to the negative bar on the breadboard.

Now we can upload a program to the Arduino, I made a six LED version and this number is supported by this program (you can also use less without changing the code, or more but you will have to add few lines of code).

The Code

//here we can declare time of each blink
int led_1_blink_time = 200;
int led_2_blink_time = 500;
int led_3_blink_time = 1100;
int led_4_blink_time = 100;
int led_5_blink_time = 1630;
int led_6_blink_time = 333;

//we need this variable to store last time of blink in milliseconds
long led_1_last_blink;
long led_2_last_blink;
long led_3_last_blink;
long led_4_last_blink;
long led_5_last_blink;
long led_6_last_blink;

void setup() {
//declaring ports as outputs
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);

}

void loop() {

//here is if that switch state of each LED. All the next ifs are
//the same just varables and pins are different
//as you can see the first thing that we have to do is check
//if millis() - last blink is bigger than blink time
//if so we are changing the state of the pin
//if it's HIGH we set it to LOW and if it is LOW we set it to HIGH
//the last thing is to save current millis to last blink variable
//all of the next if are exactly the same, they just
//uses different variables
if((millis() - led_1_last_blink) >= led_1_blink_time){
if(digitalRead(13) == HIGH){
digitalWrite(13, LOW);
}else{
digitalWrite(13, HIGH);
}
led_1_last_blink = millis();
}

if((millis() - led_2_last_blink) >= led_2_blink_time){
if(digitalRead(12) == HIGH){
digitalWrite(12, LOW);
}else{
digitalWrite(12, HIGH);
}
led_2_last_blink = millis();
}

if((millis() - led_3_last_blink) >= led_3_blink_time){
if(digitalRead(11) == HIGH){
digitalWrite(11, LOW);
}else{
digitalWrite(11, HIGH);
}
led_3_last_blink = millis();
}

if((millis() - led_4_last_blink) >= led_4_blink_time){
if(digitalRead(10) == HIGH){
digitalWrite(10, LOW);
}else{
digitalWrite(10, HIGH);
}
led_4_last_blink = millis();
}

if((millis() - led_5_last_blink) >= led_5_blink_time){
if(digitalRead(9) == HIGH){
digitalWrite(9, LOW);
}else{
digitalWrite(9, HIGH);
}
led_5_last_blink = millis();
}

if((millis() - led_6_last_blink) >= led_6_blink_time){
if(digitalRead(8) == HIGH){
digitalWrite(8, LOW);
}else{
digitalWrite(8, HIGH);
}
led_6_last_blink = millis();
}
}

 

tchouken2

Related Posts