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 relaysMy 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!
-
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 relaysMy 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!
-
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 changedigitalWrite(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(); } -
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 changedigitalWrite(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(); } -
@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); }```
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