Go Down

Topic: RGB Sketch that shouldn't work but does? Does anyone know why? (Read 632 times) previous topic - next topic

Sep 05, 2012, 11:27 pm Last Edit: Sep 05, 2012, 11:36 pm by joshreynolds1982 Reason: 1
Hi. I am reading "Beginning Arduino Programming" by Brian Evans. I am doing the first project that involves an RGB LED. It is supposed to blink each color, or so says the book, for 1 second.

First is says to use this sketch

/* Project 1: RGB Blinky
Uses a single RGB LED to cycle through three colors.
*/
void setup() {
pinMode(9, OUTPUT); // sets digital pins as outputs
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}
void loop() {
digitalWrite(9, HIGH); // turns on red
digitalWrite(11, LOW); // turns off blue
delay(1000); // waits for 1 second
digitalWrite(10, HIGH); // turns on green
digitalWrite(9, LOW); // turns off red
delay(1000); // waits for 1 second
digitalWrite(11, HIGH); // turns on blue
digitalWrite(10, LOW); // turns off green
delay(1000); // waits for 1 second
}

I arranged and have everything plugged in accordingly but no lights come on. The ground of the RGB is to Arduino GND. The other LED pins are to 9, 10, and 11.

I monkeyed with it for a while and attatched the RGB ground to 5v and used this code here.

void setup() {
pinMode(9, OUTPUT);  // Red LED
pinMode(10, OUTPUT); // Green LED
pinMode(11, OUTPUT); // Blue LED
}
void loop() {
digitalWrite(9, LOW); // turns on red
digitalWrite(10, HIGH); // turns off green
digitalWrite(11, HIGH); // turns off blue
delay(1000); // waits for 1 second
digitalWrite(10, LOW); // turns on green
digitalWrite(11, HIGH); // turns off blue
digitalWrite(9, HIGH); // turns off red
delay(1000); // waits for 1 second
digitalWrite(11, LOW); // turns on blue
digitalWrite(9, HIGH); // turns off red
digitalWrite(10, HIGH); // turns off green
delay(1000); // waits for 1 second
}

Now it works perfectly like it should. What I have a problem with is why the sketch in the book didn't work at first. It shouldn't have worked when I changed the ground RGB to 5v. Then I changed where it only turned the led on when it was low. Very confusing.

Does anyone have any clue why my version worked instead of the one that was "supposed to be right" in the book? Thank you very much for your time and for any suggestions you may have.

Josh



You are awesome :). Thank you very much. I hate it when it's something so simple lol. Thank you very much!

So if I'm plugging it up to 5v, I guess it has to be opposite then. Switching the Highs to Lows.

Go Up