Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.13 KB | None | 0 0
  1. /*
  2.  * NEEDS ADAFRUIT NEOPIXEL & MYSENSORS LIBRARIES INSTALLED.
  3.  *
  4.  *
  5.  * Documentation: http://www.mysensors.org
  6.  * Support Forum: http://forum.mysensors.org
  7.  *
  8.  * http://www.mysensors.org/build/dimmer
  9.  */
  10.  
  11. #include <MySensor.h>
  12. #include <SPI.h>
  13. #include <Adafruit_NeoPixel.h>
  14.  
  15. #define SN "LED Strip"
  16. #define SV "1.0"
  17. #define CHILD_ID 38
  18. #define LED_PIN 5
  19. #define RGB_PIN 6
  20.  
  21. // CHANGE THE LENGTH OF YOUR NEOPIXEL STRIP HERE. Default is 30.
  22. Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, RGB_PIN, NEO_GRB + NEO_KHZ800);
  23. MySensor gw;
  24.  
  25. char rgb[7] = "ffffff"; // RGB value.
  26. int currentLevel = 0;  // Current dimmer level.
  27. MyMessage dimmerMsg(CHILD_ID, V_PERCENTAGE);
  28. MyMessage lightMsg(CHILD_ID, V_STATUS);
  29. MyMessage rgbMsg(CHILD_ID, V_RGB);
  30.  
  31. long long r2 = 0;
  32. long long g2 = 0;
  33. long long b2 = 0;
  34.  
  35. void setup()
  36. {
  37.   gw.begin(incomingMessage);
  38.   gw.sendSketchInfo(SN, SV);
  39.   gw.present(CHILD_ID, S_RGB_LIGHT);
  40.   gw.present(CHILD_ID, S_DIMMER);
  41.   gw.present(CHILD_ID, S_LIGHT);
  42.   // Send initial values.
  43.   gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));
  44.   gw.send(dimmerMsg.set(currentLevel));
  45.   gw.send(rgbMsg.set(rgb));
  46.  
  47.   Serial.begin(9600);
  48.   strip.begin();
  49. }
  50.  
  51. void loop()
  52. {
  53.   gw.process();
  54. }
  55.  
  56. void incomingMessage(const MyMessage &message) {
  57.   if (message.type == V_RGB) {
  58.     // Retrieve the RGB value from the incoming message.
  59.     // RGB LED not implemented, just a dummy print.
  60.     String hexstring = message.getString();
  61.     hexstring.toCharArray(rgb, sizeof(rgb));
  62.     Serial.print("Changing color to ");
  63.     Serial.println(rgb);
  64.     gw.send(rgbMsg.set(rgb));
  65.    
  66.     long long number = strtol( &hexstring[0], NULL, 16);
  67.  
  68.     // Split them up into r, g, b values
  69.     long long r = number >> 16;
  70.     long long g = number >> 8 & 0xFF;
  71.     long long b = number & 0xFF;
  72.  
  73.     for(int i=0;i<30;i++){
  74.  
  75.       // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
  76.       strip.setPixelColor(i, strip.Color(r,g,b)); // Moderately bright green color.
  77.  
  78.       strip.show(); // This sends the updated pixel color to the hardware.
  79.       delay(50);
  80.     }
  81.    
  82.    
  83.    
  84.     r2 = r;
  85.     g2 = g;
  86.     b2 = b;
  87.   }
  88.  
  89.  
  90.   if (message.type == V_STATUS || message.type == V_PERCENTAGE) {
  91.     // Retrieve the light status or dimmer level from the incoming message.
  92.     int requestedLevel = atoi(message.data);
  93.  
  94.     // Adjust incoming level if this is a V_LIGHT update [0 == off, 1 == on].
  95.     requestedLevel *= (message.type == V_STATUS ? 100 : 1);
  96.    
  97.     /*if (message.type == V_STATUS && message.data == "1") {
  98.         requestedLevel = 100;
  99.     }*/
  100.    
  101.     // Clip incoming level to valid range of 0 to 100
  102.     requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
  103.     requestedLevel = requestedLevel < 0   ? 0   : requestedLevel;
  104.    
  105.     int requestedLevelMapped = map(requestedLevel,0,100,0,255);
  106.  
  107.     //strip.setBrightness(requestedLevelMapped);
  108.     //strip.show();
  109.    
  110.     // Change level value of LED pin.
  111.    
  112.     if (currentLevel < requestedLevelMapped) {
  113.       if (currentLevel == 0) {
  114.         strip.setBrightness(1);
  115.         strip.show();
  116.         currentLevel = 10;
  117.         for(int i=0;i<30;i++){
  118.  
  119.           // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
  120.           strip.setPixelColor(i, strip.Color(r2,g2,b2)); // Moderately bright green color.
  121.  
  122.           strip.show(); // This sends the updated pixel color to the hardware.
  123.  
  124.         }
  125.         for(int i=currentLevel;i<=requestedLevelMapped;i++){
  126.             strip.setBrightness(i);
  127.             strip.show();
  128.             delay(10);
  129.         }
  130.       } else {
  131.         for(int i=currentLevel;i<=requestedLevelMapped;i++){
  132.             strip.setBrightness(i);
  133.             strip.show();
  134.             delay(10);
  135.         }
  136.       }
  137.     } else {
  138.       for(int i=currentLevel;i>=requestedLevelMapped;i--){
  139.         strip.setBrightness(i);
  140.         strip.show();
  141.         delay(10);
  142.       }
  143.     }
  144.  
  145.    currentLevel = requestedLevel;
  146.  
  147.     // Update the gateway with the current V_STATUS and V_PERCENTAGE.
  148.     gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));
  149.     gw.send(dimmerMsg.set(currentLevel));
  150.    
  151.     currentLevel = requestedLevelMapped;
  152.    
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement