Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * NEEDS ADAFRUIT NEOPIXEL & MYSENSORS LIBRARIES INSTALLED.
- *
- *
- * Documentation: http://www.mysensors.org
- * Support Forum: http://forum.mysensors.org
- *
- * http://www.mysensors.org/build/dimmer
- */
- #include <MySensor.h>
- #include <SPI.h>
- #include <Adafruit_NeoPixel.h>
- #define SN "LED Strip"
- #define SV "1.0"
- #define CHILD_ID 38
- #define LED_PIN 5
- #define RGB_PIN 6
- // CHANGE THE LENGTH OF YOUR NEOPIXEL STRIP HERE. Default is 30.
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, RGB_PIN, NEO_GRB + NEO_KHZ800);
- MySensor gw;
- char rgb[7] = "ffffff"; // RGB value.
- int currentLevel = 0; // Current dimmer level.
- MyMessage dimmerMsg(CHILD_ID, V_PERCENTAGE);
- MyMessage lightMsg(CHILD_ID, V_STATUS);
- MyMessage rgbMsg(CHILD_ID, V_RGB);
- long long r2 = 0;
- long long g2 = 0;
- long long b2 = 0;
- void setup()
- {
- gw.begin(incomingMessage);
- gw.sendSketchInfo(SN, SV);
- gw.present(CHILD_ID, S_RGB_LIGHT);
- gw.present(CHILD_ID, S_DIMMER);
- gw.present(CHILD_ID, S_LIGHT);
- // Send initial values.
- gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));
- gw.send(dimmerMsg.set(currentLevel));
- gw.send(rgbMsg.set(rgb));
- Serial.begin(9600);
- strip.begin();
- }
- void loop()
- {
- gw.process();
- }
- void incomingMessage(const MyMessage &message) {
- if (message.type == V_RGB) {
- // Retrieve the RGB value from the incoming message.
- // RGB LED not implemented, just a dummy print.
- String hexstring = message.getString();
- hexstring.toCharArray(rgb, sizeof(rgb));
- Serial.print("Changing color to ");
- Serial.println(rgb);
- gw.send(rgbMsg.set(rgb));
- long long number = strtol( &hexstring[0], NULL, 16);
- // Split them up into r, g, b values
- long long r = number >> 16;
- long long g = number >> 8 & 0xFF;
- long long b = number & 0xFF;
- for(int i=0;i<30;i++){
- // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
- strip.setPixelColor(i, strip.Color(r,g,b)); // Moderately bright green color.
- strip.show(); // This sends the updated pixel color to the hardware.
- delay(50);
- }
- r2 = r;
- g2 = g;
- b2 = b;
- }
- if (message.type == V_STATUS || message.type == V_PERCENTAGE) {
- // Retrieve the light status or dimmer level from the incoming message.
- int requestedLevel = atoi(message.data);
- // Adjust incoming level if this is a V_LIGHT update [0 == off, 1 == on].
- requestedLevel *= (message.type == V_STATUS ? 100 : 1);
- /*if (message.type == V_STATUS && message.data == "1") {
- requestedLevel = 100;
- }*/
- // Clip incoming level to valid range of 0 to 100
- requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
- requestedLevel = requestedLevel < 0 ? 0 : requestedLevel;
- int requestedLevelMapped = map(requestedLevel,0,100,0,255);
- //strip.setBrightness(requestedLevelMapped);
- //strip.show();
- // Change level value of LED pin.
- if (currentLevel < requestedLevelMapped) {
- if (currentLevel == 0) {
- strip.setBrightness(1);
- strip.show();
- currentLevel = 10;
- for(int i=0;i<30;i++){
- // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
- strip.setPixelColor(i, strip.Color(r2,g2,b2)); // Moderately bright green color.
- strip.show(); // This sends the updated pixel color to the hardware.
- }
- for(int i=currentLevel;i<=requestedLevelMapped;i++){
- strip.setBrightness(i);
- strip.show();
- delay(10);
- }
- } else {
- for(int i=currentLevel;i<=requestedLevelMapped;i++){
- strip.setBrightness(i);
- strip.show();
- delay(10);
- }
- }
- } else {
- for(int i=currentLevel;i>=requestedLevelMapped;i--){
- strip.setBrightness(i);
- strip.show();
- delay(10);
- }
- }
- currentLevel = requestedLevel;
- // Update the gateway with the current V_STATUS and V_PERCENTAGE.
- gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));
- gw.send(dimmerMsg.set(currentLevel));
- currentLevel = requestedLevelMapped;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement