SHARE
TWEET

Arduino RGB LED randomizer

waterlubber Dec 31st, 2013 629 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. RGB LED Mood Light by HydroCorp Industries
  3. Coded by _______________________
  4. Help from Arduino Forums @ http://forum.arduino.cc
  5.  
  6. This is licesened under the Attribution-NonCommercial 4.0 Internationa
  7. Basically, it means you just have to say the author was me, and don't use it to make money.
  8. Contact me at waterlubber@rocketmail.com for license exclusions
  9.  
  10. ~~Changelog~~
  11. v1.0_beta: Basic colors. Red, Red/Green, Green, Green/Blue, Blue, Blue/Red
  12.  
  13. v1.0: Added random number generation!
  14. */
  15.  
  16. int redPower = 0;   //Power level of red led from 0-255
  17. int redPin = 9;     //Pin the red led is hooked up to.
  18. int greenPower = 0; //Power level of green...
  19. int greenPin = 10;  //Pin the green led...
  20. int bluePower = 0;  //etc.
  21. int bluePin = 11;   //etc.
  22. int oldRedPower = 0;
  23. int oldGreenPower = 0;
  24. int oldBluePower = 0;
  25.  
  26. void setup() {
  27.   pinMode(redPin, OUTPUT);  //Declares pins as outputs
  28.   pinMode(greenPin, OUTPUT);
  29.   pinMode(bluePin, OUTPUT);
  30.   randomSeed(analogRead(0));//Resets the Random Number Generator
  31.  
  32. }
  33.  
  34. void loop() {
  35.   redPower = random(256);   //Gets some random numbers, then stores them as new 'goal' numbers
  36.   greenPower = random(256);
  37.   bluePower = random(256);
  38.  
  39.   while(oldRedPower < redPower) { //Increase brightness to get to the new number
  40.     analogWrite(redPin, oldRedPower + 1);
  41.     oldRedPower++;
  42.     delay(5);
  43.   }
  44.   while(oldRedPower > redPower) { //Decrease brightness to get to the new number
  45.     analogWrite(redPin, oldRedPower -1);
  46.     oldRedPower--;
  47.     delay(5);
  48.   }
  49.   while(oldGreenPower < greenPower) {
  50.     analogWrite(greenPin, oldGreenPower + 1);
  51.     oldGreenPower++;
  52.     delay(5);
  53.   }
  54.   while(oldGreenPower > greenPower) {
  55.     analogWrite(greenPin, oldGreenPower -1);
  56.     oldGreenPower--;
  57.     delay(5);
  58.   }
  59.   while(oldBluePower < bluePower) {
  60.     analogWrite(bluePin, oldBluePower + 1);
  61.     oldBluePower++;
  62.     delay(5);
  63.   }
  64.   while(oldBluePower > bluePower) {
  65.     analogWrite(bluePin, oldBluePower - 1);
  66.     oldBluePower--;
  67.     delay(5);
  68.   }
  69.   oldRedPower = redPower; //Age the young!
  70.   oldGreenPower = greenPower;
  71.   oldBluePower = bluePower;
  72.   delay(random(5,20)*500); //Delay a random time
  73. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top