/* Stepper basic example with Raspberry Pi Pico.
 * The motor will variously rotate and pause as noted below
 */

#include <stdio.h>
#include "pico/stdlib.h"
#include "stepper.h"

// Stepper constants
const uint8_t stepper_pin_1A = 11;
const uint8_t stepper_pin_1B = 10;
const uint8_t stepper_pin_2A = 13;
const uint8_t stepper_pin_2B = 12;
const uint16_t stepper_steps_per_revolution = 200;
const stepper_mode_t stepping_mode = single;

stepper_t stepper;

uint8_t speed = 50;

int main() {
    stdio_init_all();

    // Initialise the stepper
    stepper_init(&stepper, stepper_pin_1A, stepper_pin_1B,
                 stepper_pin_2A, stepper_pin_2B,
                 stepper_steps_per_revolution, stepping_mode);
    stepper_set_speed_rpm(&stepper, speed);

    while (true) {
        // Rotate 3/4 of a turn
        stepper_rotate_degrees(&stepper, 270);
        sleep_ms(500);

        // Now rotate these many steps in the oposite direction
        stepper_rotate_steps(&stepper, -45);
        sleep_ms(500);

        // Increase the speed and rotate 360 degrees
        speed = 50;
        stepper_set_speed_rpm(&stepper, speed);
        stepper_rotate_degrees(&stepper, 360);

        // Release the coils and sleep for a while. You can check that
        // the coils are not energised by moving the rotor manually:
        // there should be little resistance
        stepper_release(&stepper);
        sleep_ms(4000);

        // Decrease the speed
        speed = 25;
        stepper_set_speed_rpm(&stepper, speed);
    }

    return 0;
}
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT