Created
January 10, 2023 13:25
-
-
Save fxprime/437927f3824036a6bd37d4aba42a0388 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
*/ | |
const int dirPin = 8; | |
const int stepPin = 9; | |
const int enablePin = 7; | |
const int sw = 6; | |
const int delayMicros = 500; //Delay between each step | |
const int maximumSteps = 230; | |
int _currentStep = 0; | |
//DIR high = ccw | |
//DIR low = cw | |
/** | |
* Go back to home position | |
* 1. Set DIR pin to HIGH to make motor turn CW | |
* 2. Send step pin (HIGH,WAIT,LOW,WAIT) to driver to move 1 step | |
* 3. Keep send step until switch is on (LOW) | |
*/ | |
void goHomePosition() { | |
//Set direction to CW | |
digitalWrite(dirPin, HIGH); | |
while(digitalRead(sw)){ | |
digitalWrite(stepPin, HIGH); | |
delayMicroseconds(delayMicros); | |
digitalWrite(stepPin, LOW); | |
delayMicroseconds(delayMicros); | |
} | |
_currentStep = 0; | |
} | |
/** | |
* Run motor CCW for given steps | |
*/ | |
void runStepperCCW(int steps) { | |
//Set direction to CCW | |
digitalWrite(dirPin, LOW); | |
//Run motor, each step contain 1 Rising edge and 1 Falling edge | |
for (int i = 0; i < steps; i++) { | |
//Stop when maximum steps is reached | |
if(_currentStep>maximumSteps) break; | |
digitalWrite(stepPin, HIGH); | |
delayMicroseconds(delayMicros); | |
digitalWrite(stepPin, LOW); | |
delayMicroseconds(delayMicros); | |
_currentStep++; | |
} | |
Serial.println(_currentStep); | |
} | |
void setup() { | |
Serial.begin(115200); | |
pinMode(enablePin, OUTPUT); | |
pinMode(stepPin, OUTPUT); | |
pinMode(dirPin, OUTPUT); | |
pinMode(sw, INPUT_PULLUP); | |
digitalWrite(enablePin, LOW); | |
} | |
void loop() { | |
const int steps = 230;//90 | |
const int stepAtHighestFlowrate = 20; | |
const int stepAtLowestFlowrate = 230; | |
const int stepAtHalfFlowrate = (20+230)/2; | |
//These code will run back to home and go to position 1 | |
//then go back to home and go to position 2 | |
//then go back to home and go to position 3 | |
delay(3000); | |
goHomePosition(); | |
delay(3000); | |
runStepperCCW(stepAtHighestFlowrate); | |
delay(3000); | |
goHomePosition(); | |
delay(3000); | |
runStepperCCW(stepAtHalfFlowrate); | |
delay(3000); | |
goHomePosition(); | |
delay(3000); | |
runStepperCCW(stepAtLowestFlowrate); | |
delay(3000); | |
//These lines of code will run back to home position | |
//and go to position 1 | |
//and go to position 2 | |
//and go to position 3 | |
goHomePosition(); | |
delay(3000); | |
runStepperCCW(stepAtHighestFlowrate); | |
delay(3000); | |
runStepperCCW(stepAtHalfFlowrate-_currentStep); | |
delay(3000); | |
runStepperCCW(stepAtLowestFlowrate-_currentStep); | |
delay(3000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment