Arduino and serial monitor input

This was a simple example to show sending information using the serial monitor, in our other examples we have generally used the serial monitor for debug, outputting messages.

This example basically switches on red, green or blue on a tri-color LED.

The LED is connected as follows

RED cathode – Pin 10
GREEN cathode – Pin 11
BLUE cathode – Pin 12

The LED was a small breakout with suitable current limiting resistors already in place.

Code

Fairly straightforward, send a key to switch on a colour, anything else switches it off

 

int redPin = 10;
int greenPin = 11;
int bluePin = 12;

void setup()
{
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
AllOff();
Serial.begin(9600);
Serial.println(“Press the following keys”);
Serial.println(“r – switch on red, g – switch on blue, b – switch on green”);
}

void loop()
{
if (Serial.available())
{
char ch = Serial.read();

switch(ch)
{
case ‘r’ : RedOn();
break;
case ‘g’ : GreenOn();
break;
case ‘b’ : BlueOn();
break;
default : AllOff();
break;
}
delay(1000);
}
}

void RedOn()
{
digitalWrite(redPin,LOW);
digitalWrite(bluePin,HIGH);
digitalWrite(greenPin,HIGH);
}

void GreenOn()
{
digitalWrite(redPin,HIGH);
digitalWrite(bluePin,HIGH);
digitalWrite(greenPin,LOW);
}

void BlueOn()
{
digitalWrite(redPin,HIGH);
digitalWrite(bluePin,LOW);
digitalWrite(greenPin,HIGH);
}

void AllOff()
{
digitalWrite(redPin,HIGH);
digitalWrite(greenPin,HIGH);
digitalWrite(bluePin,HIGH);
}

 

Previous articleArduino and a MMA7455 accelerometer
Next articlembed and tri color LED