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 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
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.
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.
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?