Arduino servo without library

As a training exercise in sending digital signals, I am trying to encode a pulse train for a servo without using the servo.h library.

The servo is a micro-servo for 9 g. The hardware is correct, as many examples of using servo.h work fine.

I have the following code. The problem is that the servo control jerks for 3 seconds rather than moving and holding still.

void loop() {
    movePulse_1000();
    delay(3000);
}

void movePulse_1000(){
    Serial.print("Start movePulse_1000()\t\t");
    for (int pulseCounter=0; pulseCounter<=150; pulseCounter++){
        digitalWrite(pinServo,LOW);
        delay(20); // between pulses
        digitalWrite(pinServo,HIGH);
        delayMicroseconds(1000);
    }
    Serial.println("End movePulse_1000()");
}
+4
source share
2 answers

, 1,5 , . , , . , . .

. , - . , pwm . , , . , , /delaymicros, , . . , .

+2

servo.h , = .

, 0 180 .

del = (7 * x) +500; - 500 1260 (, )

void movePulse(int x){
    int del=(7*x)+500;
    for (int pulseCounter=0; pulseCounter<=50; pulseCounter++){
        digitalWrite(pinServo,HIGH);
        delayMicroseconds(del);
        digitalWrite(pinServo,LOW);
        delay(20); // between pulses
    }
}
0

Source: https://habr.com/ru/post/1694313/


All Articles