Servo not working properly

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
brianh4
 
Posts: 1
Joined: Sun Feb 02, 2014 11:06 am

Servo not working properly

Post by brianh4 »

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 have copied the sketch of my code and would like some feedback on what I am doing wrong.
Please Help!

Code: Select all

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);
Last edited by adafruit_support_bill on Sun Feb 02, 2014 8:20 pm, edited 1 time in total.
Reason: Please use the 'code' button when submitting code - click 'code' and paste your code between the tags.

User avatar
Franklin97355
 
Posts: 23912
Joined: Mon Apr 21, 2008 2:33 pm

Re: Servo not working properly

Post by Franklin97355 »

Put your code between code tags and define "properly"

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”