Basic question but I've not seen it asked correctly and thusly so, answered correctly. I'll quickly give the basic project details for context.
I have a squirt gun turret with an X and Y axis of rotation. The X and Y coordinates are given by an outside system, but the systems target can move quickly so getting to the desired target coordinates as fast as possible is important.
So my base question here is: Can you PMW 2 pins at the same time? With the intended goal of moving 2 servos "at the same time".
I've seen lots of people saying you can use a "for loop" to increment 1 degree at a time to get to the desired location but that is just a cosmetic effect, and not in fact moving 2 servos at once but rather moving each one a tiny bit "one at a time". If both servos were getting their PMW signal at the same time, they would get to the end location faster.
The "for loop" solution basically gets you to the end location at X movement time + Y movement time. Where 2 signals at the same time would get you to the end location when the greater of the two was reached.
So if your goal was X40 Y60, the "for loop" would take (generalizing here) 40 + 60 ticks.
Where as, if they were both getting a PMW signal at once you could get to the end goal by 60 ticks as the 40 ticks from X axis independently reached its goal as the 60 ticks from the Y axis was running. Meaning it got to the end goal by the larger of the X and Y coordinates, instead of the total of both.
I've seen confusing, conflicting answers on this. The Uno is single core, but I've read that its more complicated than that with hardware timers and other stuff over my head, hence why I'm here asking. Just trying to refine the question from "Can you run 2 servos simultaneously to can you output 2 PMW signals at the same time?
Right now my "not so elegant" solution is to simply run 2 Arduinos at the same time, one for each servo. But I use most of my projects as learning tools so I want to know if there is a smarter way to dictate the coords to 2 servos at the same time than using a cosmetic "for loop" or 2 Arduinos.
Hardware solutions count too. I haven't looked into Adafruit shields much. That's another can of worms.
No. saying:
Servo X40
Servo Y60
Will move the servo to 40, then move Y to 60, in an L shape.
And again the "for loop" solution would make it diagonal but slow, still moving one at a time.
send servo 1 1 degree then send servo 2 one degree, the servo1, then servo2 and so on and so froth till the servos have reached their destination.
If using a ESP32 which has 2 cores and a multitasking OS, both motors can be moved at the same time when the servo move tasks are put on separate cores.
What are you talking about? Servo Movement or plotter linke system with 2 Stepper motors and x-y positioning?
A servo is NOT controlled by a PWM signal. A servo is controlled by a pulse of specific length ( 1..2ms ), which repeats every 20ms. The length of the pulse is important, not the pulse/pause ratio. With this in mind, you can change a servo's position only every 20ms. That's plenty of time for a Arduino to do this with multiple servos.
Well ok, in fact it is a PWM signal. But usually here in the forum PWM is seen as a signal where the pulse/pause ratio is important, like controlling the brightness of a LED or the speed of a DC motor. But this is not the case for a servo. The position is not controlled by the pulse pause ratio but only by the pulse width. The repetition time is usually 20ms, but can be more or less ( within limits, of course ) without affecting the position.
The main point reagarding the question in this thread is, that you can change a servo's position only once per 20ms. So it's easy for an Arduino to do it for more than one servo.
Actually, the main point of this thread is asking, can the Arduino PMW 2 or more pins at the same time. Which is why I mentioned it several times and put it in bold.
Yes. An Uno can run 12 of them. The loops you have seen are for when you need to control how fast your servos move but you can just tell the servo where to go and it will start moving. Microseconds later you command the second one and it will move too.
Watching it this morning it was running X to position before starting Y. I'm not using the full 180° range so maybe it just doesn't have enough time to start the next command before the first physically finishes. I'll have to keep playing with it I guess.