Advertisement
Guest User

Untitled

a guest
Jun 26th, 2013
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.94 KB | None | 0 0
  1.             //All credit for this code goes to Philihp at http://philihp.com/blog/2011/diy-led-hula-hoop/
  2.  
  3.             #include "LPD8806.h"
  4.             #include "SPI.h"
  5.             #include <avr/sleep.h>
  6.             #include "carrot.h"
  7.  
  8.             //since data is on 11 and clock is on 13, we can use hardware SPI
  9.             LPD8806 strip = LPD8806(160);
  10.  
  11.  
  12.             int powerPin = 4;
  13.             int upModePin = 3;
  14.             int upColorPin = 2;
  15.  
  16.             int upModeButtonState = HIGH;
  17.             int upModeButtonCycles = 0;
  18.             int upColorButtonState = HIGH;
  19.             int upColorButtonCycles = 0;
  20.  
  21.             int CYCLES_DEBOUNCE = 2; //check the button for X ticks to see if it is bouncing
  22.             int MAX_COLORS = 8;
  23.             int MAX_MODES = 8;
  24.             int MAX_STRIPES = 5;
  25.  
  26.             unsigned long tick = 0;
  27.  
  28.             int mode = 1;
  29.             int color = 1;
  30.  
  31.             uint16_t i, j, x, y;
  32.             uint32_t c, d;
  33.  
  34.             // Set the first variable to the NUMBER of pixels. 32 = 32 pixels in a row
  35.             // The LED strips are 32 LEDs per meter but you can extend/cut the strip
  36.  
  37.             void ISR_Wake() {
  38.               detachInterrupt(0);
  39.               detachInterrupt(1);
  40.             }
  41.  
  42.             void blackout() {
  43.               for(int i=0; i < strip.numPixels()+1; i++) {
  44.                 strip.setPixelColor(i, strip.Color(0,0,0));
  45.               }
  46.               strip.show();
  47.             }
  48.  
  49.  
  50.             void triggerSleep() {
  51.               blackout();
  52.  
  53.               attachInterrupt(0,ISR_Wake,LOW); //pin 2
  54.               attachInterrupt(1,ISR_Wake,LOW); //pin 3
  55.  
  56.               set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  57.               sleep_enable();
  58.               sleep_mode();
  59.               //sleeping, until rudely interrupted
  60.               sleep_disable();
  61.             }
  62.  
  63.             void triggerModeUp() {
  64.               ++mode;
  65.               blackout();
  66.             }
  67.  
  68.             void triggerColorUp() {
  69.               color++;
  70.               blackout();
  71.             }
  72.  
  73.  
  74.             void handleButtons() {
  75.               if(digitalRead(powerPin) == LOW) {
  76.                 triggerSleep();
  77.               }
  78.               // software debounce
  79.               if(digitalRead(upModePin) != upModeButtonState) {
  80.                 upModeButtonCycles++;
  81.                 if(upModeButtonCycles > CYCLES_DEBOUNCE) {
  82.                   upModeButtonCycles = 0;
  83.                   upModeButtonState = digitalRead(upModePin);
  84.                   if(upModeButtonState == LOW) {
  85.                     triggerModeUp();
  86.                   }
  87.                 }
  88.               }
  89.               // software debounce
  90.               if(digitalRead(upColorPin) != upColorButtonState) {
  91.                 upColorButtonCycles++;
  92.                 if(upColorButtonCycles > CYCLES_DEBOUNCE) {
  93.                   upColorButtonCycles = 0;
  94.                   upColorButtonState = digitalRead(upColorPin);
  95.                   if(upColorButtonState == LOW) {
  96.                     triggerColorUp();
  97.                   }
  98.                 }
  99.               }
  100.             }
  101.  
  102.             void handleStrip() {
  103.               switch(mode%MAX_MODES) {
  104.               case 0: //solid
  105.                 c = GetColor(color%MAX_COLORS);
  106.                 for(i=0; i<strip.numPixels(); i++) {
  107.                   strip.setPixelColor(i, c);
  108.                 }
  109.                 break;
  110.               case 1: //solid
  111.                 c = GetColor((tick%3+color)% MAX_COLORS);
  112.                 for(i=0; i<strip.numPixels(); i++) {
  113.                   strip.setPixelColor(i, c);
  114.                 }
  115.                 break;
  116.               case 2: //Strobe
  117.                 if(tick % 100 == 0) {
  118.                   c = GetColor(color%MAX_COLORS);
  119.                   for(i=0; i<strip.numPixels(); i++) {
  120.                     strip.setPixelColor(i, c);
  121.                   }
  122.                 }
  123.                 if(tick % 50 == 1) {
  124.                   c = strip.Color(0,0,0);
  125.                   for(i=0; i<strip.numPixels(); i++) {
  126.                     strip.setPixelColor(i, c);
  127.                   }
  128.                 }
  129.                 break;
  130.             case 3: //Strobe modified
  131.             j = tick % 384;
  132.                 if(tick % 100 == 0) {
  133.                   c = GetColor(color%MAX_COLORS);
  134.                   for(i=0; i<strip.numPixels(); i++) {
  135.                     strip.setPixelColor(i, c);
  136.                   }
  137.                 }
  138.                 if(tick % 50 == 1) {
  139.                   c = GetColor(color%MAX_COLORS);
  140.                   for(i=0; i<strip.numPixels(); i++) {
  141.                     strip.setPixelColor(i, Wheel(j));
  142.                   }
  143.                 }
  144.                 break;
  145.               case 4: //chasers
  146.                 d = (color / MAX_COLORS) % MAX_STRIPES + 1; //chaser
  147.                 c = GetColor(color % MAX_COLORS); //color
  148.                 j = tick % (strip.numPixels()/d);
  149.                 for(i=0; i < strip.numPixels(); i++) {
  150.                   if(i % (strip.numPixels()/d) == j) {
  151.                     strip.setPixelColor(i, c);
  152.                   }
  153.                   else {
  154.                     strip.setPixelColor(i, strip.Color(0,0,0));
  155.                   }
  156.                 }
  157.                 break;
  158.               case 5: //chasers + statics
  159.                 d = (color / MAX_COLORS) % MAX_STRIPES + 1; //chaser
  160.                 c = GetColor(color % MAX_COLORS); //color
  161.                 j = tick % (strip.numPixels()/d);
  162.                 for(i=0; i < strip.numPixels(); i++) {
  163.                   x = i % (strip.numPixels()/d);
  164.                   if((x == j) || (x == 0)) {
  165.                     strip.setPixelColor(i, c);
  166.                   }
  167.                   else {
  168.                     strip.setPixelColor(i, strip.Color(0,0,0));
  169.                   }
  170.                 }
  171.                 break;
  172.               case 6: // rainbows
  173.                 j = tick % 384;
  174.                 for(i=0; i < strip.numPixels(); i++) {
  175.                   strip.setPixelColor(i, Wheel(((i * 384 / strip.numPixels() * (color%MAX_COLORS)) + j) % 384));
  176.                 }
  177.                 break;
  178.               case 7: //carrot POV
  179.                 j = tick % 158;
  180.                 d = carrot[j];
  181.                 //green //orange
  182.                 c = (j < 30)?GetColor((color+2)%MAX_COLORS):GetColor((color+3)%MAX_COLORS);
  183.                 for(i=0;i<32;i++) {
  184.                   //adding 32 to the index makes it appear on the side opposite the controller
  185.                   if(d & 0x00000001) {
  186.                     strip.setPixelColor(i+32, c);
  187.                   }
  188.                   else {
  189.                     strip.setPixelColor(i+32, strip.Color(0,0,0));
  190.                   }
  191.                   d >>= 1;
  192.                 }
  193.                 break;
  194.               /* case 7: //test
  195.               int hiBit = 0;
  196.               int n = strip.numPixels() - 1;
  197.               for(i=0; i<strip.numPixels(); i++) {
  198.               for(int bit=1; bit < 0x8000; bit <<= 1) {
  199.                 if(n & bit) hiBit = bit;
  200.               }
  201.  
  202.               int bit, reverse;
  203.               for(int i=0; i<(hiBit << 1); i++) {
  204.                 // Reverse the bits in i to create ordered dither:
  205.                 reverse = 0;
  206.                 for(bit=1; bit <= hiBit; bit <<= 1) {
  207.                   reverse <<= 1;
  208.                   if(i & bit) reverse |= 1;
  209.                 }
  210.                 strip.setPixelColor(reverse, c);
  211.                 strip.show();
  212.                 delay(500);
  213.               }
  214.               delay(250); // Hold image for 1/4 sec
  215.             }
  216.              
  217.               break; */
  218.               }
  219.  
  220.               strip.setPixelColor(strip.numPixels()-1, strip.Color(0,0,0)); //set that last LED off because it overlaps
  221.               strip.show();
  222.             }
  223.  
  224.             void setup() {
  225.               // Start up the LED strip
  226.               strip.begin();
  227.  
  228.               pinMode(powerPin, INPUT); // declare pushbutton as input
  229.               pinMode(upModePin, INPUT); // declare pushbutton as input
  230.               pinMode(upColorPin, INPUT); // declare pushbutton as input
  231.  
  232.               triggerSleep();
  233.             }
  234.  
  235.             void loop() {
  236.               tick++;
  237.               handleStrip();
  238.               handleButtons();
  239.             }
  240.  
  241.             // fill the dots one after the other with said color
  242.             // good for testing purposes
  243.             void colorWipe(uint32_t c, uint8_t wait) {
  244.               int i;
  245.  
  246.               for (i=0; i < strip.numPixels(); i++) {
  247.                   strip.setPixelColor(i, c);
  248.                   strip.show();
  249.                   delay(wait);
  250.               }
  251.             }
  252.  
  253.             // Chase a dot down the strip
  254.             // good for testing purposes
  255.             void colorChase(uint32_t c, uint8_t wait) {
  256.               int i;
  257.  
  258.               for (i=0; i < strip.numPixels(); i++) {
  259.                 strip.setPixelColor(i, 0); // turn all pixels off
  260.               }
  261.  
  262.               for (i=0; i < strip.numPixels(); i++) {
  263.                   strip.setPixelColor(i, c); // set one pixel
  264.                   strip.show(); // refresh strip display
  265.                   delay(wait); // hold image for a moment
  266.                   strip.setPixelColor(i, 0); // erase pixel (but don't refresh yet)
  267.               }
  268.               strip.show(); // for last erased pixel
  269.             }
  270.  
  271.             // An "ordered dither" fills every pixel in a sequence that looks
  272.             // sparkly and almost random, but actually follows a specific order.
  273.             void dither(uint32_t c, uint8_t wait) {
  274.  
  275.               // Determine highest bit needed to represent pixel index
  276.               int hiBit = 0;
  277.               int n = strip.numPixels() - 1;
  278.               for(int bit=1; bit < 0x8000; bit <<= 1) {
  279.                 if(n & bit) hiBit = bit;
  280.               }
  281.  
  282.               int bit, reverse;
  283.               for(int i=0; i<(hiBit << 1); i++) {
  284.                 // Reverse the bits in i to create ordered dither:
  285.                 reverse = 0;
  286.                 for(bit=1; bit <= hiBit; bit <<= 1) {
  287.                   reverse <<= 1;
  288.                   if(i & bit) reverse |= 1;
  289.                 }
  290.                 strip.setPixelColor(reverse, c);
  291.                 strip.show();
  292.                 delay(wait);
  293.               }
  294.               delay(250); // Hold image for 1/4 sec
  295.             }
  296.  
  297.             // "Larson scanner" = Cylon/KITT bouncing light effect
  298.             void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait) {
  299.               int i, j, pos, dir;
  300.  
  301.               pos = 0;
  302.               dir = 1;
  303.  
  304.               for(i=0; i<((strip.numPixels()-1) * 8); i++) {
  305.                 // Draw 5 pixels centered on pos. setPixelColor() will clip
  306.                 // any pixels off the ends of the strip, no worries there.
  307.                 // we'll make the colors dimmer at the edges for a nice pulse
  308.                 // look
  309.                 strip.setPixelColor(pos - 2, strip.Color(r/4, g/4, b/4));
  310.                 strip.setPixelColor(pos - 1, strip.Color(r/2, g/2, b/2));
  311.                 strip.setPixelColor(pos, strip.Color(r, g, b));
  312.                 strip.setPixelColor(pos + 1, strip.Color(r/2, g/2, b/2));
  313.                 strip.setPixelColor(pos + 2, strip.Color(r/4, g/4, b/4));
  314.  
  315.                 strip.show();
  316.                 delay(wait);
  317.                 // If we wanted to be sneaky we could erase just the tail end
  318.                 // pixel, but it's much easier just to erase the whole thing
  319.                 // and draw a new one next time.
  320.                 for(j=-2; j<= 2; j++)
  321.                     strip.setPixelColor(pos+j, strip.Color(0,0,0));
  322.                 // Bounce off ends of strip
  323.                 pos += dir;
  324.                 if(pos < 0) {
  325.                   pos = 1;
  326.                   dir = -dir;
  327.                 } else if(pos >= strip.numPixels()) {
  328.                   pos = strip.numPixels() - 2;
  329.                   dir = -dir;
  330.                 }
  331.               }
  332.             }
  333.  
  334.             // Sine wave effect
  335.             #define PI 3.14159265
  336.             void wave(uint32_t c, int cycles, uint8_t wait) {
  337.               float y;
  338.               byte r, g, b, r2, g2, b2;
  339.  
  340.               // Need to decompose color into its r, g, b elements
  341.               g = (c >> 16) & 0x7f;
  342.               r = (c >> 8) & 0x7f;
  343.               b = c & 0x7f;
  344.  
  345.               for(int x=0; x<(strip.numPixels()*5); x++)
  346.               {
  347.                 for(int i=0; i<strip.numPixels(); i++) {
  348.                   y = sin(PI * (float)cycles * (float)(x + i) / (float)strip.numPixels());
  349.                   if(y >= 0.0) {
  350.                     // Peaks of sine wave are white
  351.                     y = 1.0 - y; // Translate Y to 0.0 (top) to 1.0 (center)
  352.                     r2 = 127 - (byte)((float)(127 - r) * y);
  353.                     g2 = 127 - (byte)((float)(127 - g) * y);
  354.                     b2 = 127 - (byte)((float)(127 - b) * y);
  355.                   } else {
  356.                     // Troughs of sine wave are black
  357.                     y += 1.0; // Translate Y to 0.0 (bottom) to 1.0 (center)
  358.                     r2 = (byte)((float)r * y);
  359.                     g2 = (byte)((float)g * y);
  360.                     b2 = (byte)((float)b * y);
  361.                   }
  362.                   strip.setPixelColor(i, r2, g2, b2);
  363.                 }
  364.                 strip.show();
  365.                 delay(wait);
  366.               }
  367.             }
  368.  
  369.             /* Helper functions */
  370.  
  371.             //Input a value 0 to 384 to get a color value.
  372.             //The colours are a transition r - g - b - back to r
  373.  
  374.             uint32_t Wheel(uint16_t WheelPos)
  375.             {
  376.               byte r, g, b;
  377.               switch(WheelPos / 128)
  378.               {
  379.               case 0:
  380.                 r = 127 - WheelPos % 128; // red down
  381.                 g = WheelPos % 128; // green up
  382.                 b = 0; // blue off
  383.                 break;
  384.               case 1:
  385.                 g = 127 - WheelPos % 128; // green down
  386.                 b = WheelPos % 128; // blue up
  387.                 r = 0; // red off
  388.                 break;
  389.               case 2:
  390.                 b = 127 - WheelPos % 128; // blue down
  391.                 r = WheelPos % 128; // red up
  392.                 g = 0; // green off
  393.                 break;
  394.               }
  395.               return(strip.Color(r,g,b));
  396.             }
  397.  
  398.  
  399.             uint32_t GetColor(int c)
  400.             {
  401.               switch(c) {
  402.               case 0:
  403.                 return strip.Color(127,0,0);
  404.               case 1:
  405.                 return strip.Color(0,0,127);
  406.               case 2:
  407.                 return strip.Color(0,127,0);
  408.               case 3:
  409.                 return strip.Color(127,31,0);
  410.               case 4:
  411.                 return strip.Color(127,127,0);
  412.               case 5:
  413.                 return strip.Color(0,127,127);
  414.               case 6:
  415.                 return strip.Color(127,0,127);
  416.               case 7:
  417.                 return strip.Color(127,127,127);
  418.               default:
  419.                 return strip.Color(0,0,0);
  420.               }
  421.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement