Created
November 16, 2018 00:42
-
-
Save natendaben/def79b17f5916a2d66b3968ad49366c4 to your computer and use it in GitHub Desktop.
Stepper Motor
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
//Declare pin functions on Redboard | |
#define stp 2 | |
#define dir 3 | |
#define MS1 4 | |
#define MS2 5 | |
#define EN 6 | |
//Declare variables for functions | |
char user_input; | |
int x; | |
int y; | |
int state; | |
int buttonState = LOW; | |
int pot = 0; | |
void setup() { | |
pinMode(stp, OUTPUT); | |
pinMode(dir, OUTPUT); | |
pinMode(MS1, OUTPUT); | |
pinMode(MS2, OUTPUT); | |
pinMode(EN, OUTPUT); | |
pinMode(8, INPUT); | |
pinMode(A0, INPUT); | |
resetEDPins(); //Set step, direction, microstep and enable pins to default states | |
Serial.begin(9600); //Open Serial connection for debugging | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
buttonState = digitalRead(8); | |
//Serial.println(pot); | |
if(buttonState==HIGH) //if button is on | |
{ | |
digitalWrite(dir, LOW); //Pull direction pin low to move "forward" | |
for(x= 1; x<1000; x++) //Loop the forward stepping enough times for motion to be visible | |
{ | |
pot = analogRead(A0); | |
int mappedPot = map(pot, 0, 1023, 1, 50); | |
int delayTime = mappedPot; | |
digitalWrite(stp,HIGH); //Trigger one step forward | |
delay(delayTime); | |
digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again | |
delay(delayTime); | |
} | |
} | |
else{ | |
resetEDPins(); | |
} | |
} | |
//Reset Easy Driver pins to default states | |
void resetEDPins() | |
{ | |
digitalWrite(stp, LOW); | |
digitalWrite(dir, LOW); | |
digitalWrite(MS1, LOW); | |
digitalWrite(MS2, LOW); | |
digitalWrite(EN, HIGH); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment