type or paste code here
#include <Servo.h>
Servo myservo;
Servo myservo1;
void setup() {
myservo.attach(9);
myservo1.attach(10);
}
void loop() {
myservo.write(0); //set initial position
delay(15);
myservo1.write(0); // set initial position
delay(15);
for(int i=0;i<=160;i+=20){
myservo.write(i);
delay(15); // gives servo time to get there
delay(3000); //hold servo in position for 3 seconds
}
for(int x=0;x<=80;x+=20){
myservo1.write(x);
delay(15);
delay(3000);
}
myservo1.write(30);
delay(15);
delay(3000);
}
I was expecting the code to move the first servo(not servo1) from position zero to 160 and provide a delay of 3 seconds at each position and then go on to serv1 for positions 0-80 again with a delay of 3 seconds at each position. I wanted to slow down the action .
This code is not doing it. Can anybody suggest a code for doing this?
I see it... your comment was inside the code block.
You need to move the 3000ms delay outside the for() loop. It would be easier to recognize the movements if you change the steps from 20 to 1 and delete all the delay(3000); functions.
If I take the delay outside the for loop the servo will run through all the positions without a delay at each position. Anyway that code doesn't work so I have tried another code.
void setup() {
myservo.attach(9);
}
void loop() {
myservo.write(0); //set initial position
delay(15);
for(int i=0;i<=160;i+=20){ //disconnect servo at each position and reattach after 3 seconds
myservo.attach(9);
myservo.write(i);
delay(15);
myservo.detach(9);
delay(3000);
}
}
For this code I get an error message of no matching function for a call to 'myservo.detach9' There wasn't originally as I didn't have 'myservo.attach9' in the code. Now that I have it I am still getting the error message.
Here is the full error message;
Arduino: 1.8.13 (Windows 10), Board: "Arduino Nano, ATmega328P"
C:\Users\peter clarke\Documents\Arduino\testing_one_servo_motor_operation\testing_one_servo_motor_operation.ino: In function 'void loop()':testing_one_servo_motor_operation:16:17: error: no matching function for call to 'Servo::detach(int)' myservo.detach(9); ^In file included from C:\Users\peter clarke\Documents\Arduino\testing_one_servo_motor_operation\testing_one_servo_motor_operation.ino:1:0:C:\Program Files (x86)\Arduino\libraries\Servo\src/Servo.h:110:8: note: candidate: void Servo::detach() void detach(); ^~~~~~C:\Program Files (x86)\Arduino\libraries\Servo\src/Servo.h:110:8: note: candidate expects 0 arguments, 1 providedexit status 1no matching function for call to 'Servo::detach(int)'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
#include <Servo.h>
Servo servo1;
Servo servo2;
void setup() {
servo1.attach(9);
servo2.attach(10);
}
void loop() {
for (int i = 0; i < 60; i++) {
servo1.write(i);
servo2.write(i);
delay(10);
}
for (int i = 60; i > 0; i--) {
servo1.write(i);
servo2.write(i);
delay(10);
}
}
I have not been sure for some time now if the servo library was capable of operating more than 1 servo at a time. So I tested it with this code. Both servos worked. I had some weight on the servos and as the servos were moving so fast there was large jolts as the servos reached their positions. If I kept the power on too long I think the whole set up would have been destroyed.
Your suggestion to slow the servo. I believe I tried that with 'delay(3000)' and then the sketch did not work at all. I can try it again to see what exactly happens. If I can't slow down the servo movement the jolts at the end of the movement will wreck the whole thing.
Any suggestions to fix this would be appreciated.
all fixed. I have included delay(3000) instead of delay (15) inside the loop now. I am getting a delay of 3 seconds now at each servo position. I previously thought this was not working.
Now no chance of sudden jolts at the end of servo movement to destroy whole set up. Because there is only a small movement before each step instead of one long step using delay(15).
Right. The loop takes a number of steps and pauses between each step. And goes from wherever it was if not already at zero to 80 degrees. and takes 15 seconds.
Big steps with a big delay. Relatively speaking.
You could take more steps, and wait a shorter time, and the servo would move the same angle in the same time but smoothly… here's stepping by one degree at a time:
for (int x = 0; x <= 80; x++) {
myservo1.write(x);
delay(188);
}
// any pause you want here at the end angle
delay(777); // short pause not too short. whatever.
Check my maths. Since you aren't doing anything else, why not move the servo like it can?
BTW delay() takes an unsigned long integer which means you could (don't) use it to do nothing at all for a day. Or a week. Up to seven weeks or so. But don't.