servo motors not working properly

Hi, I hope someone can help me with this. I am trying to operate my servo motors wirelessly through a Bluetooth module. I am also trying to operate the servos without the myservo library. I am using the Hitec 322HD servo. I have copied the sketch of my code and would like some feedback on what I am doing wrong.
Please Help!

const byte servopin = 10;
int currentPosition = 600;
int commandPosition = 600;
int Period = (20*1000);
byte rotateSpeed = 1;
int increment = 2;

void setup(){ //void setup start
Serial.begin(115200);
pinMode(servopin,OUTPUT);
}

void rotate(){
digitalWrite(servopin,HIGH);
delayMicroseconds(currentPosition);
digitalWrite(servopin,LOW);
delayMicroseconds(Period - currentPosition);
}

void loop(){
int data = Serial.read();
if (Serial.available() > 0) { //if serial available start
int data = Serial.read();

if (data ==‘a’){
commandPosition = 600;
}
if (data ==‘s’){
commandPosition = 1050;
}
if (data ==‘d’){
commandPosition = 1500;
}
if (data ==‘f’){
commandPosition = 1950;
}
if (data ==‘g’){
commandPosition = 2400;
}
}// if serial.available end

/******************************************
Set of command to assign value from
commandPosition to currentPosition
******************************************/
if (commandPosition > currentPosition){
currentPosition = (currentPosition + increment);
rotate();
}
if (commandPosition < currentPosition){
currentPosition = (currentPosition - increment);
rotate();
}
if (commandPosition == currentPosition){
currentPosition = commandPosition;
digitalWrite(servopin,LOW); // stop sending pulses
} // as soon as the desired position is
// reached

// Rotation speed
delay(rotateSpeed);brianh4

Posts: 1
Joined: 02 Feb 2014 15:06

servo_motor_control_code.ino (1.44 KB)

int data = Serial.read();
if (Serial.available() > 0) { //if serial available start
int data = Serial.read();

No.
Not sensible.
Lose the first read.

Please remember to use code tags when posting code.

Thanks for the advice, but I have removed the first Serial.read line of code, and I am still having the same problem.

No, I think you’re having a different problem, it just looks a bit like the first problem.

Any good reason for not using the Servo library?

What does your code look like now?

A servo won’t work just by setting the signal pin high. A servo needs a series of pulses of variable width between about 1 and 2 millisecs repeated about once every 50 msecs. The pulse width determines the position of the servo.

That’s why you use the servo library.

…R

A servo won’t work just by setting the signal pin high.

Psst. Function “rotate()”

I am also using 2 dc motors. The code I posted was for me to just get the servos working properly, and then integrate it into my overall code. Whenever I used the servo library, it was causing all of my digital pins (including the pins for my dc motors) to pulse width from 0 to 5V kind of like a square wave. This was causing my motors to not work. So, I was going to eliminate the servo library, and turn the servos without it. I have attached the updated code. the only thing different is the elimination of that first Serial.read () line. I appreciate the help guys.

servo_motor_control_code.ino (1.41 KB)

russdiculous1234:
I am also using 2 dc motors. The code I posted was for me to just get the servos working properly, and then integrate it into my overall code. Whenever I used the servo library, it was causing all of my digital pins (including the pins for my dc motors) to pulse width from 0 to 5V kind of like a square wave. This was causing my motors to not work. So, I was going to eliminate the servo library, and turn the servos without it. I have attached the updated code. the only thing different is the elimination of that first Serial.read () line. I appreciate the help guys.

Treating your symptom by trying to not use the servo library most likely just changes one problem for another. You must use the servo library (or one you write that does all the same things) to control servos, period. As for servo library causing problems on other pins there may be a conflict you might want to look into timer conflicts as the servo library does take over timer(s) as required and may cause conflicts with using some functions on other pins?

Lefty

AWOL:

A servo won't work just by setting the signal pin high.

Psst. Function "rotate()"

void rotate(){
digitalWrite(servopin,HIGH);
delayMicroseconds(currentPosition);
digitalWrite(servopin,LOW);
delayMicroseconds(Period - currentPosition);
}

What have I missed?

And its quite likely I did miss something!

...R

"..also using 2 DC motors..."

Um - how are you driving the motors exactly? The Arduino will not be able to do that alone unless they are tiny pager motors.

Also DC brushed motors are extremely electrically noisy which can affect all downstream circuits.