Any idea how to control two servos one at a time|?

Right now i got two servos,servo A and servo B. I want servo B to run after servo A ends. This is the code i have right now. So the servos will run in a loop. I've tested it but servo B does not run after servo A. Is there anything i missed out?

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo2;
// twelve servo objects can be created on most boards

int pos = 10;    // variable to store the servo position
int pos1 = 10; 

void setup() {
  //myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  //myservo2.attach(10); 
}

void loop() 
{
  //myservo2.detach();
  //delay(100);
  
  myservo.attach(9);
  delay(100);
  
  for (pos = 10; pos <= 170; 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(50);                       // waits 15ms for the servo to reach the position
  }
  
  for (pos = 170; pos >= 10; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(50);                       // waits 15ms for the servo to reach the position
  }

  myservo.detach();
  delay(100);

  myservo2.attach(10);
  delay(100);

   for (pos1 = 5; pos1 <= 175; pos1 += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo2.write(pos1);              // tell servo to go to position in variable 'pos'
    delay(50);                       // waits 15ms for the servo to reach the position
  }
  for (pos1 = 175; pos1 >= 5; pos1 -= 1) { // goes from 180 degrees to 0 degrees
    myservo2.write(pos1);              // tell servo to go to position in variable 'pos'
    delay(50);                       // waits 15ms for the servo to reach the position
  }
  myservo2.detach();
  delay(1000);
}

The board used is an arduino nano. The gnd of nano is connected to the gnd of the servos. The reason for running a servo one at a time is due to power consumption. I am making a dual axis solar tracker project. However I want it to reduce the total power consumption at a time, hence I want to divide the power consumption into two parts. For additional info, there is a forum chat i had going on(do forgive me if this is a duplicate question) but for time sake, the specs of my system,

  • 10W solar panel , 17.5V (able to give 1.1A max , tested outdoor by myself with a buck converter)

  • 5V buck

  • Arduino nano and two 946R servos

As you can see from the output available, only a mere 1A-ish. You can proceed to my other chat question for more info and ask for additional information there. Thanks! and stay safe!

Use tag for program code.
The program looks fine. I tried it in a wokwi simulator and it works as expected.

hmmm, wonder why my servo doesnt move. prob not enough power supplied

I hope you have not confused an Arduino with a power source. :roll_eyes:

the servos are powered not directly by arduino but by a 5V 2A powerbank. Only the signal pin is connected to the arduino. Thanks

Hi,

Are the gnds of the servo supply connected to the gnd of the Arduino?
What model Arduino are you using?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Its a arduino nano. Yes the gnd for servos and nano are together. cheers!

There's the trick. You have to be very specific in explaining things, otherwise we may well suspect you missed something extremely obvious. :grin:

Forgive me. Let me add on the main question for future viewers.

Hi,
Try this version of your code.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo2;
// twelve servo objects can be created on most boards

int pos = 10;    // variable to store the servo position
int pos1 = 10;

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(10);
}

void loop()
{

  delay(100);

  for (pos = 10; pos <= 170; 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(50);                       // waits 15ms for the servo to reach the position
  }

  for (pos = 170; pos >= 10; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(50);                       // waits 15ms for the servo to reach the position
  }

  delay(100);

  for (pos1 = 5; pos1 <= 175; pos1 += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo2.write(pos1);              // tell servo to go to position in variable 'pos'
    delay(50);                       // waits 15ms for the servo to reach the position
  }

  for (pos1 = 175; pos1 >= 5; pos1 -= 1) { // goes from 180 degrees to 0 degrees
    myservo2.write(pos1);              // tell servo to go to position in variable 'pos'
    delay(50);                       // waits 15ms for the servo to reach the position
  }
  delay(1000);
}

Tom... :smiley: :+1: :coffee: :australia:

Hi tom, Thanks for your code version. i will try it asap and let you know . cheers!

hi tom, it seems like this code runs both at the same time. However , i want it to run one servo at a time

Not sure if this the most efficient way to do it , but it does the job!

#include <Servo.h>

Servo myservo;
Servo myservo2;// create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

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

void movemotorA()
{
myservo.attach(9);
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);
}
}

void movemotorB()
{
myservo2.attach(10);
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo2.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
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
}
void loop()
{
movemotorA();
delay(2000);
movemotorB();
exit(0); //exit loop after 1 iteration. You can remove if need to run infinite loops
}

Hi,
Try this;

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo2;
// twelve servo objects can be created on most boards

int pos = 10;    // variable to store the servo position
int pos1 = 10;

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(10);
}

void loop()
{

  delay(500);

  for (pos = 10; pos <= 170; 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(75);                       // waits 15ms for the servo to reach the position
  }

  for (pos = 170; pos >= 10; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(75);                       // waits 15ms for the servo to reach the position
  }

  delay(1000);

  for (pos1 = 5; pos1 <= 175; pos1 += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo2.write(pos1);              // tell servo to go to position in variable 'pos'
    delay(75);                       // waits 15ms for the servo to reach the position
  }

  for (pos1 = 175; pos1 >= 5; pos1 -= 1) { // goes from 180 degrees to 0 degrees
    myservo2.write(pos1);              // tell servo to go to position in variable 'pos'
    delay(75);                       // waits 15ms for the servo to reach the position
  }
  delay(1000);
}

Things happen a bit slower in this one.

Can you please post a picture of your project, so we can see your component layout?

Tom... :smiley: :+1: :coffee: :australia:

There is no reason you can't track sun by sequentially moving the steppers.
Azimuth changes at 15 degrees per hour so that one will move all day every few minutes, Elevation is only a once a day move if that. Move the Azimuth stepper first then the Elevation (if needed). no need to divide power anywhere,

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.