I have made a good hardware composition where the servo is connected on the right way with the arduino uno. The problem that I encounter is that I can’t program in code so that the servo keeps turning in the same direction continuously. Can someone show some simple code to do this. For programming I can make the servo object, attach the servo object to the right pin but then I am stuck in the write method of the servo object. I searched the internet and found only how to make it turn forward and then back. That’s not what I need. It needs to keep turning in the same direction because it is for a car robot that I want to program the servo for.
Yeth. That co es back as a continuous rotation servo.
Use the code in @xfpd's simulation in #5 and your servo should go CW and CCW at two rates each, and be close to or actually stopped with the 90 degree button.
Or just
void loop() {
servoWrite(135); // spin one way
delay(2777); // for awhile
servoWrite(90); // stop, or nearly
delay(777); // for awhile
servoWrite(45); // spin the other way
delay(2777); // for awhile
}
The code that you posted in post above had this as result: turns slowly in a direction, turn fast in the same direction, stops and keeps repeating this 3-steps on and on.
Now get empirical. The number makes the servo turn. Numbers between 0 and 180 should make it go from very fast in one direction alla way to stopped and then to very fast in the other direction.
Your report makes no sense; 90 should be stopped, higher numbers for one direction faster as they are greater and greater than 90, lower numbers for the other direction, faster as the numbers are less and more less than 90.
Try just one servo.write() call by itself and tinker with the number.
void loop() {
servo.write(90); // between 0 and 180
}
With that simple code gives 90 me: turns in same direction(clockwise) continuously , slow speed first and then speeding up, go back to slow speed again and speeding up again, and so on. And so on.
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);
}