Hello,
I'm currently trying to do my first simple project written & designed by myself from the ground up. It's basically an RGB LED that will change colour to match an input on the serial monitor. I have the bulk of the code working, the only thing stumping me is how to get the LED to cycle through the spectrum whilst waiting on a serial input. I have the code working to constantly cycle the colours on it's own, but when I paste it into a 'while' function that is waiting for serial input (Serial.available() == 0);{LED cycle code here } it runs once and then waits for input but doesn't cycle again.
The code for the 'while' loop waiting in serial input is lines 28 to 70
(I know I could probably code the colours far more efficiently but i'm only just beginning so go easy on me )
int rPin = 6;
int gPin = 10;
int bPin = 11;
int rColour;
int gColour;
int bColour;
String myName;
String colour;
int redBrightness;
int greenBrightness;
int blueBrightness;
void setup() {
Serial.begin(9600);
pinMode(rPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(bPin, OUTPUT);
}
void loop() {
redBrightness = 0;
greenBrightness = 0;
blueBrightness = 0;
Serial.println("Hello!! What is your name?");
Serial.println("");
while(Serial.available() == 0);{
while(redBrightness<=255)
{
analogWrite(rPin, redBrightness);
redBrightness = (redBrightness+1);
delay(5);
}
while(greenBrightness<=255)
{
analogWrite(gPin, greenBrightness);
greenBrightness = (greenBrightness+1);
delay(5);
}
while(blueBrightness<=255)
{
analogWrite(bPin, blueBrightness);
blueBrightness = (blueBrightness+1);
delay(5);
}
while(redBrightness>=0)
{
analogWrite(rPin, redBrightness);
redBrightness = (redBrightness-1);
delay(5);
}
while(blueBrightness>=0)
{
analogWrite(bPin, blueBrightness);
blueBrightness = (blueBrightness-1);
delay(5);
}
while(greenBrightness>=0)
{
analogWrite(gPin, greenBrightness);
greenBrightness = (greenBrightness-1);
delay(5);
}
}
myName = Serial.readString();
Serial.print("Hello ");
Serial.print(myName);
Serial.print("! What is your favourite colour?");
colour = Serial.readString();
while(Serial.available() == 0);{
}
colour = Serial.readString();
if(colour == "turquoise"){
analogWrite(rPin, 64);
analogWrite(gPin, 224);
analogWrite(bPin, 208);
}
else if(colour == "pink"){
analogWrite(rPin, 255);
analogWrite(gPin, 50);
analogWrite(bPin, 50);
}
else if(colour == "purple"){
analogWrite(rPin, 150);
analogWrite(gPin, 0);
analogWrite(bPin, 150);
}
else if(colour == "red"){
analogWrite(rPin, 255);
analogWrite(gPin, 0);
analogWrite(bPin, 0);
}
else if(colour == "green"){
analogWrite(rPin, 0);
analogWrite(gPin, 255);
analogWrite(bPin, 0);
}
else if(colour == "blue"){
analogWrite(rPin, 0);
analogWrite(gPin, 0);
analogWrite(bPin, 255);
}
else if(colour == "orange"){
analogWrite(rPin, 255);
analogWrite(gPin, 128);
analogWrite(bPin, 0);
}
else if(colour == "yellow"){
analogWrite(rPin, 255);
analogWrite(gPin, 175);
analogWrite(bPin, 0);
}
else if(colour == "white"){
analogWrite(rPin, 255);
analogWrite(gPin, 255);
analogWrite(bPin, 255);
}
else if(colour == "black"){
analogWrite(rPin, 0);
analogWrite(gPin, 0);
analogWrite(bPin, 0);
}
}