Guest User

Two Array Demo 1.1

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