RGB Servo spot



  • This is little project I made while ago but just now converted it to mysensors (used to be controlled via bluetooth)
    2_1454273523371_image1 (4).JPG 1_1454273523371_image2.PNG 0_1454273523370_image3 (1).JPG

    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Servo.h>
    
    #define RED_PIN 3
    #define GREEN_PIN 5
    #define BLUE_PIN 6
    #define SERVO_H 7
    #define SERVO_V 8
    
    #define NODE_ID 5
    #define SERVO_RGB 0
    #define SKETCH_NAME "SERVO RGB"
    #define SKETCH_VERSION "1.0.0"
    #define NODE_REPEAT false
    #define FIRE_EFFECT 22
    
    Servo myservo_h;
    Servo myservo_v;
    MySensor gw;
    
    long RGB_values[3] = {0, 0, 0};
    float dimmer;
    //int r = 255;
    //int g = r - 80;
    //int b = 25;
    //int servh = 0;
    //int servv = 0;
    
    //MyMessage fireMsg(FIRE_EFFECT, V_LIGHT);
    
    void setup() {
    
      pinMode(RED_PIN, OUTPUT);
      pinMode(GREEN_PIN, OUTPUT);
      pinMode(BLUE_PIN, OUTPUT);
      pinMode(SERVO_H, OUTPUT);
      pinMode(SERVO_V, OUTPUT);
      gw.begin(incomingMessage, NODE_ID, NODE_REPEAT);
      gw.sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
      
      gw.present(SERVO_RGB, S_RGB_LIGHT, "Servo RGB", false);
      gw.present(SERVO_V, S_LIGHT, "Servo V", false);
      gw.present(SERVO_H, S_LIGHT, "Servo H", false);
      gw.request(SERVO_V, V_LIGHT);
      gw.request(SERVO_V, V_LIGHT);
      gw.request(SERVO_RGB, V_RGB);
    }
    
    void loop() {
      gw.process();
    
    }
    
    
    void incomingMessage(const MyMessage &message) {
    
      if (message.type == V_RGB) {
    
        String hexstring = message.getString();
        long number = (long) strtol( &hexstring[0], NULL, 16);
        RGB_values[0] = number >> 16;
        RGB_values[1] = number >> 8 & 0xFF;
        RGB_values[2] = number & 0xFF;
      }
      if (message.type == V_DIMMER) {
    
        if (message.sensor == SERVO_V) {
          myservo_v.attach(SERVO_V);
          myservo_v.write(map(message.getInt(), 0, 100, 72, 180));
          gw.wait(1000);
          myservo_v.detach();
        }
        if (message.sensor == SERVO_H) {
          myservo_h.attach(SERVO_H);
          myservo_h.write(map(message.getInt(), 0, 100, 0, 180));
          gw.wait(1000);
          myservo_h.detach();
        }
        if (message.sensor == SERVO_RGB) {
          dimmer = message.getInt();
          analogWrite(RED_PIN, int(RGB_values[0] * (dimmer / 100)));
          analogWrite(GREEN_PIN, int(RGB_values[1] * (dimmer / 100)));
          analogWrite(BLUE_PIN, int(RGB_values[2] * (dimmer / 100)));
        }
      }
    
      if (message.type == V_LIGHT) {
        if (message.sensor == SERVO_RGB) {
          if (message.getInt() == 0) {
            digitalWrite(RED_PIN, 0);
            digitalWrite(GREEN_PIN, 0);
            digitalWrite(BLUE_PIN, 0);
          }
          if (message.getInt() == 1) {
            analogWrite(RED_PIN, int(RGB_values[0] * (dimmer / 100)));
            analogWrite(GREEN_PIN, int(RGB_values[1] * (dimmer / 100)));
            analogWrite(BLUE_PIN, int(RGB_values[2] * (dimmer / 100)));
          }
        }
        if (message.sensor == SERVO_H) {
          if (message.getInt() == 0) {
            myservo_h.attach(SERVO_H);
            myservo_h.write(0);
            gw.wait(1000);
            myservo_h.detach();
          }
          if (message.getInt() == 1) {
            myservo_v.attach(SERVO_V);
            myservo_v.write(180);
            gw.wait(1000);
            myservo_v.detach();
          }
        }
        if (message.sensor == SERVO_H) {
          if (message.getInt() == 0) {
            myservo_h.attach(SERVO_H);
            myservo_h.write(0);
            gw.wait(1000);
            myservo_h.detach();
          }
          if (message.getInt() == 1) {
            myservo_v.attach(SERVO_V);
            myservo_v.write(180);
            gw.wait(1000);
            myservo_v.detach();
          }
        }
      }
    }
    
    


  • Hi,

    Very nice looking project. Do you have a list of the hardware you used and a schematic by any chance?


Log in to reply
 

472
Online

6.9k
Users

7.8k
Topics

82.9k
Posts

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.