How to Blink LED Lights in Binary Pattern with Arduino

Learn how to use four different colored LEDs

In a faraway kingdom, there was a powerful wizard who could control the elements with his magic. 

A picture of a wizard heading into a faraway castle

He had four special orbs that glowed with the colors of the elements: red for fire, blue for water, green for earth and yellow for air.

A picture of a wizard with glowing orbs around

The wizard had discovered a secret language that only the wisest of creatures could understand. It was called binary and it used only two symbols: 0 and 1. With this language, the wizard could communicate with the spirits of the elements and command them to do his bidding.

A picture of a wizard with 0s and 1 around him and a book


One day, the wizard decided to create a spell that would make his orbs glow in a special pattern. He wrote the spell using a magical code that looked like this:




int yellowLED = 7;
int redLED = 6;
int blueLED = 13;
int greenLED = 11;
int dit = 100;
int dat= 500;

These lines declare variables that will be used in the program. The variables yellowLED, redLED, blueLED, and greenLED are set to specific numbers that correspond to the pins on the microcontroller that will control the LEDs. The variables dit and dat represent the duration of time that the LED will be on and off.

void setup() {
pinMode(redLED,OUTPUT);
pinMode(blueLED,OUTPUT);
pinMode(greenLED,OUTPUT);
pinMode(yellowLED,OUTPUT);
}

This is the setup() function, which runs once when the Arduino board is powered on or reset. These lines set the pins for the LEDs as output pins, meaning that they will be used to send a signal (in this case, to turn the LEDs on and off).


void loop()

This is the loop() function, which runs repeatedly after the setup() function has completed. These lines define the binary patterns for each of the 16 possible combinations of LED states, in Morse code. The digitalWrite() function is used to set the state of each LED (either HIGH or LOW).

digitalWrite(redLED,LOW);
delay(dit);
digitalWrite(blueLED,LOW);
delay(dit);
digitalWrite(yellowLED,LOW);
delay(dit);
digitalWrite(greenLED,LOW);
delay(dit);
delay(dat);//0000
This section of code is part of the main program loop that runs continuously until the microcontroller is turned off or reset. It first turns off all of the LEDs by setting their pins to LOW, then waits for a duration of time specified by the dit variable. 
digitalWrite(redLED,LOW);
delay(dit);
digitalWrite(blueLED,LOW);
delay(dit);
digitalWrite(yellowLED,HIGH);
delay(dit);
digitalWrite(greenLED,LOW);
delay(dit);
delay(dat);//0010
//  count n
digitalWrite(redLED,LOW);
delay(dit);
digitalWrite(blueLED,LOW);
delay(dit);
digitalWrite(yellowLED,HIGH);
delay(dit);
digitalWrite(greenLED,HIGH);
delay(dit);
delay(dat);//0011
//  count n
digitalWrite(redLED,LOW);
delay(dit);
digitalWrite(blueLED,HIGH);
delay(dit);
digitalWrite(yellowLED,LOW);
delay(dit);
digitalWrite(greenLED,LOW);
delay(dit);
delay(dat);//0100
//  count n
digitalWrite(redLED,LOW);
delay(dit);
digitalWrite(blueLED,HIGH);
delay(dit);
digitalWrite(yellowLED,LOW);
delay(dit);
digitalWrite(greenLED,HIGH);
delay(dit);
delay(dat);//0101
//  count n
digitalWrite(redLED,LOW);
delay(dit);
digitalWrite(blueLED,HIGH);
delay(dit);
digitalWrite(yellowLED,HIGH);
delay(dit);
digitalWrite(greenLED,LOW);
delay(dit);
delay(dat);
//0110
//  count n
digitalWrite(redLED,LOW);
delay(dit);
digitalWrite(blueLED,HIGH);
delay(dit);
digitalWrite(yellowLED,HIGH);
delay(dit);
digitalWrite(greenLED,HIGH);
delay(dit);
delay(dat);//0111
//  count n
digitalWrite(redLED,HIGH);
delay(dit);
digitalWrite(blueLED,LOW);
delay(dit);
digitalWrite(yellowLED,LOW);
delay(dit);
digitalWrite(greenLED,LOW);
delay(dit);
delay(dat);//1000
//  count n
digitalWrite(redLED,HIGH);
delay(dit);
digitalWrite(blueLED,LOW);
delay(dit);
digitalWrite(yellowLED,LOW);
delay(dit);
digitalWrite(greenLED,HIGH);
delay(dit);
delay(dat);//1001
//  count n
digitalWrite(redLED,HIGH);
delay(dit);
digitalWrite(blueLED,LOW);
delay(dit);
digitalWrite(yellowLED,HIGH);
delay(dit);
digitalWrite(greenLED,LOW);
delay(dit);
delay(dat);
//1010
//  count n
digitalWrite(redLED,HIGH);
delay(dit);
digitalWrite(blueLED,LOW);
delay(dit);
digitalWrite(yellowLED,HIGH);
delay(dit);
digitalWrite(greenLED,HIGH);
delay(dit);
delay(dat);//1011
//  count n
digitalWrite(redLED,HIGH);
delay(dit);
digitalWrite(blueLED,HIGH);
delay(dit);
digitalWrite(yellowLED,LOW);
delay(dit);
digitalWrite(greenLED,LOW);
delay(dit);
delay(dat);//1100
//  count n
digitalWrite(redLED,HIGH);
delay(dit);
digitalWrite(blueLED,HIGH);
delay(dit);
digitalWrite(yellowLED,LOW);
delay(dit);
digitalWrite(greenLED,HIGH);
delay(dit);
delay(dat);//1101
//  count n
digitalWrite(redLED,HIGH);
delay(dit);
digitalWrite(blueLED,HIGH);
delay(dit);
digitalWrite(yellowLED,HIGH);
delay(dit);
digitalWrite(greenLED,LOW);
delay(dit);
delay(dat);//1110
//  count n
digitalWrite(redLED,HIGH);
delay(dit);
digitalWrite(blueLED,HIGH);
delay(dit);
digitalWrite(yellowLED,HIGH);
delay(dit);
digitalWrite(greenLED,HIGH);
delay(dit);
delay(dat);//1111
}

After this delay, it turns on the first LED by setting its pin to HIGH, waits again for dit time, and then moves on to the next LED. This pattern continues until all of the LEDs have been turned on and off once, and then there is an additional delay of dat time before starting the pattern again.

The rest of the code is similar, but each section turns on the LEDs in a different pattern based on a binary sequence, with each LED representing a bit in the sequence. For example, the section that starts with digitalWrite(redLED,LOW) turns on the green LED to represent the binary number 0001. The binary numbers 0000 to 1111 are represented in this way, with each pattern being displayed for a duration of dat time before moving on to the next pattern.
Download the code here : ARDUINO CODE



Components for the projects:
1.Arduino uno
2. green led
3. yellow led
4.red led
5.blue led
6.resistor(330ohms)
7.battery
Circuit diagram of the binary led circuit

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.