About Control RGB LED for Arduino
This app used in combination with an Arduino allows the user to turn on and off an RGB LED. It is designed to be used by teachers who wish to give students an introduction to basic electronics and computer coding.
You will need an Arduino, jumper wires, an RGB LED, and an HC-06 bluetooth device.
The code can be found here:
#include
int outPinRed = 9;
int outPinGreen = 10;
int outPinBlue = 11;
SoftwareSerial BT(1, 0); //TX, RX respectively
String color;
void setup() {
BT.begin(9600);
pinMode(outPinRed, OUTPUT); //RED
pinMode(outPinGreen, OUTPUT); //GREEN
pinMode(outPinBlue, OUTPUT); //BLUE
}
void loop() {
while (BT.available()) { //check to see if there is an available port
delay(10); // delay added to make things stable
char c = BT.read(); //conduct a serial read
color += c ; //build the string "blue, red, and green"
}
if (color.length() > 0) {
if (color == "red")
{
digitalWrite(outPinRed, HIGH);
digitalWrite(outPinGreen, LOW);
digitalWrite(outPinBlue, LOW);
delay(20);
}
else if (color == "green")
{
digitalWrite(outPinRed, LOW);
digitalWrite(outPinGreen, HIGH);
digitalWrite(outPinBlue, LOW);
delay(20);
}
else if (color == "blue")
{
digitalWrite(outPinRed, LOW);
digitalWrite(outPinGreen, LOW);
digitalWrite(outPinBlue, HIGH);
delay(20);
}
else if (color == "stop")
{
digitalWrite(outPinRed, HIGH);
digitalWrite(outPinGreen, HIGH);
digitalWrite(outPinBlue, HIGH);
delay(500);
digitalWrite(outPinRed, LOW);
digitalWrite(outPinGreen, LOW);
digitalWrite(outPinBlue, LOW);
delay(500);
digitalWrite(outPinRed, HIGH);
digitalWrite(outPinGreen, HIGH);
digitalWrite(outPinBlue, HIGH);
delay(500);
digitalWrite(outPinRed, LOW);
digitalWrite(outPinGreen, LOW);
digitalWrite(outPinBlue, LOW);
delay(500);
digitalWrite(outPinRed, HIGH);
digitalWrite(outPinGreen, HIGH);
digitalWrite(outPinBlue, HIGH);
delay(500);
digitalWrite(outPinRed, LOW);
digitalWrite(outPinGreen, LOW);
digitalWrite(outPinBlue, LOW);
delay(500);
}
else if (color == "fader")
{
analogWrite(outPinRed, random(0,255));
analogWrite(outPinGreen, random(0,255));
analogWrite(outPinBlue, random(0,255));
delay(250);
}
color = ""; //Reset the variable
}
}