Advertisement
ccarman602

RGB LED Rainbow Blink

Jun 3rd, 2020
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * This code lets you play with the RGB LED!
  3.  *
  4.  * There are three LEDs inside this one bulb - red, green and blue. Each of them requires current
  5.  * to flow through them in order to light up. Because I wasn't paying attention, I accidentally ordered
  6.  * LEDs that are "backward" and use a common anode, or positive terminal, instead of common cathode, so
  7.  * we have to do a little math magic to make them work (that's in the colorLED function). Don't worry
  8.  * about it! Here's what you need to do:
  9.  *
  10.  * Connect the longest leg of the RGB LED (the anode) to hole f10 on the breadboard. You'll have to bend
  11.  * it out of shape a little bit to make this work.
  12.  * Then, put the outside pin next to anode in hole e9. The other two pins go into e10 and e11.
  13.  * Connect the following Arduino pins to the breadboard using color-coded male-to-male wires:
  14.  * - Arduino pin 9 (red) to breadboard hole a9
  15.  * - Arduino pin 10 (green) to breadboard hole a10
  16.  * - Arduino pin 11 (blue) to breadboard hole a11
  17.  * Finally, bridge a 1k resistor from j10 to j1, and then a wire from f1 to Arduino pin 12
  18.  * If you want a brighter LED, use a smaller resistor (e.g. 330-ohm), but not too small!
  19.  *
  20.  * There's more info about RGB LEDs here:
  21.  * https://www.circuitbread.com/tutorials/how-rgb-leds-work-and-how-to-control-color
  22.  * and here:
  23.  * https://www.sunfounder.com/learn/lesson-5-rgb-led-universal-kit.html
  24.  *
  25.  */
  26.  
  27. #define redPin    9
  28. #define greenPin 10
  29. #define bluePin  11
  30. #define outPin   12 // we shouldn't use pin 13 because it's tied to LED_BUILTIN
  31. #define delayTime 500
  32.  
  33. void setup() {
  34.   // put your setup code here, to run once:
  35.   pinMode(redPin, OUTPUT); // red
  36.   pinMode(greenPin, OUTPUT); // green
  37.   pinMode(bluePin, OUTPUT); // blue
  38.   pinMode(outPin, OUTPUT); // provides the positive current to the common anode
  39.   pinMode(LED_BUILTIN, OUTPUT);
  40.   digitalWrite(LED_BUILTIN, LOW); // this is distracting so we're going to turn it off
  41. }
  42.  
  43. void loop() {
  44.   // put your main code here, to run repeatedly:
  45.   digitalWrite(outPin, HIGH); // this continuously supplies the positive current to the common anode
  46.  
  47.   colorLED(255, 0, 0); // red
  48.   delay(delayTime);
  49.  
  50.   colorLED(192, 192, 0); // orange?
  51.   delay(delayTime);
  52.  
  53.   colorLED(128, 192, 0); // yellow
  54.   delay(delayTime);
  55.  
  56.   colorLED(0, 255, 0); // green
  57.   delay(delayTime);
  58.  
  59.   colorLED(0, 0, 255); // blue
  60.   delay(delayTime);
  61.  
  62.   colorLED(128, 0, 192); // purple
  63.   delay(delayTime);
  64. }
  65.  
  66. void colorLED(int red, int green, int blue) {
  67.   // since it's common anode, you have to treat the signal opposite of what you would normally do
  68.   // so you have to subtract the value FROM 255 (which is the maximum value)
  69.   analogWrite(redPin, (255 - red));
  70.   analogWrite(greenPin, (255 - green));
  71.   analogWrite(bluePin, (255 - blue));
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement