Advertisement
Corium

Untitled

Dec 7th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.64 KB | None | 0 0
  1. #include <TimerOne.h>
  2. #include "LPD6803.h"
  3. #include <RCSwitch.h>
  4.  
  5. RCSwitch mySwitch = RCSwitch();
  6.  
  7. // Choose which 2 pins you will use for output.
  8. // Can be any valid output pins.
  9. int dataPin = 9;       // 'yellow' wire
  10. int clockPin = 6;      // 'green' wire
  11. // Don't forget to connect 'blue' to ground and 'red' to +5V
  12. // Timer 1 is also used by the strip to send pixel clocks
  13.  
  14. // Set the first variable to the NUMBER of addresses.
  15. LPD6803 strip = LPD6803(50, dataPin, clockPin);
  16.  
  17. int lastFunction;
  18.  
  19. void setup() {
  20.   Serial.begin(9600);
  21.   mySwitch.enableReceive(0); //Interrupt 0, digital pin 2 on UNO, tested on MEGA.
  22.    
  23.   // The Arduino needs to clock out the data to the pixels
  24.   // this happens in interrupt timer 1, we can change how often
  25.   // to call the interrupt. setting CPUmax to 100 will take nearly all all the
  26.   // time to do the pixel updates and a nicer/faster display,
  27.   // especially with strands of over 100 dots.
  28.   // (Note that the max is 'pessimistic', its probably 10% or 20% less in reality)
  29.  
  30.   strip.setCPUmax(45);  // start with 50% CPU usage. up this if the strand flickers or is slow. Tested on MEGA, doesn't work quite well on UNO :D
  31.   strip.begin(); // Start up the LED counter
  32.   strip.show(); // Update the strip, to start they are all 'off'
  33.  
  34.   lastFunction = 0; //Set lastFunction to default value
  35. }
  36.  
  37.  
  38. void loop() {
  39.  
  40.  
  41.  Serial.println("onLoop");
  42.  if (mySwitch.available())
  43.  {
  44.     int value = mySwitch.getReceivedValue();
  45.    
  46.     if (value == 0)
  47.     {
  48.       Serial.print("Unknown encoding");
  49.     }
  50.     else
  51.     {
  52.       switch(getRemote(value))
  53.       {
  54.         case 0:
  55.         mySwitch.resetAvailable();
  56.           runLastFunction();
  57.           break;
  58.         case 1:
  59.         mySwitch.resetAvailable();
  60.           rainbow(50, true);
  61.           break;
  62.         case 2:
  63.         mySwitch.resetAvailable();
  64.           panWipe(25, 30, 50, true);
  65.           break;
  66.         case 3:
  67.         case 4:
  68.         case 5:
  69.         case 6:
  70.         case 7:
  71.         default:
  72.         mySwitch.resetAvailable();
  73.           wipeToBlack(45);
  74.           break;  
  75.       }
  76.     }
  77.   }
  78.  
  79. }
  80.  
  81. void runLastFunction()
  82. {
  83.   switch(lastFunction)
  84.   {
  85.     default:
  86.     case 0:
  87.       wipeToBlack(50);
  88.       return colorWipe(Wheel(56), 50); //COLOR PULSE!! :D
  89.     case 1:
  90.       return panWipe(25, 30, 50, true);
  91.   }
  92. }
  93.  
  94. void rainbow(uint8_t wait, bool infinite) {
  95.   int i, j;
  96.   long t;
  97.  
  98.   for (j=0; j < 96 * 3; j++) {     // 3 cycles of all 96 colors in the wheel
  99.     for (i=0; i < strip.numPixels(); i++) {
  100.       strip.setPixelColor(i, Wheel( (i + j) % 96));
  101.     }
  102.     strip.show();   // write all the pixels out
  103.     t = millis();
  104.     while(millis() - t < wait)
  105.     {
  106.       if(mySwitch.available())
  107.       {
  108.         return;
  109.       }
  110.     //wait
  111.     }
  112.   }
  113.   if(infinite == true)
  114.   {
  115.     return rainbow(wait, infinite);
  116.   }
  117.   return;
  118. }
  119.  
  120. // Slightly different, this one makes the rainbow wheel equally distributed
  121. // along the chain
  122. void rainbowCycle(uint8_t wait) {
  123.   int i, j;
  124.  
  125.   for (j=0; j < 96 * 5; j++) {     // 5 cycles of all 96 colors in the wheel
  126.     for (i=0; i < strip.numPixels(); i++) {
  127.       // tricky math! we use each pixel as a fraction of the full 96-color wheel
  128.       // (thats the i / strip.numPixels() part)
  129.       // Then add in j which makes the colors go around per pixel
  130.       // the % 96 is to make the wheel cycle around
  131.       strip.setPixelColor(i, Wheel( ((i * 96 / strip.numPixels()) + j) % 96) );
  132.     }  
  133.     strip.show();   // write all the pixels out
  134.     delay(wait);
  135.   }
  136. }
  137.  
  138. // fill the dots one after the other with said color
  139. // good for testing purposes
  140. void colorWipe(uint16_t c, uint8_t wait) {
  141.   int i, t;
  142.   for (i=0; i < strip.numPixels(); i++) {
  143.       strip.setPixelColor(i, c);
  144.       strip.show();
  145.       t = millis();
  146.       while(millis() - t < wait)
  147.       {
  148.         if(mySwitch.available())
  149.         {
  150.           return;
  151.         }
  152.         //wait
  153.       }
  154.   }
  155.   return;
  156. }
  157.  
  158.  
  159. void panWipe(uint16_t c, uint16_t c2, int wait, bool infinite) {
  160.   int i;
  161.   long t;
  162.   int nPixels = strip.numPixels();
  163.  
  164.   for(int j = 0; j < nPixels; j++)
  165.   {
  166.     for (i=0; i < (nPixels / 2); i++) {
  167.         strip.setPixelColor(((i + j) % nPixels), Wheel(map(i, 0, (nPixels / 2), c, c2)));
  168.     }
  169.     for (i=(nPixels / 2); i < nPixels; i++) {
  170.         strip.setPixelColor(((i + j) % nPixels), Wheel(map((nPixels - i), 0, (nPixels / 2), c, c2)));
  171.     }
  172.         strip.show();
  173.         t = millis();
  174.         while(millis() - t < wait)
  175.         {
  176.           if(mySwitch.available())
  177.           {
  178.             int receivedKey = getRemote(mySwitch.getReceivedValue());
  179.            
  180.             if(receivedKey == 2) //Speed decrease key
  181.             {
  182.               wait += 5;
  183.               Serial.println(wait);
  184.               mySwitch.resetAvailable();
  185.             }
  186.            
  187.             else if(receivedKey == 3) //Speed increase key
  188.             {
  189.               if(wait > 45)
  190.               {
  191.                 wait -= 5;
  192.               }
  193.               Serial.println(wait);
  194.               mySwitch.resetAvailable();
  195.             }
  196.  
  197.             else if(receivedKey == 4) //Color1 increase key
  198.             {
  199.               if(c > 0)
  200.               {
  201.                 c -= 1;
  202.               }
  203.               Serial.println(c);
  204.               mySwitch.resetAvailable();
  205.             }
  206.  
  207.             else if(receivedKey == 5) //Color1 decrease key
  208.             {
  209.               if(c < 96)
  210.               {
  211.                 c += 1;
  212.               }
  213.               Serial.println(c);
  214.               mySwitch.resetAvailable();
  215.             }
  216.  
  217.             else if(receivedKey == 6) //Color2 increase key
  218.             {
  219.               if(c2 > 0)
  220.               {
  221.                 c2 -= 1;
  222.               }
  223.               Serial.println(c2);
  224.               mySwitch.resetAvailable();
  225.             }
  226.  
  227.             else if(receivedKey == 7) //Color2 decrease key
  228.             {
  229.               if(c2 < 96)
  230.               {
  231.                 c2 += 1;
  232.               }
  233.               Serial.println(c2);
  234.               mySwitch.resetAvailable();
  235.             }
  236.            
  237.             else if(receivedKey == 1) //Pause key received
  238.             {
  239.                 wipeToBlack(50);
  240.                 mySwitch.resetAvailable(); //Clean buffer after "Pause" key pressed
  241.                
  242.                 while(!mySwitch.available()) //On pause, wait until new key
  243.                 {}
  244.                
  245.                 while(mySwitch.available()) //Resume when any key (break)
  246.                 {
  247.                     mySwitch.resetAvailable();
  248.                     break;
  249.                 }
  250.             }
  251.           }
  252.           //wait
  253.         }
  254.   }
  255.  
  256.   if(infinite)
  257.   {
  258.   return panWipe(c, c2, wait, infinite);
  259.   }
  260.   return;
  261. }
  262.  
  263. void wipeToBlack(uint8_t wait) {
  264.   uint8_t c = 0;
  265.   int i;
  266.   long t;
  267.   for (i=0; i <= (strip.numPixels() / 2); i++) {
  268.       strip.setPixelColor(i, c);
  269.       strip.setPixelColor((strip.numPixels() - i), c);
  270.       strip.show();
  271.       t = millis();
  272.       while(millis() - t < wait)
  273.       {
  274.         //wait
  275.       }
  276.   }
  277.   return;
  278. }
  279.  
  280. /* Helper functions */
  281.  
  282. // Create a 15 bit color value from R,G,B
  283. unsigned int Color(byte r, byte g, byte b)
  284. {
  285.   //Take the lowest 5 bits of each value and append them end to end
  286.   return( ((unsigned int)g & 0x1F )<<10 | ((unsigned int)b & 0x1F)<<5 | (unsigned int)r & 0x1F);
  287. }
  288.  
  289. //Input a value 0 to 127 to get a color value.
  290. //The colours are a transition r - g -b - back to r
  291. unsigned int Wheel(byte WheelPos)
  292. {
  293.   byte r,g,b;
  294.   switch(WheelPos >> 5)
  295.   {
  296.     case 0:
  297.       r=31- WheelPos % 32;   //Red down
  298.       g=WheelPos % 32;      // Green up
  299.       b=0;                  //blue off
  300.       break;
  301.     case 1:
  302.       g=31- WheelPos % 32;  //green down
  303.       b=WheelPos % 32;      //blue up
  304.       r=0;                  //red off
  305.       break;
  306.     case 2:
  307.       b=31- WheelPos % 32;  //blue down
  308.       r=WheelPos % 32;      //red up
  309.       g=0;                  //green off
  310.       break;
  311.   }
  312.   return(Color(r,g,b));
  313. }
  314.  
  315. int getRemote(int value)
  316. {
  317.   switch(value)
  318.       {
  319.         case 5592512:
  320.           return 0;
  321.           break;
  322.         case 5592368:
  323.           return 1;
  324.           break;
  325.         case 5592560:
  326.           return 2;
  327.           break;
  328.         case 5592332:
  329.           return 3;
  330.           break;
  331.         case 5592524:
  332.           return 4;
  333.           break;
  334.         case 5592380:
  335.           return 5;
  336.           break;
  337.         case 5592572:
  338.           return 6;
  339.           break;
  340.         case 5592323:
  341.           return 7;
  342.           break;
  343.         default:
  344.           return 99;
  345.           break;  
  346.       }
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement