Arduino code 2 servos

Hello everyone,

I have done the next code but there is a misstake somewhere because the servos don't work but I can't find it...
I want to move the servo1 from 0 degrees to 30 degrees, in the same time, the servo2 from 45 degrees to position 15.
Then servo2 from 15º to 35º.
After that, servo1 from 30º to 0º and servo2 from 35º to 45º.

#include <Servo.h>
int pos1 = 0;
int pos2 = 0;
Servo servo1;
Servo servo2;
void setup()
{


  servo1.attach(9);
  servo2.attach(10);
}

void loop()
{
  for(pos1 = 0; pos1 < 30; pos1++)
  for(pos2 = 45; pos2 > 15; pos2--)
  { 
    servo1.write(pos1);
    servo2.write(pos2);
    delay(67);
    {
      for(pos2 = 15; pos2 < 35; pos2++)
      servo2.write(pos2);
      delay(67);
    }
    {
      for(pos1 = 30; pos1 > 0; pos1--)
      for(pos2 = 35; pos2 < 45; pos2++)
      servo1.write(pos1);
      servo2.write(pos2);
      delay(67);
    }
  }
}

Your topic does not relate to version 2 of the IDE specifically so has been moved to the Programming category of the forum

Always show us a good schematic of your proposed circuit.

Show us good images of your ‘actual’ wiring.


Add some serial print statements in the code to prove variables are what you think they are.


Does this make sense ?


  for(pos2 = 45; pos2 > 15; pos2--)
  { 
    servo1.write(pos1);
    servo2.write(pos2);
    delay(67);
    {
      for(pos2 = 15; pos2 < 35; pos2++)
      servo2.write(pos2);
      delay(67);
    }
    

How are the servos powered.

Powering one servo from an Arduino is iffy. Powering two servos from an Arduino is a recipe for frustration.

What´s the task of this sketch in the real world?

first servo degree difference 30° - 0° = 30
second servo degree difference 45° - 15° = 30
which means the same amount of degree per time-unit

after that

first servo: 30 - 0 = 30
second servo 45 - 35 = 10
which means different amount of degrees per time-unit
first servo does 3 times more
which means in a loop
servo1--
servo2: if servo1 = 27 servo2--
servo2: if servo1 = 24 servo2--
.....
for checking is servo1_angle 30, 27, 24 ...
you can use the modulo-operator
servo1_angle % 3 == 0
best regards Stefan

This does not work as you suppose. The two for loops are NOT executed in parallel, but one inside the other. That means for every loop step in the outer loop, the inner for loop is completely executed.
You may consider to use a library that can move the servos slowly by itself, e.g. my MobaTools library. Then you need no for loops to move the servos slowly. And the servos can move really at the same time.

Both servos with an external 12v battery and the arduino with another 5v battery.
I'm building a robotic arm.

Hello @MicroBahner

I have checked your servo examples in MobaTools but I can't find the example with more than one servo.
Do you have any example to control two servos in parallel movement?

In the mobatools you set the servospeed prior to the servo-move.
Then you call a single time the final position
and the slowly moving is done by the library in the background

best regards Stefan

Yes, unfortunately there is no such example. I will create one for the next version.
But it's really straightforward: Simply create to servo instances and execute to servo.write() statements directly one after the other. The servos will move at the same time. Be aware, that the write()-statements are non blocking. Your sketches continues to run while the servos are moving. But you can ask in your sketch wether the servos have reached their target position.

A simple example with your values:

#include <MobaTools.h>
int pos1 = 0;
int pos2 = 0;
MoToServo servo1;
MoToServo servo2;
void setup()
{


  servo1.attach(9);
  servo1.setSpeedTime( 4000 );  // ms to move from 0 ... 180
  servo2.attach(10);
  servo2.setSpeedTime( 3000 );  // ms to move from 0 ... 180
  // Move servos to inital position ( this will always be done in max speed! )
  servo1.write(0);
  servo2.write(45);
}

void loop()
{
  // simple example to move servos in parallel:
  servo1.write(30);
  servo2.write(15);
  // write() is not blocking, so wait till servos reach target position
  while ( servo1.moving() || servo2.moving() );
  delay(100);
  servo2.write(35);
  while ( servo2.moving() );
  delay(100);
  servo1.write(0);
  servo2.write(45);
  while ( servo1.moving() || servo2.moving() );
  // 1 second delay before starting over
  delay( 1000 );

}

Thank you so much @MicroBahner !!
This has been very helpfully and the servos works as I wished jejeje

Can I add in the MobaTools the control of the servos by bluetooth? Like in the servo library?

I'm not quite shure what you mean by this. Can you give an example?

What I meaning is if I include the bluetooth HC-05 will work with the include MobaTools?

#include <MobaTools.h>
#include <SoftwareSerial.h>


MoToServo servo1;
MoToServo servo2;

int Pos1 = 0;
int Pos2 = 0;

SoftwareSerial bt_serial(3,2);

void setup()
{

I never tried this, but I don't think it's a problem. You can safely try. If it works with servo.h it should work with MobaTools too. Both use the same HW-components of an AVR Arduino.

Including multiple libraries is possible.
A problem would only arise if two libraries try to use the same microcontroller-internal hardware.

Example: Servo/MobaTools use timer1 and IR-send/receive uses timer1.

This would be a conflict because both want to use the same internal "tool"

As an example for multiple includefiles

#include <SafeString.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#include <time.h>                   // time() ctime()
#include <Wire.h>
#include <SDP6x.h>
#include <ESPDashPro.h>
#include <WebSerialPro.h>

best regards Stefan

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