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.
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.
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 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
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
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
Comments
Post a Comment