Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Hardware
  3. Multiple Servos

Multiple Servos

Scheduled Pinned Locked Moved Hardware
9 Posts 3 Posters 3.1k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • numanxN Offline
    numanxN Offline
    numanx
    wrote on last edited by
    #1

    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!

    gohanG 1 Reply Last reply
    0
    • numanxN numanx

      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!

      gohanG Offline
      gohanG Offline
      gohan
      Mod
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • numanxN Offline
        numanxN Offline
        numanx
        wrote on last edited by
        #3

        Individually, thanks for your reply!

        1 Reply Last reply
        1
        • gohanG Offline
          gohanG Offline
          gohan
          Mod
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • numanxN Offline
            numanxN Offline
            numanx
            wrote on last edited by
            #5

            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();
            }
            
            
            gohanG 1 Reply Last reply
            0
            • numanxN numanx

              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();
              }
              
              
              gohanG Offline
              gohanG Offline
              gohan
              Mod
              wrote on last edited by
              #6

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

              numanxN 1 Reply Last reply
              1
              • gohanG gohan

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

                numanxN Offline
                numanxN Offline
                numanx
                wrote on last edited by
                #7

                @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

                1 Reply Last reply
                0
                • numanxN Offline
                  numanxN Offline
                  numanx
                  wrote on last edited by
                  #8

                  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); 
                  
                  }```
                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    anno
                    wrote on last edited by
                    #9
                    This post is deleted!
                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    12

                    Online

                    11.7k

                    Users

                    11.2k

                    Topics

                    113.1k

                    Posts


                    Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • MySensors
                    • OpenHardware.io
                    • Categories
                    • Recent
                    • Tags
                    • Popular