Arduino Fun Fun Series: Alternating Light Ups With Input

by Abdul Halim_93 in Circuits > Arduino

105 Views, 1 Favorites, 0 Comments

Arduino Fun Fun Series: Alternating Light Ups With Input

tinkerhut.JPG

Hello There! I am back with another experiment! This time we are going to do a little variant of the first experiment, Alternating Light Ups . The objective of this experiment is as follows:

Objective

1. To key in an input to control the delay of the alternating light ups

Items Needed:
1. Arduino Board( Arduino UNO is recommended, i am using the MEGA varient)

2. One LED(any color you like!)

How Does the Circuit Looks Like?????

schems.JPG

Well just like the previous experiment, we just needed to plug in an LED on pin 12 of the Arduino board! Again just a small tiny reminder, The negative leg of the LED goes to the ground while the positive leg goes to pin 12.


The schematic is as shown for reference purposes

What Does the Code Mean?

Code.JPG

As usual i am going to separate the code into three parts

INITIALISATION:
int led = 12;// initializing the led

Setting Up:

void setup() {

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() {

Serial.println("How much should i loop?"); //Prompt User for Input

if (Serial.available() > 0) {//if input available

int cnt = 1;//set the counter into 1

int answer = Serial.parseInt();//get the input to another variable

while(cnt <= answer)//if counter less than or equal than input, then repeat

{

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

Serial.print(answer);//print the input as well

Serial.println();// print a new line(go to a new line) on the serial monitor

cnt++;// add the count variable

}

}

}



the whole code is also shown for reference











That's Nice But Where to Key in the Input?

serial.JPG
serialmonitor.JPG

Look at the most top hand right item in the Arduino IDE, you will see something like a magnifying glass symbol. Just click it and a pop up will appear. That pop up is called the serial monitor. This monitor will show whatever that you printed out(for example:Serial.print(cnt*1000); )

Other than showing output, you can also key in the input on the input box on top of the monitor! In this example, try key in your favorite number and press enter( You can also click send). After keying in your input you will see the delay counting up just like the previous experiment until your chosen number!

Welp, thats all for this week's experiment. I hope everyone who tries the experiment could get the result as intended but if you dont, dont worry, comment down and i may be able to help you out tooooo! Saying that, i am going to see you guys on the next experiment, this time, i might use some sort of display :)


Have a nice day everyone! See ya again!