Hi everyone !
I just bought a RGB LED from Sparkfun (LED - RGB Clear Common Cathode - COM-00105 - SparkFun Electronics) and I'm trying to use it. This is pretty simple. Now I'd like to control the color from my computer and change it with the Serial monitor, and I wrote this code :
int REDPin = 11; // RED pin of the LED to PWM pin 4
int GREENPin = 10; // GREEN pin of the LED to PWM pin 5
int BLUEPin = 9; // BLUE pin of the LED to PWM pin 6
int incomingByte = 0;
void setup()
{
pinMode(REDPin, OUTPUT);
pinMode(GREENPin, OUTPUT);
pinMode(BLUEPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
Serial.println("Entrer rouge");
while (Serial.available() == 0) { }
incomingByte = Serial.read();
analogWrite(REDPin, incomingByte);
Serial.println("Entrer vert");
while (Serial.available() == 0) { }
incomingByte = Serial.read();
analogWrite(GREENPin, incomingByte);
Serial.println("Entrer bleu");
while (Serial.available() == 0) { }
incomingByte = Serial.read();
analogWrite(BLUEPin, incomingByte);
delay(20);
// analogWrite(GREENPin, 255);
// analogWrite(REDPin, 255);
// analogWrite(BLUEPin, 255);
}
Simply, it asks to enter red, waits the Serial read to be > 0 and then writes what it got to the red pin, doing the same after for the green and blue pins. Now my question is simple : why does not it work ? ^^ The program is simple but when I enter the red, it doesn't asks me the other colors and the LED is white and then asks me the green (vert for me because I'm french) but whatever I put it does nothing, and it asks me the green forever.
Can someone help me ? I think it's because we have to "reset" the Serial.available() but I did not figure how to do it.
Thank you in advance