Interesting, not my recollection of my experience at one time; that was 3 years ago. However, I see it in the code. Curious. Thanks for that.
Regardless, it's far better to set the position before attaching, unless 90 is your desired starting point. Because you will see a twitch, as our OP is seeing, when 90 is applied, then 0 is applied, as will happen when the servo interrupt wakes and applies that initial 90, then applies his 0 in the next 20 ms interval.
What doesn't work, a motor or a simulation? My advice was for a physical (REAL) motor.
If I had known you were playing with a "sim", I wouldn't have replied.
hi
thank you so much,
yeeeees it works in WokWi.
You definitly solve my problem.
I think from now I will work only with Wokwi.
Thank again.
Thank you everyone too.
You don't have to but it is interesting that the servo jitters like a real one would. I don't know if that was done on purpose or it's a bug in the simulator.
If you like wokwi stay with it but I think Tinkercad has more components you can use.
I just tried,
the servo motor doesn't move at the beginning like before, the bug is fixed,
but when I push button plus to move to 180 degres, it stops at about 140 degres,
not possible to go until 180 !!! it's strange.
But it's ok for me,
I give up definitly Tinkercad.
I don't want to lose my time in bugs.
But thank you sir to try to help me I appreciate.
I was playing with the numbers, the 2000 should be set to 2500. MonServo.attach(6, 500, 2500);
The numbers correspond to the minimum and maximum pulse width in microseconds. For most 180 degree servos 500 coresponds to 0 degrees and 2500 to 180 degrees and 1500 to 90 degrees. You can also use .writeMicroseconds( value) to move the servo
The numbers may be different for diferent servos.
If code talks, it says servos have 181 positions numbered 0 .. 180.
I've never seen anyone avoid setting a servo angle to 180.
This code from servo.h
void Servo::write(int value)
{
if(value < MIN_PULSE_WIDTH)
{ // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
if(value < 0) value = 0;
if(value > 180) value = 180;
value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX());
}
this->writeMicroseconds(value);
}
leaves only the perennial question of exzctly how bad using map() sucks in this case. Probably not too much.