Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 15 February 2018
- This is a test sketch to operate a reversible stepper motor on an Arduino Mega.
- The stepper motor spins all the time, but pressing the button reverses the motor for 1 rotation cycle.
- This sketch uses a 'fixed' software button debounce where the debounce time is not tracked
- and the button is only re-enabled after a certain event completes
- (in this case when the stepper motor completes one cycle of rotation).
- This is not the usual way to debounce,
- but it can work okay if the button event is long enough-
- (such as a stepper motor turning a bunch of times).
- */
- #include <Stepper.h>
- const int stepsPerRevolution = 128;
- const int inPin = 52;
- int val = 0;
- Stepper myStepper(stepsPerRevolution, 3, 5, 4, 6);
- int stepperDirection = 0; // This is used to hold a modified value of stepsPerRevolution (either positive or negative) without changing the original value.
- // Below is variables for debouncing the button input.
- bool button_enabled = true;
- // There is no variables needed for debounce timing.
- void setup() {
- myStepper.setSpeed(120);
- pinMode(inPin, INPUT);
- pinMode(13, OUTPUT);
- digitalWrite(13, HIGH);
- }
- void loop() {
- if (button_enabled == true) {
- val = digitalRead(inPin);
- if (val == 1) {
- button_enabled = false;
- // If the button is enabled and it is pressed, then it is disabled here.
- digitalWrite(13, LOW);
- }
- }
- if (val == HIGH) {
- stepperDirection = stepsPerRevolution;
- }
- else {
- stepperDirection = (stepsPerRevolution * -1);
- }
- myStepper.step(stepperDirection);
- // After the motor has rotated through one cycle, the direction value is reset to zero and the button is re-enabled.
- val = 0;
- button_enabled = false;
- digitalWrite(13, HIGH); // Turn pin 13 back on, since the button is now enabled.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement