Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.4 kB
0
Indexable
Never
#include <pigpio.h>
#include <iostream>
#include <unistd.h> // For sleep()

int main() {
    // Initialize the pigpio library
    if (gpioInitialise() < 0) {
        std::cerr << "Failed to initialize pigpio" << std::endl;
        return 1;
    }

    // Set GPIO pin number
    unsigned int pin = 14;

    // Set servo parameters
    int mid_pulse_width = 1500; // Mid pulse width in microseconds

    // Set GPIO pin mode to output
    if (gpioSetMode(pin, PI_OUTPUT) != 0) {
        std::cerr << "Failed to set GPIO mode" << std::endl;
        gpioTerminate();
        return 1;
    }

    // Set servo to mid position
    if (gpioServo(pin, mid_pulse_width) != 0) {
        std::cerr << "Failed to set servo to mid position" << std::endl;
        gpioTerminate();
        return 1;
    }
    
    std::cout << "Servo mid" << std::endl;
    sleep(3);

    // Move servo
    while (true) {
        // Move from -90° to 90°
        for (int i = -10; i <= 10; ++i) {
            gpioServo(pin, mid_pulse_width + i * 100); // Adjust pulse width for movement
            sleep(2);
        }
        // Move from 90° to -90°
        for (int i = 10; i >= -10; --i) {
            gpioServo(pin, mid_pulse_width + i * 100); // Adjust pulse width for movement
            sleep(2);
        }
    }

    // Terminate pigpio library
    gpioTerminate();

    return 0;
}
Leave a Comment