Multiple Servos



  • Hi there,
    Could someone help me with a sketch for running multiple servos.
    Like in the example for the Relay Actuator
    #define SERVO_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_SERVOS 1 // Total number of attached relays

    My current sketch is:

    
    // Debug
    #define MY_DEBUG
    // Partea cu Conexiunea la net
    #define MY_GATEWAY_ENC28J60
    #define MY_IP_ADDRESS 192,168,1,66
    #define MY_PORT 5003
    #define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
    #include <UIPEthernet.h>
    // Sfarsit parte
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Servo.h>
    
    #define SERVO_DIGITAL_OUT_PIN 4
    #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
    
    MyMessage msg(CHILD_ID, V_DIMMER);
    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;
    bool state;
    
    void setup()
    {
      // Request last servo state at startup
      request(CHILD_ID, V_DIMMER);
    }
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Servo", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID, S_DIMMER);
    }
    
    void loop()
    {
      if (attachedServo && millis() - timeOfLastChange > DETACH_DELAY) {
         myservo.detach();
         attachedServo = false;
      }
    }
    
    void receive(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 * 1.8 * val); // sets the servo position 0-180
         // Write some debug info
         Serial.print("Servo changed. new state: ");
         Serial.println(val);
         }
       timeOfLastChange = millis();
    }
    

    Thanks!


  • Mod

    @numanx but do you want to control each servo individually or all together at the same time?



  • Individually, thanks for your reply!


  • Mod

    You stripped a lot of code from the relay sketch, I think you need to put back all the FOR cycles it you want the code handle automatically the number of servos



  • Ok so here is the sketch with the for cycles.
    But it still won't work. The servos are presented to the Controller but it won't make any move.
    I think the problem is with the void receive.
    I don't know how to change

    	digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
    

    for the servo

         myservo.write(SERVO_MAX + (SERVO_MIN-SERVO_MAX)/100 * 1.8 * val); // sets the servo position 0-180
    
    // Debug
    #define MY_DEBUG
    // Partea cu Conexiunea la net
    #define MY_GATEWAY_ENC28J60
    #define MY_IP_ADDRESS 192,168,1,66
    #define MY_PORT 5003
    #define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
    #include <UIPEthernet.h>
    // Sfarsit parte
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Servo.h>
    
    #define SERVO_1 4 // Arduino Digital I/O pin number for first servo (second on pin+1 etc)
    #define NUMBER_OF_SERVOS 2 // Total number of attached servos
    #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
    
    //MyMessage msg(CHILD_ID, V_DIMMER);
    Servo myservo;  // create servo object to control a servo
    unsigned long timeOfLastChange = 0;
    bool attachedServo = false;
    bool state;
    
    void before()
    {
        for (int servo=1, pin_S=SERVO_1; servo<=NUMBER_OF_SERVOS; servo++, pin_S++) {
          myservo.attach(pin_S);
        }
    }
    
    void setup()
    {
      // Request last servo state at startup
        for (int servo=1, pin_S=SERVO_1; servo<=NUMBER_OF_SERVOS; servo++, pin_S++) {
          request(servo, V_DIMMER);
      }
    }
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Servo", "1.0");
        // Register all sensors to gw (they will be created as child devices)
      for (int servo=1, pin_S=SERVO_1; servo<=NUMBER_OF_SERVOS; servo++, pin_S++) {
          present(servo, S_DIMMER);
      }
    }
    
    void loop()
    {
      if (attachedServo && millis() - timeOfLastChange > DETACH_DELAY) {
         myservo.detach();
         attachedServo = false;
      }
    }
    
    void receive(const MyMessage &message) {
      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 * 1.8 * val); // sets the servo position 0-180
         // Write some debug info
         Serial.print("Servo changed. new state: ");
         Serial.println(val);
         }
       timeOfLastChange = millis();
    }
    
    

  • Mod

    @numanx start adding some debug prints and see where data is not correct



  • @gohan
    Debug print when changing the dimmer slider to different values.

    0;255;3;0;9;Eth: 0;0;3;0;2;
    0;255;3;0;9;Eth: 0;0;3;0;2;Get Version
    0;255;3;0;9;Eth: 0;1;1;1;3;7
    Servo changed. new state: 7
    0;255;3;0;9;Eth: 0;0;3;0;18;PING
    0;255;3;0;9;Eth: 0;1;1;1;3;21
    Servo changed. new state: 21
    0;255;3;0;9;Eth: 0;1;1;1;3;36
    Servo changed. new state: 36
    0;255;3;0;9;Eth: 0;0;3;0;18;PING
    0;255;3;0;9;Eth: 0;0;3;0;18;PING



  • Found this on arduino.cc forum.
    I think there should be a way for creating different servo objects for each servo but I don't know how to do that.

    Any sugestion from @mfalkvidd ?

    #include <Servo.h>
    
     Servo myservo[6]; 
      
    int pinAttch = 0;    // variable to store pin attachment
    int servWrite = 0;    // variable to store servo writing
    
    void setup() {
      Serial.begin(9600);
      for(pinAttch = 2; pinAttch < 7; pinAttch++)
        {                                                
        myservo[pinAttch].attach(pinAttch);
        }  
      }
    
    void loop() {
    
    for(servWrite = 2; servWrite < 7; servWrite++)
      {                                                
       
      myservo[servWrite].write(80);
      delay(2000);
    
      }
    exit(0); 
    
    }```

Log in to reply
 

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