Advertisement
diemastermonkey

rgbbounce

Feb 10th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.73 KB | None | 0 0
  1. /* "RGBBounce" - RGB fading without delay()
  2.     For Arduino C  
  3.     @diemastermonkey 2016 (for Internet Toybox)
  4.  */
  5.  
  6. // From config: What pins are RGB
  7. int iRedPin = 9;
  8. int iGreenPin = 10;
  9. int iBluePin = 11;
  10.  
  11. // Target values - user may change at any time
  12. // RGB Step routine continuously moving toward them
  13. int iRedTarget = 128;
  14. int iBlueTarget = 240;
  15. int iGreenTarget = 32;
  16.  
  17. // Current values - more efficient than reading them
  18. int iRedNow = 0;
  19. int iBlueNow = 0;
  20. int iGreenNow = 0;
  21.  
  22. // A counter for RGB updates: This controls rate of color fades
  23. // Decrement each cycle, then when zero, update RGB and recharge
  24. int iRGBCounterCharge = 50;    // Recharge to this
  25. int iRGBCounter = iRGBCounterCharge;
  26.  
  27. // For user: How often to randomly change (use 0 for never)
  28. // This is 'sides' on the dice for changes
  29. int iRGBRandomDice = 4096000;    // 2048000 =~ 30s changes @ 16mhz
  30.  
  31. void setup()  {
  32.   // Set output mode - superfluous?
  33.     pinMode (iRedPin, OUTPUT);
  34.     pinMode (iGreenPin, OUTPUT);
  35.     pinMode (iBluePin, OUTPUT);  
  36.    
  37. }
  38.  
  39. void loop()  {
  40.   // Update RGB pins if time to do so, else mb randomize targets
  41.   if (fnRGBStep() == 0) {
  42.     // No RGB event, maybe randomize
  43.     iRedTarget = fnRGBTargetRand (iRedTarget);
  44.     iGreenTarget = fnRGBTargetRand (iGreenTarget);    
  45.     iBlueTarget = fnRGBTargetRand (iBlueTarget);
  46.   }
  47. }
  48.  
  49. // Move and set RGB values toward current global "target" values
  50. // Also handles RGB "update" counter. Returns 1 if it was event time.
  51. int fnRGBStep () {
  52.     iRGBCounter--;          // Decrement counter, ditch if not time yet
  53.     if (iRGBCounter > 0) { return (0); }  
  54.     // If still here time for event - recharge counter
  55.     iRGBCounter = iRGBCounterCharge;
  56.    
  57.     int iRate = 1;          // Optional change rate!
  58.     // Red
  59.     iRedNow += iRate * fnToward (iRedNow, iRedTarget);
  60.     analogWrite (iRedPin, iRedNow);
  61.     // Green
  62.     iGreenNow += iRate * fnToward (iGreenNow, iGreenTarget);
  63.     analogWrite (iGreenPin, iGreenNow);
  64.     // Blue
  65.     iBlueNow += iRate * fnToward (iBlueNow, iBlueTarget);
  66.     analogWrite (iBluePin, iBlueNow);
  67.  
  68.    return (1);    // If caller interested, it was RGB event time
  69. }
  70.  
  71. // Return value that should be added to get from argFrom to argTo
  72. int fnToward (int iArgFrom, int iArgTo) {
  73.   if (iArgFrom < iArgTo) { return (+1); }
  74.   if (iArgFrom > iArgTo) { return (-1); }
  75.   return (0);      // If same
  76. }
  77.  
  78. // Return a random destination for the arg RGB value, or the arg value
  79. // Die is sides set by user in iRGBRandomDice, thus == 1 allows "0 for never"
  80. int fnRGBTargetRand (int iArgNow) {
  81.   if (random(iRGBRandomDice) == 1) {    // 1-in-x chance of changing
  82.     return (random(255));
  83.   }  
  84.   return (iArgNow);                    // Else what you sent me
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement