Skip to content
  • Low Power Servo Actuator

    Development servo arduino low power battery powered rf24
    8
    0 Votes
    8 Posts
    5k Views
    martinhjelmareM
    @hek Thanks for the comments. Sounds good. I'll report back when I have some progress.
  • Servo issues - Weird things happening!!

    Troubleshooting mysensors servo
    5
    0 Votes
    5 Posts
    4k Views
    M
    Just to let you know that I solved the issue. I found a library where you can control the speed and make sure it get to it's position. You can find it here: https://github.com/netlabtoolkit/VarSpeedServo I incorporated it in the Mysensor sketch and it works eventhoug it looks like it only goes like 90 degrees but it's enough for me : #include <MySensor.h> #include <SPI.h> #include <VarSpeedServo.h> // #include <Servo.h> #define SERVO_DIGITAL_OUT_PIN 3 #define SERVO_MIN 0 // Fine tune your servos min. 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 #define CHILD_ID 10 // Id of the sensor child MySensor gw; MyMessage msg(CHILD_ID, V_DIMMER); VarSpeedServo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created // Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created Sensor gw(9,10); unsigned long timeOfLastChange = 0; bool attachedServo = false; void setup() { // Attach method for incoming messages gw.begin(incomingMessage); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("Servo", "1.0"); // Register all sensors to gw (they will be created as child devices) gw.present(CHILD_ID, S_COVER); // Request last servo state at startup gw.request(CHILD_ID, V_DIMMER); } void loop() { gw.process(); if (attachedServo && millis() - timeOfLastChange > DETACH_DELAY) { myservo.detach(); attachedServo = false; } } void incomingMessage(const MyMessage &message) { myservo.attach(SERVO_DIGITAL_OUT_PIN); attachedServo = true; if (message.type==V_DIMMER) { // This could be M_ACK_VARIABLE or M_SET_VARIABLE int val = message.getInt(); myservo.write(SERVO_MAX + (SERVO_MIN-SERVO_MAX)/100 * val,255,true); // sets the servo position 0-180 // Write some debug info Serial.print("Servo changed. new state: "); Serial.println(val); } else if (message.type==V_UP) { Serial.println("Servo UP command"); myservo.write(SERVO_MIN,255,true); gw.send(msg.set(100)); } else if (message.type==V_DOWN) { Serial.println("Servo DOWN command"); myservo.write(SERVO_MAX,255,true); gw.send(msg.set(0)); } else if (message.type==V_STOP) { Serial.println("Servo STOP command"); myservo.detach(); attachedServo = false; } timeOfLastChange = millis(); }
  • 6 Votes
    19 Posts
    27k Views
    slartiS
    My ethernet gateway died and I made another one on a Raspberry Pi so I updated all of my sensors from 1.4 libraries to 2.2. Here's the updated sketch for these servo blind control nodes: // A sketch to control a servo with a button and MySensors messages //#define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #include <SPI.h> #include <MySensors.h> #include <Servo.h> #include <Button.h> //https://github.com/JChristensen/Button #define BUTTON_PIN 4 //Connect a tactile button switch (or something similar) //from Arduino pin 4 to ground. #define PULLUP true //To keep things simple, we use the Arduino's internal pullup resistor. #define INVERT true //Since the pullup resistor will keep the pin high unless the //switch is closed, this is negative logic, i.e. a high state //means the button is NOT pressed. (Assuming a normally open switch.) #define DEBOUNCE_MS 20 //A debounce time of 20 milliseconds usually works well for tactile button switches. #define LONG_PRESS_PERIOD 700 //How long to keep button pressed until sweeping starts #define MAX_DEGREES 180 //Servo limits. Whatever works for you. #define MIN_DEGREES 0 #define CHILD_ID 3 Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS); //Declare the button Servo myservo; enum {DECREASING, INCREASING}; //On a servo mounted on the left, with outer slat edge down as closed, // closing is going toward 180, opening toward 0 (on my fake futaba s3003's) boolean invertConversions = true; // false if opening toward 180 boolean servoDirection = INCREASING; //A variable that keeps the current servo direction int servoPin = 3; int servoPosition; int servoSpeed = 1; // The bigger, the faster. 1=slow 5=fast MyMessage msg(CHILD_ID, V_PERCENTAGE); void setup(void) { } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo("Servo", "1.3"); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID, S_COVER); } void loop(void) { myBtn.read(); //Read the button if (myBtn.wasReleased()){ //If the button was pressed once sweep to end of current direction SweepToDirectionEnd(); send(msg.set(ConvertDegToPercent(servoPosition))); } if (myBtn.pressedFor(LONG_PRESS_PERIOD)){ //If the button is held down the servo will start to sweep SweepUntilStop(); send(msg.set(ConvertDegToPercent(servoPosition))); } } int ConvertPercentToDeg(int percent) { int degree; if (invertConversions) degree = map(percent, 0, 100, MAX_DEGREES, MIN_DEGREES); if (!invertConversions) degree = map(percent, 0, 100, MIN_DEGREES, MAX_DEGREES); return degree; } int ConvertDegToPercent(int degree) { int percent; if (invertConversions) percent = map(degree, MAX_DEGREES, MIN_DEGREES, 0, 100); if (!invertConversions) percent = map(degree, MIN_DEGREES, MAX_DEGREES, 0, 100); return percent; } void receive(const MyMessage &message) { myservo.attach(servoPin); if (message.type==V_PERCENTAGE) { int val = message.getInt(); SweepToPosition(ConvertPercentToDeg(val)); //In this case the value has to be inverted because 0 = open send(msg.set(val)); } else if (message.type==V_STATUS) { if (message.getInt() == 1){ SweepToPosition(ConvertPercentToDeg(100)); send(msg.set(100)); } else if(message.getInt() == 0) { SweepToPosition(ConvertPercentToDeg(0)); send(msg.set(0)); } } else myservo.detach(); } void ServoMoveUp() { if ((myservo.attached()) && servoPosition < MAX_DEGREES){ servoDirection = INCREASING; servoPosition += servoSpeed; myservo.write(servoPosition); delay(10); Serial.print("Servo Position: "); Serial.println(servoPosition); } if (!myservo.attached()){ Serial.println("Servo stopped while moving toward MAX, direction unchanged"); delay(100 * servoSpeed); } if (servoPosition >= MAX_DEGREES){ Serial.println("MAX reached, changing direction toward MIN"); servoDirection = DECREASING; delay(100 * servoSpeed); // Wait for the last movement to finish } } void ServoMoveDown() { if ((myservo.attached()) && servoPosition > MIN_DEGREES){ servoDirection = DECREASING; servoPosition -= servoSpeed; delay(10); myservo.write(servoPosition); Serial.print("Servo Position: "); Serial.println(servoPosition); } if (!myservo.attached()){ Serial.println("Servo stopped while moving toward MIN, direction unchanged"); delay(100 * servoSpeed); } if (servoPosition == MIN_DEGREES){ Serial.println("MIN reached, changing direction toward MAX"); servoDirection = INCREASING; delay(100 * servoSpeed); } } void SweepToDirectionEnd() { myservo.attach(servoPin); if (servoDirection == INCREASING){ Serial.println("Going to MAX and stopping there"); while (servoPosition < MAX_DEGREES){ ServoMoveUp(); } delay(20 * servoSpeed); myservo.detach(); } else if (servoDirection == DECREASING){ Serial.println("Going to MIN and stopping there"); while (servoPosition > MIN_DEGREES){ ServoMoveDown(); } delay(20 * servoSpeed); myservo.detach(); } } void SweepUntilStop() { myservo.attach(servoPin); while (myBtn.isPressed()){ myBtn.read(); if (myBtn.isReleased()) myservo.detach(); if (servoDirection == INCREASING) ServoMoveUp(); if (servoDirection == DECREASING) ServoMoveDown(); } } void SweepToPosition(int destination) { if (abs(destination - servoPosition) >= servoSpeed) //Don't move if destination is close to position myservo.attach(servoPin); if (destination > servoPosition && myservo.attached()){ Serial.print("Going to "); Serial.print(destination); Serial.println(" and stopping there"); while (servoPosition < destination){ ServoMoveUp(); } delay(20 * servoSpeed); myservo.detach(); } if (destination < servoPosition && myservo.attached()){ Serial.print("Going to "); Serial.print(destination); Serial.println(" and stopping there"); while (servoPosition > destination){ ServoMoveDown(); } delay(20 * servoSpeed); myservo.detach(); } }```
  • 2 Votes
    11 Posts
    15k Views
    RJ_MakeR
    Very Cool -- Thanks

24

Online

11.7k

Users

11.2k

Topics

113.1k

Posts