Guest User

Untitled

a guest
Oct 17th, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.14 KB | None | 0 0
  1. /*****************************************************************************************
  2.  * Oil Rigs Prototype
  3.  * By LucasM
  4.  * 10/17/14
  5.  * TEST BRANCH - Dual Animation Test
  6.  *
  7.  * Runs Two Animations on different Pins, using 2 Arrays
  8.  * Includes AnimateMarquee, AnimateLine, ClearPixelsInstant, ClearPixelRange
  9.  *
  10.  ******************************************************************************************/
  11.  
  12. //Add these libraries
  13. #include <FastLED.h> //Library for RGB LED Control
  14. #include <DmxSimple.h> //Library for DMX Control
  15. #include <Metro.h> //Easy Timer Library
  16.  
  17.  
  18. //Define Constants
  19. //Define Pins
  20. #define BUTTON1 2
  21. #define BUTTON2 3
  22. #define BUTTON3 5
  23. //Data Pins
  24. #define PIXEL_PIN1 12
  25. #define PIXEL_PIN2 11
  26. #define DMX_PIN 4 //Data Pin for DMX Output, must be 4 on LM's Breakout Board
  27.  
  28. //Create other Constants
  29. //Pixel Counts for Each Array
  30. const int ARRAY1_SIZE 300;
  31. const int ARRAY2_SIZE 64;
  32. //Time Out Values for Pixel Animations
  33. const int CYCLETIMEOUT1 = 255;
  34. const int CYCLETIMEOUT2 = 400;
  35. const int CYCLETIMEOUT3 = 500;
  36. //DMX Channels
  37. const int DMXZONE1 = 1;
  38. const int DMXZONE2 = 2;
  39. const int DMXZONE3 = 3;
  40. //Values for Various Brightness levels, used in DMX and Pixel operations
  41. const int HOMEBRIGHT = 100;
  42. const int FLUXBRIGHT = 150;
  43. const int MAXBRIGHT = 255;
  44.  
  45. //Pixel Ranges for Various Areas
  46. const int BEG1 = 0;
  47. const int END1 = 300;
  48. const int BEG2 = 0;
  49. const int END2 = 64;
  50. const int BEG3 = 0;
  51. const int END3 = 0;
  52. const int BEG4 = 0;
  53. const int END4 = 0;
  54. //Define Color Variables for Animations
  55. //Hue Rainbow:
  56. //(0-Red)(32-Orange)(64-Yellow)(96-Green)(128-Aqua)(160-Blue)(192-Purple)(224-Pink)
  57. //Saturation 0 = White, 255 = Full Color
  58. const byte HUE1 = 0;
  59. const byte SAT1 = 255;
  60. const byte HUE2 = 96;
  61. const byte SAT2 = 255;
  62. const byte HUE3 = 160;
  63. const byte SAT3 = 255;
  64. const byte HUE4 = 160;
  65. const byte SAT4 = 0;
  66.  
  67. //Create Pixel Array
  68. CRGB leds1[ARRAY1_SIZE];//Create array for RGB LED Control
  69. //Create Pixel Stepper Clocks (In Milliseconds)
  70. Metro PixelClock_Anim = Metro(50);
  71. Metro PixelClock_Fade = Metro(10);  
  72.  
  73. //Define Variables
  74. int ACycles1 = 0;
  75. int ACycles2 = 0;
  76. int ACycles3 = 0;
  77. int ACycles4 = 0;
  78. //Setup Starting Values for Variables used to keep track of where the animation is through each loop
  79. int Tracker1 = BEG1;
  80. int Tracker2 = BEG2;
  81. int Tracker3 = BEG3;
  82. int Tracker4 = BEG4;
  83. //Brightness Values are used for DMX Controlled lights, AND for functions which dim/brighten slowly
  84. byte Brightness1 = 0;
  85. byte Brightness2 = 0;
  86. byte Brightness3 = 0;
  87. byte Brightness4 = 0;
  88.  
  89. boolean RunPixelA = false;  //Used for the Metronome timing Pixel Animation Steps
  90. boolean RunPixelF = false;  //Used for the Metronome timing Pixel Fade Steps
  91. //Booleans for Animation Run States
  92. boolean Animation1 = false;
  93. boolean Animation2 = false;
  94. boolean Animation3 = false;
  95. boolean Animation4 = false;
  96.  
  97.  
  98.  
  99.  
  100.  
  101. /*****************************************************************************************
  102.  * SETUP
  103.  *****************************************************************************************/
  104. void setup() {
  105.   //Setup Pins
  106.   pinMode(BUTTON1, INPUT_PULLUP);
  107.   pinMode(BUTTON2, INPUT_PULLUP);
  108.   pinMode(BUTTON3, INPUT_PULLUP);
  109.   pinMode(BUTTON4, INPUT_PULLUP);
  110.   pinMode(PIXEL_PIN1, OUTPUT);
  111.   pinMode(PIXEL_PIN2, OUTPUT);
  112.   pinMode(DMX_PIN, OUTPUT);
  113.  
  114.   //Other Actions
  115.   FastLED.addLeds<NEOPIXEL, PIXEL_PIN1>(leds1, ARRAY1_SIZE);
  116.   FastLED.addLeds<NEOPIXEL, PIXEL_PIN2>(leds2, ARRAY2_SIZE);
  117.  
  118.  
  119.   DmxSimple.usePin(DMX_PIN);
  120.   DmxSimple.maxChannel(512);
  121.  
  122.   //Start with A Clean Slate
  123.   memset(leds1,0,sizeof(leds1));
  124.   memset(leds2,0,sizeof(leds2));
  125.  
  126.   FastLED.show();
  127.  
  128.   delay(1000);
  129.  
  130. }
  131.  
  132. /*****************************************************************************************
  133.  * LOOP
  134.  *****************************************************************************************/
  135. void loop() {
  136.   //Check to see if enough time has elapsed to draw the next step for each type of Pixel Animation
  137.   if(PixelClock_Anim.check() == 1){
  138.     RunPixelA = true;
  139.   }
  140.   else {
  141.     RunPixelA = false;
  142.   }
  143.     if(PixelClock_Fade.check() == 1){
  144.     RunPixelF = true;
  145.   }
  146.   else {
  147.     RunPixelF = false;
  148.   }
  149.  
  150.  
  151.   //Button 1 - Red Marquee, Pixel Array on First Array
  152.   if(digitalRead(BUTTON1) == LOW){
  153.     Animation1 = true;
  154.     ACycles1 = 0;
  155.   }
  156.  
  157.   if(ACycles1 == CYCLETIMEOUT1){
  158.     Animation1 = false;
  159.   }
  160.  
  161.   if(Animation1 == true){//if the button is down
  162.     if(RunPixelA == true){//and it is time to step
  163.       Tracker1 = AnimateMarquee(END1, Tracker1, HUE1, SAT1);
  164.       ACycles1++;
  165.     }
  166.   }
  167.  
  168.  if(Animation1 == false){
  169.     Tracker1 = ClearPixelsInstant(BEG1, END1, Tracker1);
  170.   }
  171.  
  172.  
  173.   //Button 2 - Draw A Green Line On Second Array
  174.   if(digitalRead(BUTTON2) == LOW){
  175.     Animation2 = true;
  176.     ACycles2 = 0;
  177.   }
  178.  
  179.   if(ACycles2 == CYCLETIMEOUT2){
  180.     Animation2 = false;
  181.   }
  182.  
  183.   if(Animation2 == true){//if the button is down
  184.     if(RunPixelF == true){//and it is time to step
  185.       Tracker2 = AnimateLine(BEG2, END2, Tracker2, HUE2, SAT2, MAXBRIGHT);
  186.       ACycles2++;
  187.     }
  188.   }
  189.  
  190.  if(Animation2 == false){
  191.     Tracker2 = ClearPixelsInstant2(BEG2, END2, Tracker2);
  192.   }
  193.  
  194.    
  195.  
  196. }
  197.  
  198.  
  199.  
  200.  
  201. /*****************************************************************************************
  202.  * FUNCTIONS
  203.  *****************************************************************************************/
  204.  
  205. //#AnimateMarquee
  206. //Causes a contiuous trail of 5 pixels, spaced by 3 apart, to animate
  207. //Assumes Start at 0
  208. //There is probably a more elegant way to code this
  209. //This is easy to read
  210. int AnimateMarquee(int EndPixel, int CurrentPixel, int Hue, int Sat){
  211.   //Depending on CurrentPixel Value, Pick the animation case
  212.   const int FLUXBRIGHT = 150;
  213.   const int MAXBRIGHT = 255;
  214.   switch (CurrentPixel){
  215.   case 0:
  216.     leds1[0] = CHSV(Hue, Sat, FLUXBRIGHT);
  217.     leds1[1] = CHSV(Hue, Sat, FLUXBRIGHT);
  218.     leds1[2] = CHSV(Hue, Sat, FLUXBRIGHT);
  219.     leds1[3] = CHSV(Hue, Sat, FLUXBRIGHT);
  220.     leds1[4] = CHSV(Hue, Sat, FLUXBRIGHT);
  221.     leds1[5] = CHSV(Hue, Sat, MAXBRIGHT);
  222.     leds1[6] = CHSV(Hue, Sat, MAXBRIGHT);
  223.     leds1[7] = CHSV(Hue, Sat, MAXBRIGHT);
  224.     break;
  225.  
  226.   case 1:
  227.     leds1[0] = CHSV(Hue, Sat, MAXBRIGHT);
  228.     leds1[1] = CHSV(Hue, Sat, FLUXBRIGHT);
  229.     leds1[2] = CHSV(Hue, Sat, FLUXBRIGHT);
  230.     leds1[3] = CHSV(Hue, Sat, FLUXBRIGHT);
  231.     leds1[4] = CHSV(Hue, Sat, FLUXBRIGHT);
  232.     leds1[5] = CHSV(Hue, Sat, FLUXBRIGHT);
  233.     leds1[6] = CHSV(Hue, Sat, MAXBRIGHT);
  234.     leds1[7] = CHSV(Hue, Sat, MAXBRIGHT);
  235.     break;
  236.  
  237.   case 2:
  238.     leds1[0] = CHSV(Hue, Sat, MAXBRIGHT);
  239.     leds1[1] = CHSV(Hue, Sat, MAXBRIGHT);
  240.     leds1[2] = CHSV(Hue, Sat, FLUXBRIGHT);
  241.     leds1[3] = CHSV(Hue, Sat, FLUXBRIGHT);
  242.     leds1[4] = CHSV(Hue, Sat, FLUXBRIGHT);
  243.     leds1[5] = CHSV(Hue, Sat, FLUXBRIGHT);
  244.     leds1[6] = CHSV(Hue, Sat, FLUXBRIGHT);
  245.     leds1[7] = CHSV(Hue, Sat, MAXBRIGHT);
  246.     break;
  247.  
  248.   case 3:
  249.     leds1[0] = CHSV(Hue, Sat, MAXBRIGHT);
  250.     leds1[1] = CHSV(Hue, Sat, MAXBRIGHT);
  251.     leds1[2] = CHSV(Hue, Sat, MAXBRIGHT);
  252.     leds1[3] = CHSV(Hue, Sat, FLUXBRIGHT);
  253.     leds1[4] = CHSV(Hue, Sat, FLUXBRIGHT);
  254.     leds1[5] = CHSV(Hue, Sat, FLUXBRIGHT);
  255.     leds1[6] = CHSV(Hue, Sat, FLUXBRIGHT);
  256.     leds1[7] = CHSV(Hue, Sat, FLUXBRIGHT);
  257.     break;
  258.  
  259.   case 4:
  260.     leds1[0] = CHSV(Hue, Sat, FLUXBRIGHT);
  261.     leds1[1] = CHSV(Hue, Sat, MAXBRIGHT);
  262.     leds1[2] = CHSV(Hue, Sat, MAXBRIGHT);
  263.     leds1[3] = CHSV(Hue, Sat, MAXBRIGHT);
  264.     leds1[4] = CHSV(Hue, Sat, FLUXBRIGHT);
  265.     leds1[5] = CHSV(Hue, Sat, FLUXBRIGHT);
  266.     leds1[6] = CHSV(Hue, Sat, FLUXBRIGHT);
  267.     leds1[7] = CHSV(Hue, Sat, FLUXBRIGHT);
  268.     break;
  269.  
  270.   case 5:
  271.     leds1[0] = CHSV(Hue, Sat, FLUXBRIGHT);
  272.     leds1[1] = CHSV(Hue, Sat, FLUXBRIGHT);
  273.     leds1[2] = CHSV(Hue, Sat, MAXBRIGHT);
  274.     leds1[3] = CHSV(Hue, Sat, MAXBRIGHT);
  275.     leds1[4] = CHSV(Hue, Sat, MAXBRIGHT);
  276.     leds1[5] = CHSV(Hue, Sat, FLUXBRIGHT);
  277.     leds1[6] = CHSV(Hue, Sat, FLUXBRIGHT);
  278.     leds1[7] = CHSV(Hue, Sat, FLUXBRIGHT);
  279.     break;
  280.  
  281.   case 6:
  282.     leds1[0] = CHSV(Hue, Sat, FLUXBRIGHT);
  283.     leds1[1] = CHSV(Hue, Sat, FLUXBRIGHT);
  284.     leds1[2] = CHSV(Hue, Sat, FLUXBRIGHT);
  285.     leds1[3] = CHSV(Hue, Sat, MAXBRIGHT);
  286.     leds1[4] = CHSV(Hue, Sat, MAXBRIGHT);
  287.     leds1[5] = CHSV(Hue, Sat, MAXBRIGHT);
  288.     leds1[6] = CHSV(Hue, Sat, FLUXBRIGHT);
  289.     leds1[7] = CHSV(Hue, Sat, FLUXBRIGHT);
  290.     break;
  291.  
  292.   case 7:
  293.     leds1[0] = CHSV(Hue, Sat, FLUXBRIGHT);
  294.     leds1[1] = CHSV(Hue, Sat, FLUXBRIGHT);
  295.     leds1[2] = CHSV(Hue, Sat, FLUXBRIGHT);
  296.     leds1[3] = CHSV(Hue, Sat, FLUXBRIGHT);
  297.     leds1[4] = CHSV(Hue, Sat, MAXBRIGHT);
  298.     leds1[5] = CHSV(Hue, Sat, MAXBRIGHT);
  299.     leds1[6] = CHSV(Hue, Sat, MAXBRIGHT);
  300.     leds1[7] = CHSV(Hue, Sat, FLUXBRIGHT);
  301.     break;
  302.   }
  303.  
  304.  
  305.   //after picking case, apply to the rest of the range
  306.   for(int i = 7; i < EndPixel; i = i + 8){
  307.     memmove8( &leds1[i], &leds1[0], 8 * sizeof( CRGB ) );
  308.   }
  309.  
  310.   FastLED.show();
  311.   //Reset at 8, since the function will draw all of the rest for you
  312.   if(CurrentPixel <= 7){
  313.     ++CurrentPixel;
  314.   }
  315.   if(CurrentPixel >= 8){
  316.     CurrentPixel = 0;
  317.   }
  318.   return CurrentPixel;
  319. }
  320.  
  321.  
  322.  
  323. //#AnimateLine
  324. //Starting at one end, a solid line grows to the end
  325. //Call without Checking the timer to make it appear like they all turn on instantly
  326. //Adjust Brightness on the fly to make it appear to Glow or Fade
  327. int AnimateLine(int StartPixel, int EndPixel, int CurrentPixel, byte Hue, byte Saturation, byte Brightness){
  328.   leds2[CurrentPixel] = CHSV(Hue, Saturation, Brightness);
  329.   FastLED.show();
  330.   if(CurrentPixel < EndPixel){
  331.     CurrentPixel++;
  332.   }
  333.   if(CurrentPixel == EndPixel){
  334.     CurrentPixel = StartPixel;
  335.   }
  336.   return CurrentPixel;
  337. }
  338.  
  339.  
  340. //#ClearPixelsInstant
  341. //Clear All Pixels Everywhere in the Array
  342. int ClearPixelsInstant(int StartPixel, int EndPixel, int CurrentPixel){
  343.   memset(leds1,0,sizeof(leds1));
  344.   FastLED.show();
  345.   CurrentPixel = StartPixel;
  346.   return CurrentPixel;
  347. }
  348.  
  349. //#ClearPixelsInstant2
  350. //Clear All Pixels Everywhere in the Array
  351. int ClearPixelsInstant2(int StartPixel, int EndPixel, int CurrentPixel){
  352.   memset(leds2,0,sizeof(leds2));
  353.   FastLED.show();
  354.   CurrentPixel = StartPixel;
  355.   return CurrentPixel;
  356. }
Advertisement
Add Comment
Please, Sign In to add comment