Free views, likes and subscribers at YouTube. Now!
Get Free YouTube Subscribers, Views and Likes

Tutorial 4 - Learn to handle a serial monitor

Follow
Emotech Systems

To carry out this simple tutorial 4. The components that we will need to carry out this project are the following:

1. Arduino UNO board
2. Breadboard
3. Malemale connection cables
4. Five 330Ω resistors (orange, orange, brown)
5. One red LED
6. One yellow LED
7. One white LED
8. One green LED
9. One blue LED

Below is the program for this tutorial, copy and paste on Arduino program:

//Declaración de constantes
#define BLUE 2 // Pin to which the blue LED is connected
#define GREEN 7 // Pin to which the green LED is connected
#define WHITE 8 // Pin to which the white LED is connected
#define YELLOW 12 // Pin to which the yellow LED is connected
#define RED 13 // Pin to which the red LED is connected
#define SERIALSPEED 9600 // Serial Monitor timing in milliseconds

//Serial Monitor Pinouts and Initialization
void setup()
{
pinMode (BLUE, OUTPUT);
pinMode (GREEN, OUTPUT);
pinMode (WHITE, OUTPUT);
pinMode (YELLOW, OUTPUT);
pinMode (RED, OUTPUT);
AllToLow();
Serial.begin(SERIALSPEED);
}

void loop()
{
//Displays the message on the serial monitor and reads the chosen option
int selected = ShowMessage ();
switch (selected)
{
case 48: //Option "0" selected
Serial.println("All LEDs off!");
AllToLow();
break;

case 49: //Option "1" selected
Serial.println("Blue LED on!");
AllToLow();
digitalWrite(BLUE, HIGH);
break;

case 50: //Option "2" selected
Serial.println("Green LED on!");
AllToLow();
digitalWrite(GREEN, HIGH);
break;

case 51: //Option "3" selected
Serial.println("White LED on!");
AllToLow();
digitalWrite(WHITE, HIGH);
break;

case 52: //Option "4" selected
Serial.println("Yellow LED on!");
AllToLow();
digitalWrite(YELLOW, HIGH);
break;

case 53: //Option "5" selected
Serial.println("Red LED on!");
AllToLow();
digitalWrite(RED, HIGH);
break;

case 57: //Option "9" selected
Serial.println("All LEDs on!");
digitalWrite(BLUE, HIGH);
digitalWrite(GREEN, HIGH);
digitalWrite(WHITE, HIGH);
digitalWrite(YELLOW, HIGH);
digitalWrite(RED, HIGH);
break;
}
}

//Function that turns off all the LEDs in the circuit
void AllToLow()
{
digitalWrite(BLUE, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(WHITE, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
}

//Function that displays the menu on the serial monitor console and reads the chosen option

int ShowMessage ()
{
Serial.println ("Turn off all LEDs: 0");
Serial.println ("Turn on blue LED: 1");
Serial.println ("Green LED on: 2");
Serial.println ("Turn on white LED: 3");
Serial.println ("Turn on yellow LED: 4");
Serial.println ("Red LED on: 5");
Serial.println ("Turn on all LEDs: 9");
Serial.print ("Enter the option: ");
while (Serial.available () == 0);
int input = Serial.read ();
return input;
}

Enjoy, share and subscribe!!

posted by ta1wa2n1o6