RGB Servo spot
-
This is little project I made while ago but just now converted it to mysensors (used to be controlled via bluetooth)

#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(); } } } }
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login