Notifications
Clear all

Problem with Servo code


sj_h1
(@sj_h1)
Estimable Member
Joined: 12 months ago
Posts: 109
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)
Prominent Member
Joined: 2 years ago
Posts: 883
 

@sj_h1

It seemed to work for me?

1 starts at 0 fast move to 180

2 fast move to 90, fast move to 0

3 slow move to 180

4 slow move to 0

5 fast move to 90

 6 fast move to 0

GO TO 3

 

 

/* 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 = 9; //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
  }
}

 

loops2

 

 


ReplyQuote
Will
 Will
(@will)
Reputable Member
Joined: 3 months ago
Posts: 368
 

Try using a PWM pin like 3 or 9.

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


ReplyQuote
Will
 Will
(@will)
Reputable Member
Joined: 3 months ago
Posts: 368
 

@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.


ReplyQuote
robotBuilder
(@robotbuilder)
Prominent Member
Joined: 2 years ago
Posts: 883
 

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;

 

This post was modified 3 months ago by robotBuilder

ReplyQuote
Will
 Will
(@will)
Reputable Member
Joined: 3 months ago
Posts: 368
 

@robotbuilder

Excellent !

 

Thank you very much.


ReplyQuote
sj_h1
(@sj_h1)
Estimable Member
Joined: 12 months ago
Posts: 109
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