Arduino Fun Fun Series: Alternating Light Ups
by Abdul Halim_93 in Circuits > Arduino
205 Views, 2 Favorites, 0 Comments
Arduino Fun Fun Series: Alternating Light Ups
Hello! and Welcome to my very first Instructables(i was active back then but i kinda forgot my old account lol). But nevertheless, i rekindled my passion on Arduino and on my path to tinker again. However, i needed some refreshers so i am making this Instructables as a guide for newcomers/pros who need refreshers. So without further ado lets get to the Objective of the Project:
Objective:
1. To alternately use Arduino built in LED and external LED
Items Needed:
1. Arduino Board( Arduino UNO is recommended, i am using the MEGA varient)
2. One LED(any color you like!)
Circuit Assemble!
The circuit part for this project is very easy! Basically just plug in a LED on the Arduino board! But remember, LED has negative leg(short leg or cathode leg) and positive leg(long leg or anode leg). The negative leg goes to the ground while the positive leg goes to pin 12.
What about the built in LED? As the name suggested, its built in( and also is connected on pin 13) so nothing to be done there.
For reference the schematic for the project is as shown
Coding the Project
Now comes the second interesting part. CODING!!! When it comes to arduino, i would always separate the code into three parts:
1.Initialisation: Where you initialize any device that you are going to use. In this project the we are going to initialize our LED at pin 12. The built in LED does not need to be initialized as its built in
2. Setting Up: Where you set up your input and output. Input is anything that requires your input in while output is whatever outcome that you want. In this project we dont have any input but we have three outputs which is our built in LED, LED at pin 12 and our serial communication output
3. The body: This is where the majority of the code is. This is where you right your code, the algorithms, equations, loops and so on and so forth
The breakdown of the code is as follows:
INITIALISATION:
int led = 12;// initializing the led
Setting Up:
void setup() {
// put your setup code here, to run once:
pinMode(led,OUTPUT);// setting the led in pin 12 as output
pinMode(LED_BUILTIN,OUTPUT);// setting the internal led as output
Serial.begin(9600);// setting the serial communication for write in and out
}
Body:
void loop() {
// put your main code here, to run repeatedly:
int cnt = 1; // initialise the cnt variable
while(cnt < 10)// a while loop for repetation
{
digitalWrite(LED_BUILTIN, HIGH);// setting internal led to light up
digitalWrite(led, LOW);// setting led pin 12 to not light
delay(cnt*1000);//delay base on cnt loop
digitalWrite(LED_BUILTIN, LOW);// setting internal pin to not light
digitalWrite(led, HIGH);// setting led pin 12 light up
delay(cnt*1000);//delay base on cnt loop
Serial.print(cnt*1000);// print the delay on the serial monitor
Serial.println();// print a new line(go to a new line) on the serial monitor
cnt++;// add the count variable
}
//basically this experiment is made to demo the while counting the
//arduino will blink accroding the count in seconds
//and will print out the reading in millisecond
}
The Whole code is as shown in the picture
Demo Video!
Well, after uploading the code your result should be something like the video shown.
So i hope anyone who tries this would be able to carry it out successfully and I'll be back with more experiments and exercises in next Instructables!
Have a nice day everyone! See ya again!