shdowmyst

Neopixel + IR remote + arduino

Jul 22nd, 2016
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.25 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #include <Wire.h>
  3. #define I2C_SLAVE_ADDRESS 8
  4.  
  5. // Remote control button codes
  6. const unsigned int BUTTON_1 = 0xFD08F7;
  7. const unsigned int BUTTON_2 = 0xFD8877;
  8. const unsigned int BUTTON_3 = 0xFD48B7;
  9. const unsigned int BUTTON_4 = 0xFD28D7;
  10. const unsigned int BUTTON_5 = 0xFDA857;
  11. const unsigned int BUTTON_6 = 0xFD6897;
  12. const unsigned int BUTTON_7 = 0xFD18E7;
  13. const unsigned int BUTTON_8 = 0xFD9867;
  14. const unsigned int BUTTON_9 = 0xFD58A7;
  15. const unsigned int BUTTON_UP = 0xFD50AF;
  16. const unsigned int BUTTON_DOWN = 0xFD10EF;
  17. const unsigned int BUTTON_VOL = 0xFD906F;
  18.  
  19. int t = 0; // variable for switching led rings with the remote
  20. int b = 255; // brigtness starting value
  21. // IR received code
  22. unsigned int irCode = 0;            //ir code ends here
  23. // Pattern types supported:
  24. enum  pattern { NONE, RAINBOW_CYCLE, THEATER_CHASE, COLOR_WIPE, SCANNER, FADE, FIRE };
  25. // Patern directions supported:
  26. enum  direction { FORWARD, REVERSE };
  27.  
  28. // NeoPattern Class - derived from the Adafruit_NeoPixel class
  29. class NeoPatterns : public Adafruit_NeoPixel
  30. {
  31.     public:
  32.  
  33.     // Member Variables:
  34.     pattern  ActivePattern;  // which pattern is running
  35.     direction Direction;     // direction to run the pattern
  36.  
  37.     unsigned long Interval;   // milliseconds between updates
  38.     unsigned long lastUpdate; // last update of position
  39.  
  40.     uint32_t Color1, Color2;  // What colors are in use
  41.     uint16_t TotalSteps;  // total number of steps in the pattern
  42.     uint16_t Index;  // current step within the pattern
  43.  
  44.     void (*OnComplete)();  // Callback on completion of pattern
  45.  
  46.     // Constructor - calls base-class constructor to initialize strip
  47.     NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)())
  48.     :Adafruit_NeoPixel(pixels, pin, type)
  49.     {
  50.         OnComplete = callback;
  51.     }
  52.  
  53.     // Update the pattern
  54.     void Update()
  55.     {
  56.         if((millis() - lastUpdate) > Interval) // time to update
  57.         {
  58.             lastUpdate = millis();
  59.             switch(ActivePattern)
  60.             {
  61.                 case RAINBOW_CYCLE:
  62.                     RainbowCycleUpdate();
  63.                     break;
  64.                 case THEATER_CHASE:
  65.                     TheaterChaseUpdate();
  66.                     break;
  67.                 case COLOR_WIPE:
  68.                     ColorWipeUpdate();
  69.                     break;
  70.                 case SCANNER:
  71.                     ScannerUpdate();
  72.                     break;
  73.                 case FADE:
  74.                     FadeUpdate();
  75.             case FIRE:
  76.                     RainbowCycleUpdate();  
  77.                     break;
  78.                 default:
  79.                     break;
  80.             }
  81.         }
  82.     }
  83.  
  84.     // Increment the Index and reset at the end
  85.     void Increment()
  86.     {
  87.         if (Direction == FORWARD)
  88.         {
  89.            Index++;
  90.            if (Index >= TotalSteps)
  91.             {
  92.                 Index = 0;
  93.                 if (OnComplete != NULL)
  94.                 {
  95.                     OnComplete(); // call the comlpetion callback
  96.                 }
  97.             }
  98.         }
  99.         else // Direction == REVERSE
  100.         {
  101.             --Index;
  102.             if (Index <= 0)
  103.             {
  104.                 Index = TotalSteps-1;
  105.                 if (OnComplete != NULL)
  106.                 {
  107.                     OnComplete(); // call the comlpetion callback
  108.                 }
  109.             }
  110.         }
  111.     }
  112.  
  113.     // Reverse pattern direction
  114.     void Reverse()
  115.     {
  116.         if (Direction == FORWARD)
  117.         {
  118.             Direction = REVERSE;
  119.             Index = TotalSteps-1;
  120.         }
  121.         else
  122.         {
  123.             Direction = FORWARD;
  124.             Index = 0;
  125.         }
  126.     }
  127.  
  128.     // Initialize for a RainbowCycle
  129.     void RainbowCycle(uint8_t interval, direction dir = FORWARD)
  130.     {
  131.         ActivePattern = RAINBOW_CYCLE;
  132.         Interval = interval;
  133.         TotalSteps = 255;
  134.         Index = 0;
  135.         Direction = dir;
  136.     }
  137.  
  138.  
  139.     // Update the Rainbow Cycle Pattern
  140.     void RainbowCycleUpdate()
  141.     {
  142.         for(int i=0; i< numPixels(); i++)
  143.         {
  144.             setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255));
  145.         }
  146.         show();
  147.         Increment();
  148.     }
  149.  
  150.     // Initialize for a Theater Chase
  151.     void TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir = FORWARD)
  152.     {
  153.         ActivePattern = THEATER_CHASE;
  154.         Interval = interval;
  155.         TotalSteps = numPixels();
  156.         Color1 = color1;
  157.         Color2 = color2;
  158.         Index = 0;
  159.         Direction = dir;
  160.    }
  161.  
  162.     // Update the Theater Chase Pattern
  163.     void TheaterChaseUpdate()
  164.     {
  165.         for(int i=0; i< numPixels(); i++)
  166.         {
  167.             if ((i + Index) % 3 == 0)
  168.             {
  169.                 setPixelColor(i, Color1);
  170.             }
  171.             else
  172.             {
  173.                 setPixelColor(i, Color2);
  174.             }
  175.         }
  176.         show();
  177.         Increment();
  178.     }
  179.  
  180.     // Initialize for a ColorWipe
  181.     void ColorWipe(uint32_t color, uint8_t interval, direction dir = FORWARD)
  182.     {
  183.         ActivePattern = COLOR_WIPE;
  184.         Interval = interval;
  185.         TotalSteps = numPixels();
  186.         Color1 = color;
  187.         Index = 0;
  188.         Direction = dir;
  189.     }
  190.  
  191.  
  192.     // Update the Color Wipe Pattern
  193.     void ColorWipeUpdate()
  194.     {
  195.         setPixelColor(Index, Color1);
  196.         show();
  197.         Increment();
  198.     }
  199.  
  200.     // Initialize for a SCANNNER
  201.     void Scanner(uint32_t color1, uint8_t interval)
  202.     {
  203.         ActivePattern = SCANNER;
  204.         Interval = interval;
  205.         TotalSteps = (numPixels() - 1) * 2;
  206.         Color1 = color1;
  207.         Index = 0;
  208.     }
  209.  
  210.     // Update the Scanner Pattern
  211.     void ScannerUpdate()
  212.     {
  213.         for (int i = 0; i < numPixels(); i++)
  214.         {
  215.             if (i == Index)  // Scan Pixel to the right
  216.             {
  217.                  setPixelColor(i, Color1);
  218.             }
  219.             else if (i == TotalSteps - Index) // Scan Pixel to the left
  220.             {
  221.                  setPixelColor(i, Color1);
  222.             }
  223.             else // Fading tail
  224.             {
  225.                  setPixelColor(i, DimColor(getPixelColor(i)));
  226.             }
  227.         }
  228.         show();
  229.         Increment();
  230.     }
  231.  
  232.     // Initialize for a Fade
  233.     void Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir = FORWARD)
  234.     {
  235.         ActivePattern = FADE;
  236.         Interval = interval;
  237.         TotalSteps = steps;
  238.         Color1 = color1;
  239.         Color2 = color2;
  240.         Index = 0;
  241.         Direction = dir;
  242.     }
  243.  
  244.     // Update the Fade Pattern
  245.     void FadeUpdate()
  246.     {
  247.         // Calculate linear interpolation between Color1 and Color2
  248.         // Optimise order of operations to minimize truncation error
  249.         uint8_t red = ((Red(Color1) * (TotalSteps - Index)) + (Red(Color2) * Index)) / TotalSteps;
  250.         uint8_t green = ((Green(Color1) * (TotalSteps - Index)) + (Green(Color2) * Index)) / TotalSteps;
  251.         uint8_t blue = ((Blue(Color1) * (TotalSteps - Index)) + (Blue(Color2) * Index)) / TotalSteps;
  252.  
  253.         ColorSet(Color(red, green, blue));
  254.         show();
  255.         Increment();
  256.     }
  257.  
  258.  
  259.     // Calculate 50% dimmed version of a color (used by ScannerUpdate)
  260.     uint32_t DimColor(uint32_t color)
  261.     {
  262.         // Shift R, G and B components one bit to the right
  263.         uint32_t dimColor = Color(Red(color) >> 1, Green(color) >> 1, Blue(color) >> 1);
  264.         return dimColor;
  265.     }
  266.  
  267.     // Set all pixels to a color (synchronously)
  268.     void ColorSet(uint32_t color)
  269.     {
  270.         for (int i = 0; i < numPixels(); i++)
  271.         {
  272.             setPixelColor(i, color);
  273.         }
  274.         show();
  275.     }
  276.  
  277.     // Returns the Red component of a 32-bit color
  278.     uint8_t Red(uint32_t color)
  279.     {
  280.         return (color >> 16) & 0xFF;
  281.     }
  282.  
  283.     // Returns the Green component of a 32-bit color
  284.     uint8_t Green(uint32_t color)
  285.     {
  286.         return (color >> 8) & 0xFF;
  287.     }
  288.  
  289.     // Returns the Blue component of a 32-bit color
  290.     uint8_t Blue(uint32_t color)
  291.     {
  292.         return color & 0xFF;
  293.     }
  294.  
  295.     // Input a value 0 to 255 to get a color value.
  296.     // The colours are a transition r - g - b - back to r.
  297.     uint32_t Wheel(byte WheelPos)
  298.     {
  299.         WheelPos = 255 - WheelPos;
  300.         if(WheelPos < 85)
  301.         {
  302.             return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  303.         }
  304.         else if(WheelPos < 170)
  305.         {
  306.             WheelPos -= 85;
  307.             return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  308.         }
  309.         else
  310.         {
  311.             WheelPos -= 170;
  312.             return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  313.         }
  314.     }
  315.  
  316.    // brightness, need some improvements probably
  317.    void incbrigtness() {
  318.    if (b >= 255) {
  319.       b = 255;
  320.    setBrightness(b);}
  321.    else {b = b + 10;  
  322.    setBrightness(b);}
  323.    }
  324.  
  325.    void decbrigtness() {
  326.    if (b <= 5) {
  327.       b = 5;
  328.    setBrightness(b);}
  329.    else {b = b - 10;  
  330.    setBrightness(b);}
  331.    }
  332.  
  333. };
  334.  
  335. void Ring1Complete();
  336. void Ring2Complete();
  337.  
  338. void receiveEvent(int numBytes) {//decode the ir signal
  339.   byte byte1 = Wire.read();
  340.   byte byte2 = Wire.read();
  341.   irCode = byte2;
  342.   irCode = irCode << 8;
  343.   irCode = irCode | byte1;
  344. }                                                                        
  345.  
  346. // Define some NeoPatterns for the two rings and the stick
  347. //  as well as some completion routines
  348. NeoPatterns Ring1(24, 6, NEO_GRB + NEO_KHZ800, &Ring1Complete); // led parameters
  349. NeoPatterns Ring2(24, 7, NEO_GRB + NEO_KHZ800, &Ring2Complete);    
  350.  
  351. // Initialize everything and prepare to start                                            
  352. void setup()
  353. {
  354.    Serial.begin(9600);
  355.    Serial.println("my-main-program starting!");
  356.    Wire.begin(I2C_SLAVE_ADDRESS);
  357.    Wire.onReceive(receiveEvent);
  358.    Serial.println("my-main-program successfully started!");
  359.  }
  360. // Main loop                                                  
  361. void loop()
  362. {
  363.     // Update the rings.
  364.    if (irCode) {             //Reads the ir code
  365.     handleButton(irCode);
  366.     irCode = 0;
  367.   }
  368.  Ring1.Update();
  369.  Ring2.Update();
  370.  }
  371.  
  372. void handleButton(unsigned int irCode) {
  373.   switch (irCode) {
  374.    case BUTTON_UP:      // button up  will control led stip 1, button 2 will control let strip 2, button 3 both.
  375.       t = 1;
  376.      Serial.println(t);    
  377.      break;
  378.    case BUTTON_DOWN:
  379.       t = 2;
  380.      Serial.println(t);
  381.       break;
  382.    case BUTTON_VOL:
  383.      t = 0;
  384.       Serial.println(t);
  385.      break;
  386.    case BUTTON_1:
  387.      if (t == 1) {    // this also needs some improvements
  388.       Serial.println(t);
  389.      Ring1.TheaterChase(Ring1.Color(0,255,0), Ring1.Color(0,0,50), 100);
  390.       }
  391.      else if (t == 2) {
  392.      Serial.println(t);
  393.      Ring2.TheaterChase(Ring1.Color(255,0,0), Ring1.Color(0,50,0), 100);
  394.       }
  395.      else {      // some random effects for testing
  396.      Serial.println(t);
  397.       Ring1.TheaterChase(Ring1.Color(0,0,255), Ring1.Color(50,0,0), 100);
  398.      Ring2.TheaterChase(Ring1.Color(0,0,255), Ring1.Color(50,0,0), 100);
  399.       }
  400.    break;
  401.     case BUTTON_2:
  402.       Serial.println("2");
  403.        Ring1.Scanner(Ring1.Color(255,0,0), 55);
  404.      break;
  405.     case BUTTON_3:
  406.      Ring1.ColorWipe(Ring1.Color(0,255,255), 55);
  407.      Serial.println("3");
  408.       break;
  409.     case BUTTON_4:
  410.       Ring1.Scanner(Ring1.Color(0,255,255), 55);
  411.      Serial.println("4");
  412.       break;
  413.     case BUTTON_5:
  414.       Serial.println("5");
  415.       break;
  416.     case BUTTON_6:
  417.       Serial.println("6");
  418.       break;
  419.     case BUTTON_7:
  420.       Serial.println("7");
  421.       break;
  422.     case BUTTON_8:         //  test the brightness here.
  423.    Ring1.incbrigtness();
  424.    Serial.println(b);
  425.      break;
  426.     case BUTTON_9:
  427.    Ring1.decbrigtness();
  428.    Serial.println(b);
  429.      break;
  430.     default:
  431.       Serial.print("Unrecognized code received: 0x");
  432.       Serial.println(irCode, HEX);
  433.       break;      
  434.   }    
  435. }
  436.  
  437.  
  438. //------------------------------------------------------------
  439. //Completion Routines - get called on completion of a pattern
  440. //------------------------------------------------------------
  441.  
  442. // Ring1 Completion Callback
  443. void Ring1Complete() //dont really need to do anything specific after loop ended so should go back to update.
  444. {Ring1.Update(); }
  445.  
  446. void Ring2Complete()
  447. {Ring2.Update(); }
Advertisement
Add Comment
Please, Sign In to add comment