Help with this code please.

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.

biotech:
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.

Lefty

Hi biotech..
Why are you using PWM for servo??
Have you already know how servos work?
I suggest you to use servo library, or you can write your own code, but not using PWM.

You may want to read this first before you write your own servo code :
http://www.servocity.com/html/how_do_servos_work_.html

Or you can use this library :

Hi,

I just thoght that by making the mods to a servo for continuous rotation I would get a DC motor. So that´s why I was using PWM.

With the info you gave me I made the following code to do the forward-stop-backwards rotations:

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
myservo.write(180);
delay(15);
myservo.write(90);
delay(15);
myservo.write(0);
delay(15);
}

I still don´t get the movement I want as the servo only rotates in one direction now. What is wrong? Thanks!

biotech:
I just thoght that by making the mods to a servo for continuous rotation I would get a DC motor.

How did you modified the servo exactly?

Servos have IC circuit & a pot inside, while DC motor don't. Did you remove the pot & the circuit (PCB)? As far as I know, you still need motor driver (e.g L293D) to controlling direction & speed of a DC motor.

Even if you modified your servo for continuous rotation like usually people do (e.g : http://letsmakerobots.com/node/4873) , not all kind servos are easily modified.

Well, like I asked before, how did you modified the servo exactly?

Hi Karma,

The mods I did at first didn´t include steps 3, 4 and 5 from the instructions you sent me. Now I have done those steps as well (the work on the pot) but I still get the gears rotating continuously in one direction only.

I guess I didn´t modify the pot propertly. When I connect the servo to the microcontroller (spet 4) do I need to download some instruction so the servo goes to the central position?

Is there another way to do it as it seems I have problems finding the center position of the pot this way?

Thanks.

Those delays look very short. Try 3000 instead of 15, it'll give the servo a chance to show you what it's doing.

   myservo.write(180);             
   delay(15);
   myservo.write(90);             
   delay(15);
   myservo.write(0);           
   delay(15);

Most hobby servos read the signal line at a frequency around 50hz (about every 20 ms). You're switching the direction at a rate faster than the servo is likely reading. So, not only is the rate too fast for you to see what's happening, it's probably too fast even for the servo itself.

As wildbill recommended, try delays on the order of several seconds.