1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#include <ezButton.h>
#include <AccelStepper.h>
//Variable won't change. They're used here to set pin numbers:
ezButton ForwardButton(12); // create ezButton object that attach to pin 12;
ezButton StopForwardButton(8); // create ezButton object that attach to pin 8;
ezButton BackwardButton(11); // create ezButton object that attach to pin 11;
ezButton StopBackwardButton(9); // create ezButton object that attach to pin 9;
const int SHORT_PRESS_TIME = 500; // 500 milliseconds
int smDirectionPin = 2; //Direction pin
int smStepPin = 3; //Stepper pin
int ledpin = 13; //Internal Led
// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER,smStepPin,smDirectionPin);
// variables will change:
int stepPinStateForward = LOW; // the current forward state of motor
int stepPinStateBackward = LOW; // the current backward state of motor
int manualMode = LOW; // the current Manual Mode state
unsigned long pressedTime = 0; // Variable for long press detection (Manual Mode)
unsigned long releasedTime = 0; // Variable for long press detection (Manual Mode)
void setup() {
ForwardButton.setDebounceTime(50); // set debounce time to 50 milliseconds
StopForwardButton.setDebounceTime(50); // set debounce time to 50 milliseconds
BackwardButton.setDebounceTime(50); // set debounce time to 50 milliseconds
StopBackwardButton.setDebounceTime(50); // set debounce time to 50 milliseconds
/**
* The use of EZButton librairies
*/
// initialize the pin as an inputs/output:
pinMode(smDirectionPin, OUTPUT);
pinMode(smStepPin, OUTPUT);
pinMode(ledpin,OUTPUT);
pinMode(relay_pin, OUTPUT);
/**
* Basic motor setup
*/
stepper.setMaxSpeed(1000);
stepper.setAcceleration(300);
Serial.begin(9600);
}
void loop() {
/**
* ezButton library
*/
ForwardButton.loop(); // MUST call the loop() function first
StopForwardButton.loop(); // MUST call the loop() function first
BackwardButton.loop(); // MUST call the loop() function first
StopBackwardButton.loop(); // MUST call the loop() function first
// read the state of the switches value:
int StateForwardButton = ForwardButton.getState();
int StateStopForwardButton = StopForwardButton.getState();
int StateBackwardButton = BackwardButton.getState();
int StateStopBackwardButton = StopBackwardButton.getState();
/**
* Auto/Manual Mode detection.
* If a long press button is detected then active Manual Mode for opening door
*/
if(ForwardButton.isPressed() || BackwardButton.isPressed()){
pressedTime = millis(); //time value at pressed button
}
else if(ForwardButton.isReleased() || BackwardButton.isReleased()) { // button is released
releasedTime = millis(); //time value at released button
long pressDuration = releasedTime - pressedTime;
if( pressDuration > SHORT_PRESS_TIME ){
manualMode = !manualMode;
//Serial.println(manualMode);
}
}
/**
* Since the ezButton library used the internal pull-up resistor,
* the state will be HIGH when no press, and LOW when pressed.
* If Forward button is pressed and limit switch is released in manual mode then move stepper
*/
//MANUAL MODE
//MOVE FORWARD
if(ForwardButton.isPressed() && StateStopForwardButton == HIGH && manualMode == HIGH) {
stepPinStateForward = !stepPinStateForward; //pass state from LOW to HIGH
digitalWrite(ledpin, stepPinStateForward); //Led ON
if(stepPinStateForward == HIGH ){
stepper.moveTo(5000);
stepper.runToPosition();
stepPinStateForward = !stepPinStateForward; //reverse the state of the pin
digitalWrite(ledpin, stepPinStateForward); //Led OFF
}
}
/**
* If limit switch is pressed and stepper is running in manual mode, then stop motor
*/
if (StopForwardButton.isPressed() && stepper.isRunning() && manualMode == HIGH){
stepPinStateForward = !stepPinStateForward; //pass from HIGH to LOW
digitalWrite(ledpin, stepPinStateForward); //Led OFF
Serial.println("stop");
stepper.setCurrentPosition(0);
stepper.stop();
}
//MOVE BACKWARD
if(BackwardButton.isPressed() && StateStopBackwardButton == HIGH && manualMode == HIGH) {
stepPinStateBackward = !stepPinStateBackward; //pass state from LOW to HIGH
digitalWrite(ledpin, stepPinStateBackward); // led ON
if(stepPinStateBackward == HIGH ){
stepper.moveTo(0);
stepper.runToPosition();
stepPinStateBackward = !stepPinStateBackward; //reverse the pin state
digitalWrite(ledpin, stepPinStateBackward); // Led OFF
}
}
/**
* If limit switch is pressed and stepper is running in manual mode, then stop motor
*/
if (StopBackwardButton.isPressed() && stepper.isRunning() && manualMode == HIGH) {
stepPinStateBackward = !stepPinStateBackward;
digitalWrite(ledpin, stepPinStateBackward);
stepper.setCurrentPosition(0);
stepper.stop();
}
} |
Partager