Arduino Fun Fun Series: LED Controller Using Keyboard Inputs!

by Abdul Halim_93 in Circuits > Arduino

557 Views, 0 Favorites, 0 Comments

Arduino Fun Fun Series: LED Controller Using Keyboard Inputs!

tinkerhut.JPG

Heya everybody! How are you doing today? I hope everyone is fine and good always. Ok so today, i am going to demonstrate on how to turn on and off and control brightness of LED using keyboard input. This project is the offshoot of the original one Alternating Light Ups and Alternating Light Ups with Input . Without further delays, lets get to the objective and items needed for the project

Objective:

1.To Turn on and off LED using keyboard input

2. To increase and decrease brightness of LED using keyboard input

Items Needed:

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

2. LED(any color you like)

The Schematics

schems.JPG

So far the schematics for this project is pretty simple. Basically, we just need to plug in the LED at pin 13 and Ground.The negative leg goes to the ground while the positive leg goes to pin 13.

Coding the Project

As usual, i like to divide my code into three parts, namely, Declaration, Setting Up and Code Body/Execution.

Declaration(written before void_setup():

String ledcontrol; //i am going to declare string that is used to control the LED

int led = 13; // LED at pin 13

int brightness = 0; // a variable to store brightness value

Setting up:

void setup()

{

Serial.begin(9600); // declare serial port baud rate

pinMode(led, OUTPUT); // led as an output

delay(2000); // delay for 2 seconds

Serial.println("This experiment is to test input from keyboard to turn on and off led. Use command on to turn on and command off to turn off the led"); //a simple print out on what to do

}

Execution Code:


void loop() {
if(Serial.available()){ //check if serial monitor working

ledcontrol = Serial.readStringUntil('\n'); // this variable will read any input keyed in serial monitor
if(ledcontrol == "on") // if we key in on in the serial monitor

{
brightness = 255; // set the brightness to highest

Serial.println(ledcontrol); // print the brightness value

analogWrite(led, brightness); // write the value of brightness to the led (Means led will turn on and light up to the highest

}

else if(ledcontrol == "off") // if we key in off in the serial monitor
{

brightness = 0; // set the brightness to lowest

Serial.println(ledcontrol); // same as on loop

analogWrite(led, brightness);
}

else if (ledcontrol == "up") // if we key in up in the serial monitor
{

brightness = brightness + 10; // add the current value of brightness with 10 analogWrite(led, brightness);// same as the on loop

Serial.print("led brightness is: ");

Serial.println(brightness);

}

else if (ledcontrol == "down")// if we key in down in the serial monitor

{

brightness = brightness - 100; // subtract the current value of brightness with 100 analogWrite(led, brightness);

Serial.print("led brightness is: ");

Serial.println(brightness);

if (brightness < 0)// and if the value is less than 0

{

brightness = 0; // maintain the value as 0

analogWrite(led, brightness);

}

}

else {

Serial.println("invalid command"); // if you key in something else other than the commands above, the led will blink and say invalid command

digitalWrite(led, HIGH);

delay(1000);

digitalWrite(led, LOW);

delay(1000);

}

}

}

Test It Out!

Now for the final part! Upload the code into your Arduino and open up the serial monitor. Key in the words "on", "off", "up", and "down". See what does the input does to the led and enjoy! I'll be back with more experiments to share to all of youuuuuuuu!