Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This code lets you play with the RGB LED!
- *
- * There are three LEDs inside this one bulb - red, green and blue. Each of them requires current
- * to flow through them in order to light up. Because I wasn't paying attention, I accidentally ordered
- * LEDs that are "backward" and use a common anode, or positive terminal, instead of common cathode, so
- * we have to do a little math magic to make them work (that's in the colorLED function). Don't worry
- * about it! Here's what you need to do:
- *
- * Connect the longest leg of the RGB LED (the anode) to hole f10 on the breadboard. You'll have to bend
- * it out of shape a little bit to make this work.
- * Then, put the outside pin next to anode in hole e9. The other two pins go into e10 and e11.
- * Connect the following Arduino pins to the breadboard using color-coded male-to-male wires:
- * - Arduino pin 9 (red) to breadboard hole a9
- * - Arduino pin 10 (green) to breadboard hole a10
- * - Arduino pin 11 (blue) to breadboard hole a11
- * Finally, bridge a 1k resistor from j10 to j1, and then a wire from f1 to Arduino pin 12
- * If you want a brighter LED, use a smaller resistor (e.g. 330-ohm), but not too small!
- *
- * There's more info about RGB LEDs here:
- * https://www.circuitbread.com/tutorials/how-rgb-leds-work-and-how-to-control-color
- * and here:
- * https://www.sunfounder.com/learn/lesson-5-rgb-led-universal-kit.html
- *
- */
- #define redPin 9
- #define greenPin 10
- #define bluePin 11
- #define outPin 12 // we shouldn't use pin 13 because it's tied to LED_BUILTIN
- #define delayTime 500
- void setup() {
- // put your setup code here, to run once:
- pinMode(redPin, OUTPUT); // red
- pinMode(greenPin, OUTPUT); // green
- pinMode(bluePin, OUTPUT); // blue
- pinMode(outPin, OUTPUT); // provides the positive current to the common anode
- pinMode(LED_BUILTIN, OUTPUT);
- digitalWrite(LED_BUILTIN, LOW); // this is distracting so we're going to turn it off
- }
- void loop() {
- // put your main code here, to run repeatedly:
- digitalWrite(outPin, HIGH); // this continuously supplies the positive current to the common anode
- colorLED(255, 0, 0); // red
- delay(delayTime);
- colorLED(192, 192, 0); // orange?
- delay(delayTime);
- colorLED(128, 192, 0); // yellow
- delay(delayTime);
- colorLED(0, 255, 0); // green
- delay(delayTime);
- colorLED(0, 0, 255); // blue
- delay(delayTime);
- colorLED(128, 0, 192); // purple
- delay(delayTime);
- }
- void colorLED(int red, int green, int blue) {
- // since it's common anode, you have to treat the signal opposite of what you would normally do
- // so you have to subtract the value FROM 255 (which is the maximum value)
- analogWrite(redPin, (255 - red));
- analogWrite(greenPin, (255 - green));
- analogWrite(bluePin, (255 - blue));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement