Using an UNO trying to use the serial monitor to send commands to turn on/off certain colors or blink a led strip. Trouble is once i send one command it works but it doesnt respond to any other commands. Also i really havent figured out how to blink the leds in the function outside of loop. The led has four leads, one power and three colors. The colors are all ground and im using grounding to turn the led on or off. Below is my code.
// Code to work a led strip, change colors or blink by the input number received from the serial monitor.
int pinBlue = 4; // Setting pins and naming ints
int pinRed = 5;
int pinGreen = 6;
char ch = Serial.read();
void pushBlue(char ch) //Blue function
{
digitalWrite (pinBlue,LOW); //Low turns the light on
delay (100); //Quick pause before checking if a new char has been sent
{
Serial.read(); //checking for new char
if
(ch =='1')
pushBlue(ch);
else
digitalWrite (pinBlue,HIGH); //Turns off the blue function
}
}
void pushRed(char ch) //Same as blue function just with red led
{
digitalWrite (pinRed,LOW);
delay (100);
{
Serial.read();
if
(ch =='2')
pushRed(ch);
else
digitalWrite (pinRed,HIGH);
}
}
void pushGreen(char ch)//Same as above
{
digitalWrite (pinGreen,LOW);
delay (100);
{
Serial.read();
if
(ch =='3')
pushGreen(ch);
else
digitalWrite (pinGreen,HIGH);
}
}
void clearColor() //Turns off all leds
{
digitalWrite (pinBlue,HIGH);
digitalWrite (pinRed,HIGH);
digitalWrite (pinGreen,HIGH);
}
char blinkBlue(char ch) //Blinking Blue
{
while (ch =='4')
digitalWrite (pinBlue,LOW);
delay (1000);
digitalWrite (pinBlue,HIGH);
delay (1000);
}
void setup() {
// put your setup code here, to run once:
pinMode (pinBlue, OUTPUT); // Setting the pins as outputs
pinMode (pinRed, OUTPUT);
pinMode (pinGreen, OUTPUT);
digitalWrite (pinBlue,HIGH);
digitalWrite (pinRed,HIGH);
digitalWrite (pinGreen,HIGH);
Serial.begin (9600); //Starting the serial comm and printing line below
Serial.println ("Enter Led Number 1,2,3,4 or x to clear");
}
void loop() {
if (Serial.available()) // Opening if statement follwed by all the diff vars for led's
{
char ch = Serial.read(); // Setting the char ch as the link between the led pins
if (ch =='1')
{
Serial.println ("Turned on LED ");
pushBlue(ch); // Calling the push blue below
}
if (ch =='2') //Same as above and below, just calling different pins from different serial inputs
{
Serial.print("Turned on LED ");
pushRed(ch);
}
if (ch =='3')
{
Serial.print ("Turned on LED ");
pushGreen (ch);
Serial.print ("Turned on LED ");
}
{
if (ch =='x')
Serial.print ("Turned off all LED ");
clearColor ();
}
{
if (ch =='4')
char blinkBlue (char ch);
Serial.print ("Blinking LED ");
Serial.println(ch);
}
}}