Good Day
I have just started to use servos, have modified other sketches and played around with success. However I am just trying to write my own simple program and the only results i get it the serial monitor printing out at the top of every loop. I modified the sweep program to use the 2 servos , so I know I don't have a wiring or physical issue. Ca n someone please point out what I know has to be a simple mistake.
#include <Servo.h>
Servo myservo1, myservo2;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
myservo1.attach(5);
myservo2.attach(6);
myservo1.write(100);
myservo2.write(100);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println ("Top of loop");
myservo1.write(10);
delay(500);
myservo2.write(10);
delay(500);
myservo1.write(180);
delay(500);
myservo2.write(180);
delay(500);
}
Thank you for your time.
1 Like
How are you powering the servos?
Did the OP ever get the servo sweep program to work with one servo?
Post images of the project, please. Thanks for code in code tags.
Why using pins 5 and 6? Those pins are a bit different, "In Arduino Uno, PWM pins are 3, 5, 6, 9, 10 and 11. The frequency of the PWM signal on pins 5 and 6 will be about 980Hz and 490Hz on other pins. "
I used the Arduino for the 2 servo sweep test and all was well.
I moved the pins around to eliminate them as the problem. pins 5 and 6 were chosen for no particular reason.
Yes the sweep worked wit hone servo, I then modified it to 2 servos and it worked as well.
Is there a glaring error in my code or should it work?
Post your working two servo sweep code.
Can you mark the topic as solved the, please?
Here is the sweep code example provided with Arduino but modified for 2 servos.
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep
*/
#include <Servo.h>
Servo myservo1,myservo2;
// create servo object to control a servo
// twelve servo objects can be created on most boards
int pos1; // variable to store the servo position
int pos2;
void setup() {
Serial.begin(9600);
myservo1.attach(5); // attaches the servo on pin 5 to the servo object
myservo2.attach(6);
}
void loop() {
Serial.println("Start Loop");
for (pos1 = 0; pos1 <= 180; pos1 += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo1.write(pos1); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos2 = 180; pos2 >= 0; pos2 -= 1) { // goes from 180 degrees to 0 degrees
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
Serial.println(pos1);
for (pos2 = 0; pos2 <= 180; pos2 += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo2.write(pos2); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
for (pos1 = 180; pos1 >= 0; pos1 -= 1) { // goes from 180 degrees to 0 degrees
myservo1.write(pos1); // tell servo to go to position in variable 'pos'
delay(5); // waits 15 ms for the servo to reach the position
}
Serial.println(pos2);
delay (200);
Serial.println("End Loop");
delay(200);
}
Sorry for the miscommunication. I used the 2 servo sweep code to test my wiring, power supply and servos. It is the original code posted that won't work. It has to be something I am missing.
So, does your modified sweep work with the hardware setup you were using with your new code?
Yes, the setup works perfectly when i use the modified sweep program.
And does not work with the original posted program
Try longer delays - the sweep version has 900 milliseconds worth to go from 0 to 180.
Made the changes below, but no change in result
#include <Servo.h>
Servo myservo1, myservo2;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
myservo1.attach(5);
myservo2.attach(6);
myservo1.write(100);
delay (2500);
myservo2.write(100);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println ("Top of loop");
myservo1.write(10);
delay(2500);
myservo2.write(10);
delay(2500);
myservo1.write(180);
delay(2500);
myservo2.write(180);
delay(2500);
}
If you now load your modified sweep again, changing nothing else, does it work?
yes, with nothing changed it runs fine in sweep, and seems to run or try to run the voidsetup code with the original posted and just print out to serial, no servo movement after that
Not all servos can manage the full 180 degree range, though I would expect to get some movement followed by it straining at the end. Try a smaller range like 45 to 135.
To Awol's earlier point though, Servo problems are almost always power related.
Thank wild bill
Its easy enough to try out a separate power supply. I will give it a shot.