|
| 1 | +/* |
| 2 | + Interruptible Three-phase Stepper Motor Control - one revolution |
| 3 | +
|
| 4 | + This program drives a three-phase unipolar stepper motor. |
| 5 | + The motor is attached to GPIO pins 17, 18, 19 of a Heltec wifi kit 32 development board |
| 6 | + (modify for your Arduino or whatever) |
| 7 | +
|
| 8 | + The motor should revolve one revolution in one direction, then |
| 9 | + one revolution in the other direction. |
| 10 | +
|
| 11 | + A potentiometer is used to set speed (like the MotorKnob example) |
| 12 | + If you select a slow speed (like 1), it can take a long time to step |
| 13 | +
|
| 14 | + If you don't want to wait for a cycle to finish itself, you can |
| 15 | + "interrupt" it by pressing the pushbutton -- the ISR calls |
| 16 | + Stepper::interrupt() which sets a private boolean "INTERRUPTED" |
| 17 | + value in your Stepper, which will cause the while() loop in Stepper::step() |
| 18 | + to exit instead of running a long time. The user code will then call |
| 19 | + Stepper::clear_interrupt(), which resets the INTERRUPTED boolean, in your Stepper |
| 20 | +
|
| 21 | +Output should look like this: |
| 22 | + config pushbutton ==> Done |
| 23 | + Attach interrupt... ==> Done |
| 24 | + Setting stepper speed... ==> Done |
| 25 | + setup() complete |
| 26 | + clockwise, speed: 1 button PRESSED |
| 27 | + counterclockwise, speed: 23 button NOT pressed |
| 28 | + clockwise, speed: 23 button NOT pressed |
| 29 | + counterclockwise, speed: 24 button NOT pressed |
| 30 | + clockwise, speed: 11 button PRESSED |
| 31 | + counterclockwise, speed: 8 button NOT pressed |
| 32 | + ... |
| 33 | +
|
| 34 | + Created 11 Mar. 2007; Modified 30 Nov. 2009 by Tom Igoe |
| 35 | + Modified 14 Dec 2022 by Joe Brendler |
| 36 | + */ |
| 37 | + |
| 38 | +#include <Arduino.h> |
| 39 | +#include <Stepper.h> |
| 40 | + |
| 41 | +#define pot A0 |
| 42 | +#define pushbutton 37 |
| 43 | + |
| 44 | +const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution for your motor |
| 45 | +long speed = 50; // analog input reading (in rpm) |
| 46 | + |
| 47 | +struct buttonInterruptLine |
| 48 | +{ |
| 49 | + const uint8_t PIN; // gpio pin number |
| 50 | + volatile uint32_t numHits; // number of times fired |
| 51 | + volatile bool PRESSED; // boolean logical; is triggered now |
| 52 | +}; |
| 53 | +buttonInterruptLine buttonPress = {pushbutton, 0, false}; |
| 54 | + |
| 55 | +// initialize the three-phase stepper library on GPIO |
| 56 | +Stepper myStepper(stepsPerRevolution, 17, 18, 19); |
| 57 | + |
| 58 | +/*------------------------------------------------------------------------------ |
| 59 | + handle_button_interrupt() - set flag and call stepper interrupt method |
| 60 | + ------------------------------------------------------------------------------*/ |
| 61 | +void IRAM_ATTR handle_button_interrupt() |
| 62 | +{ |
| 63 | + buttonPress.PRESSED = true; |
| 64 | + myStepper.interrupt(); // method sets a flag |
| 65 | +} |
| 66 | + |
| 67 | +void setup() |
| 68 | +{ |
| 69 | + // initialize the serial port: |
| 70 | + Serial.begin(115200); |
| 71 | + |
| 72 | + // initialize analog input |
| 73 | + pinMode(pot, INPUT); |
| 74 | + |
| 75 | + // Configure function pushbutton interrupt pin |
| 76 | + Serial.print("config pushbutton"); |
| 77 | + pinMode(buttonPress.PIN, INPUT_PULLDOWN); |
| 78 | + Serial.println(" ==> Done"); |
| 79 | + Serial.print("Attach interrupt... "); |
| 80 | + attachInterrupt(buttonPress.PIN, handle_button_interrupt, FALLING); |
| 81 | + Serial.println(" ==> Done"); |
| 82 | + // set stepper speed |
| 83 | + Serial.print("Setting stepper speed..."); |
| 84 | + // set the speed |
| 85 | + myStepper.setSpeed(speed); |
| 86 | + Serial.println(" ==> Done"); |
| 87 | + Serial.println("setup() complete"); |
| 88 | +} |
| 89 | + |
| 90 | +void loop() |
| 91 | +{ |
| 92 | + // read speed from 12-bit ADC input (map 1-100; stepper doesn't like speed=0) |
| 93 | + speed = (long)(map(analogRead(pot), 0, 4095, 1, 100)); |
| 94 | + myStepper.setSpeed(speed); |
| 95 | + Serial.printf("clockwise, speed: %d ", speed); |
| 96 | + // step one revolution in one direction: |
| 97 | + myStepper.step(stepsPerRevolution); |
| 98 | + if (buttonPress.PRESSED) |
| 99 | + Serial.println(" button PRESSED"); |
| 100 | + else |
| 101 | + Serial.println(" button NOT pressed"); |
| 102 | + if (buttonPress.PRESSED) |
| 103 | + { |
| 104 | + buttonPress.PRESSED = false; |
| 105 | + myStepper.clear_interrupt(); |
| 106 | + } |
| 107 | + delay(500); |
| 108 | + |
| 109 | + // read speed from 12-bit ADC input (map 1-100; stepper doesn't like speed=0) |
| 110 | + speed = (long)(map(analogRead(pot), 0, 4095, 1, 100)); |
| 111 | + myStepper.setSpeed(speed); |
| 112 | + Serial.printf("counterclockwise, speed: %d ", speed); |
| 113 | + // step one revolution in the other direction: |
| 114 | + myStepper.step(-stepsPerRevolution); |
| 115 | + if (buttonPress.PRESSED) |
| 116 | + Serial.println(" button PRESSED"); |
| 117 | + else |
| 118 | + Serial.println(" button NOT pressed"); |
| 119 | + if (buttonPress.PRESSED) |
| 120 | + { |
| 121 | + buttonPress.PRESSED = false; |
| 122 | + myStepper.clear_interrupt(); |
| 123 | + } |
| 124 | + delay(500); |
| 125 | +} |
0 commit comments