Added detech functionality. Contribution Derek Macias
This commit is contained in:
@@ -3,6 +3,8 @@
|
|||||||
// The servo consumes much power and should probably have its own powersource.'
|
// The servo consumes much power and should probably have its own powersource.'
|
||||||
// The arduino might spontanally restart if too much power is used (happend
|
// The arduino might spontanally restart if too much power is used (happend
|
||||||
// to me when servo tried to pass the extreme positions = full load).
|
// to me when servo tried to pass the extreme positions = full load).
|
||||||
|
// Contribution by: Derek Macias
|
||||||
|
|
||||||
|
|
||||||
#include <Sensor.h>
|
#include <Sensor.h>
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
@@ -11,23 +13,27 @@
|
|||||||
#include <RF24.h>
|
#include <RF24.h>
|
||||||
|
|
||||||
#define SERVO_DIGITAL_OUT_PIN 3
|
#define SERVO_DIGITAL_OUT_PIN 3
|
||||||
#define SERVO_MIN 30 // Fine tune your servos min. 0-180
|
#define SERVO_MIN 0 // Fine tune your servos min. 0-180
|
||||||
#define SERVO_MAX 180 // Fine tune your servos max. 0-180
|
#define SERVO_MAX 180 // Fine tune your servos max. 0-180
|
||||||
|
#define DETACH_DELAY 900 // Tune this to let your movement finish before detaching the servo
|
||||||
|
|
||||||
// Set RADIO_ID to something unique in your sensor network (1-254)
|
// Set RADIO_ID to something unique in your sensor network (1-254)
|
||||||
// or set to AUTO if you want gw to assign a RADIO_ID for you.
|
// or set to AUTO if you want gw to assign a RADIO_ID for you.
|
||||||
#define RADIO_ID AUTO
|
#define RADIO_ID 6
|
||||||
#define CHILD_ID 10 // Id of the sensor child
|
#define CHILD_ID 10 // Id of the sensor child
|
||||||
|
|
||||||
Sensor gw(9, 10);
|
Sensor gw(9, 10);
|
||||||
Servo myservo; // create servo object to control a servo
|
Servo myservo; // create servo object to control a servo
|
||||||
// a maximum of eight servo objects can be created Sensor gw(9,10);
|
// a maximum of eight servo objects can be created Sensor gw(9,10);
|
||||||
|
unsigned long timeOfLastChange = 0;
|
||||||
|
bool attachedServo = false;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(BAUD_RATE);
|
Serial.begin(BAUD_RATE);
|
||||||
gw.begin(RADIO_ID);
|
gw.begin(RADIO_ID);
|
||||||
myservo.attach(SERVO_DIGITAL_OUT_PIN);
|
|
||||||
|
|
||||||
// Register all sensors to gw (they will be created as child devices)
|
// Register all sensors to gw (they will be created as child devices)
|
||||||
gw.sendSensorPresentation(CHILD_ID, S_COVER);
|
gw.sendSensorPresentation(CHILD_ID, S_COVER);
|
||||||
@@ -44,28 +50,37 @@ void loop()
|
|||||||
message_s message = gw.getMessage();
|
message_s message = gw.getMessage();
|
||||||
setRelayStatus(message);
|
setRelayStatus(message);
|
||||||
}
|
}
|
||||||
|
if (attachedServo && millis() - timeOfLastChange > DETACH_DELAY) {
|
||||||
|
myservo.detach();
|
||||||
|
attachedServo = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setRelayStatus(message_s message) {
|
void setRelayStatus(message_s message) {
|
||||||
|
myservo.attach(SERVO_DIGITAL_OUT_PIN);
|
||||||
|
attachedServo = true;
|
||||||
if (message.header.type==V_DIMMER) { // This could be M_ACK_VARIABLE or M_SET_VARIABLE
|
if (message.header.type==V_DIMMER) { // This could be M_ACK_VARIABLE or M_SET_VARIABLE
|
||||||
int val = atoi(message.data);
|
int val = atoi(message.data);
|
||||||
myservo.write(SERVO_MIN + (SERVO_MAX-SERVO_MIN)/100 * val); // sets the servo position 0-180
|
myservo.write(SERVO_MAX + (SERVO_MIN-SERVO_MAX)/100 * val); // sets the servo position 0-180
|
||||||
// Write some debug info
|
// Write some debug info
|
||||||
Serial.print("Servo changed. new state: ");
|
Serial.print("Servo changed. new state: ");
|
||||||
Serial.println(val);
|
Serial.println(val);
|
||||||
} else if (message.header.type==V_UP) {
|
} else if (message.header.type==V_UP) {
|
||||||
Serial.println("Servo UP command");
|
Serial.println("Servo UP command");
|
||||||
myservo.write(SERVO_MAX);
|
myservo.write(SERVO_MIN);
|
||||||
gw.sendVariable(CHILD_ID, V_DIMMER, 100);
|
gw.sendVariable(CHILD_ID, V_DIMMER, 100);
|
||||||
} else if (message.header.type==V_DOWN) {
|
} else if (message.header.type==V_DOWN) {
|
||||||
Serial.println("Servo DOWN command");
|
Serial.println("Servo DOWN command");
|
||||||
myservo.write(SERVO_MIN);
|
myservo.write(SERVO_MAX);
|
||||||
gw.sendVariable(CHILD_ID, V_DIMMER, 0);
|
gw.sendVariable(CHILD_ID, V_DIMMER, 0);
|
||||||
} else if (message.header.type==V_STOP) {
|
} else if (message.header.type==V_STOP) {
|
||||||
Serial.println("Servo STOP command");
|
Serial.println("Servo STOP command");
|
||||||
// Servo is pretty fast.. don't think we have time to send a stop
|
myservo.detach();
|
||||||
}
|
attachedServo = false;
|
||||||
|
|
||||||
|
}
|
||||||
|
timeOfLastChange = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user