PLEASE PLEASE PLEASE HELP!!! SERVO PROBLEMS!!!

I have tried, through my WHOLE SUNDAY to get this code:

DO

  IF PULSES < 600 THEN
    GOTO increment
  ELSEIF PULSES > 1150 THEN
    GOTO decrement
  ENDIF

increment:
PULSES = 600
DO
  PULSES = PULSES + 25
  PULSOUT 15, PULSES
  PAUSE 20
LOOP UNTIL PULSES > 1150
RETURN
decrement:
PULSES = 1150
DO
  PULSES = PULSES - 25
  PULSOUT 15, PULSES
  PAUSE 20
LOOP UNTIL PULSES < 600
RETURN

LOOP

…And trun it into Arduino code. So far, I have been unsuccessful, and I already blew out a nice Hitec micro servo in the process. Does anyone in these forums have experience with PBASIC code? I need to get the arduino code to do what the PBASIC code is written to do. If not, I can explain how the PBASIC code works. By now though, I am pretty frustrated with my servo program. Can anyone help me??!! :’(

Here’s my (unsuccessful) Arduino code (ignore the commented out parts):

int servoPin = 3;
int irPin = 0;
int val = 0;
int pulses;

void setup() {
      Serial.begin(9600);
        pinMode(servoPin, OUTPUT);      
}

void loop()
{
  if(pulses < 1250)
    increment();
  else if(pulses > 2225)
    decrement();
    
  //val = analogRead(irPin);
  //delay(5);
  //Serial.println("PULSE");
  //Serial.println(pulses); 
  //delay(5);
  //Serial.println("IR"); 
  //delay(5);
  //Serial.println(val);
}   
void increment()
{
  //pulses = 1250;
  while(pulses > 1252)
  {
    pulses = pulses + 15;
    digitalWrite(servoPin, HIGH);
    delay(pulses);
    digitalWrite(servoPin, LOW);
    delay(20); 
  } 
    return;  
}  
void decrement()
{
  //pulses = 2225;
  while(pulses > 1252)
  {
    pulses = pulses - 15;
    digitalWrite(servoPin, HIGH);
    delay(pulses);
    digitalWrite(servoPin, LOW);
    delay(20);
  } 
    return;
}

delay(n) delays n milliseconds. In my servo code, I use delayMicroseconds(). Maybe you’re using the wrong function and your units are off?

-j

delay(n) delays n milliseconds. Maybe you’re using the wrong function and your units are off?
-j

kg4wsv is right, the pulse width should be in microseconds in your code. But the delay(20) after the pulse is correct, that should remain at 20 milliseconds.

pulses = pulses + 15;
digitalWrite(servoPin, HIGH);
delayMicroseconds(pulses); // this needs to be in microseconds
digitalWrite(servoPin, LOW);
delay(20); // this delay of 20 milliseconds is correct

I figured it out. Thanks, though. I need to start a new thread, though. thread closed

Here’s the code (with other stuff in it that I need help with. I’m going to address it in another thread).

int servoPin = 3;
int irPin = 0;
int val = 0;
int pulses;

void setup() {
  Serial.begin(9600);
  pinMode(servoPin, OUTPUT);      
}

void loop()
{
  if(pulses >= 2300)
    {  pulses = 2300;
      do 
      {
        pulses = pulses - 50;
        digitalWrite(servoPin, HIGH);
        delayMicroseconds(pulses);
        digitalWrite(servoPin, LOW);
        delay(20);
        readir();
      } while(pulses >= 1300);
    } 
  else if(pulses <= 1300)
    {  pulses = 1300;
      do 
      {
        pulses = pulses + 50;   
        digitalWrite(servoPin, HIGH);
        delayMicroseconds(pulses);
        digitalWrite(servoPin, LOW);
        delay(20);
        readir();
      } while(pulses <= 2300);
    }
}
void readir()
{
    val = analogRead(irPin);
    //delay(5);
    Serial.println("PULSE");
    delay(5);
    Serial.println(pulses); 
    delay(5);
    Serial.println("IR"); 
    delay(5);
    Serial.println(val);
}