hey guys, Im doing a tutorial to make a push button switching color rgb led, here is the code
const int rled=9;
const int bled=11;
const int gled=10;
const int button=2;
boolean lastbutton = LOW;
boolean currentbutton = LOW;
int ledMode = 0;
void setup()
{
pinMode(rled, OUTPUT);
pinMode(bled, OUTPUT);
pinMode(gled, OUTPUT);
pinMode(button, INPUT);
}
boolean debounce(boolean last)
{
boolean current = digitalRead(button);
if (last != current)
{
delay (5);
current = digitalRead(button);
}
return current;
}
void setMode(int mode)
{
if (mode == 1)
{
digitalWrite(rled, HIGH);
digitalWrite(bled, LOW);
digitalWrite(gled, LOW);
}
if (mode == 2)
{
digitalWrite(rled, LOW);
digitalWrite(bled, LOW);
digitalWrite(gled, HIGH);
}
if (mode == 3)
{
digitalWrite(rled, LOW);
digitalWrite(bled, HIGH);
digitalWrite(gled, LOW);
}
if (mode == 4)
{
analogWrite(rled, 127);
analogWrite(bled, 127);
analogWrite(gled, 0);
}
if (mode == 5)
{
analogWrite(rled, 0);
analogWrite(bled, 127);
analogWrite(gled, 127);
}
if (mode == 6)
{
analogWrite(rled, 127);
analogWrite(bled, 0);
analogWrite(gled, 127);
}
if (mode == 7)
{
analogWrite(rled, 85);
analogWrite(bled, 85);
analogWrite(gled, 85);
}
else (mode == 0);
{
analogWrite(rled, 0);
analogWrite(bled, 0);
analogWrite(gled, 0);
}
}
void loop()
{
currentbutton = debounce(lastbutton);
if (lastbutton == LOW && currentbutton == HIGH)
{
ledMode++;
}
lastbutton = currentbutton;
if (ledMode == 8) ledMode = 0;
setMode(ledMode);
}
im using 220 ohm resistors on the leds using usb power, im getting flickering anytime i have two colors going at the same time, but when i have just red, green or blue on, their fine. anyone know what my problem is?