Hi guys,
I am trying to move a 9g servo modified for 360 rotation. I want that the servo rotate forward for 5 seconds, then stop for 5 seconts and finally rotate backwards for 5 seconds and repeat the whole sequene.
The code I am using is as follows:
int PWMvalue = 0;
int DCmotorpin = 9;
void setup()
{
pinMode(DCmotorpin, OUTPUT);
randomSeed(analogRead(0));
}
void loop()
{
PWMvalue = random(0, 250);
analogWrite(DCmotorpin, PWMvalue);
delay(5000);
PWMvalue = random(0, 0);
analogWrite(DCmotorpin, PWMvalue);
delay(1000);
PWMvalue = random(0, -250);
analogWrite(DCmotorpin, PWMvalue);
delay(5000);
}
The problem is that instead of getting a forward-stop-backwards rotation sequence I get five or six rotations forward then one backwards or two and so on with no apparent order...
Is there a problem with the 9g modified servo (just mechanical mods) or with the code? Help please.
No, you are way off base. You can't control servos with arduino analogWrite() commands, the PWM frequency is all wrong. You use the arduino Servo library functions to setup and control servos, and issue servo.write() commands to move the servos. For a servo modified for continuous rotation a servo.write(90) will cause it to stop, a servo.write(0) will be max speed in one direction and servo.write(180) will be max speed in the opposite direction.
http://arduino.cc/en/Reference/ServoLefty