Notifications
Clear all

Problem with Servo code

7 Posts
3 Users
0 Likes
1,323 Views
(@sj_h1)
Member
Joined: 3 years ago
Posts: 167
Topic starter  

I am having an issue with getting my servos to work. I have written the following sample to demostrate the problem. HELP!!!! The servo does not move in the code in set up or the 1st part of the loop. It does work in the for loop however. Any ideas why?

 

/* 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 myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int servoPin = 2; //pin servo contol is plugged into
int pos = 0; // variable to store the servo position

void setup() {
myservo.attach(servoPin); // attaches the servo on pin servoPin to the servo object

Serial.begin(9600);

Serial.println("Set servo to 0");
myservo.write(0); // tell servo to go to position 0
delay(5000);
Serial.println("Set servo to 180");
myservo.write(180); // tell servo to go to position 180
delay(5000);

}

void loop() {
Serial.println("In Loop");

pos = 90;

Serial.println("Set servo to 90");
myservo.write(pos); // tell servo to go to position 0
delay(5000);

Serial.println("Set servo to 0");
pos = 0;
myservo.write(pos); // tell servo to go to position 180
delay(5000);

Serial.println("In for loop");
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}


   
Quote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2037
 

   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2504
 

Try using a PWM pin like 3 or 9.

And also, you may need to use an auxiliary 5V source to power the servo.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2504
 

@robotbuilder

It's off-topic, but can you please tell me how you got realistic looking, formatted code in your post ? I've tried using the specified method and have failed every time.

You seem to have the magic formula.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2037
 

If you are using the Arduino IDE you first select the source code in that IDE with,
Right mouse click, Select All
Right mouse click, Copy as HTML

Then you go to your post. Where you want to insert the code tap the Enter key a few times to produce some <p></p> between which to insert the code. Then in the top bar of the post you are inserting it into select {;} and you will see the <p></p> 's. Between the <p> and the </p> right click mouse and Paste.

By the way in the source code I used,

int servoPin = 9;

 


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2504
 

@robotbuilder

Excellent !

 

Thank you very much.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@sj_h1)
Member
Joined: 3 years ago
Posts: 167
Topic starter  

OK I will try using a separate power supply for the servo since it has been verified thst the code should work.


   
ReplyQuote