Howdy. Newbie here, so pardon my inexperience.
I'm driving a stepper motor w/ an EasyDriver and I've got the interface working decently. Everything's talking, but I have some programming issues.
I've got it hooked to a potentiometer that controls the speed (through microstepping) of the motor, and a momentary switch that determines the direction the motor spins.
What I'd like to do is push the button and have the motor turn a predetermined number of steps and STOP. Then, release the button, turn a predetermined number of steps in the other direction and STOP.
Right now, it moves those steps, pauses, then moves again ad infinitum until I press or release the button.
How do I make it just move where I want and hold?
Here's the sketch:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
int dirpin = 3; // the number of stepper direction pin
int steppin = 12; // the number of stepper input
int sensorPin = 0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
int i;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
pinMode(dirpin, OUTPUT); // initialize stepper direction pin as output
pinMode(steppin, OUTPUT); // initialize stepper drive pin as output
}
void loop(){
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
if (buttonState == LOW) { // check if the pushbutton is pressed.
// if it is, the buttonState is LOW
digitalWrite(ledPin, HIGH); // turn LED on
sensorValue = analogRead(sensorPin);
digitalWrite(dirpin, LOW); // Sets the direction.
delay(1); // smooths transition
Serial.println(">>");
for (i = 0; i<600; i++) // Iterate for 600 microsteps.
{
digitalWrite(steppin, LOW); // This LOW to HIGH change is what creates the
digitalWrite(steppin, HIGH); // "Rising Edge" so the easydriver knows to when to step.
delayMicroseconds(sensorValue); // Sets the speed based on pot setting
}
}
else {
sensorValue = analogRead(sensorPin);
digitalWrite(ledPin, LOW); // turn LED off
digitalWrite(dirpin, HIGH); // Change direction.
delay(1);
Serial.println("<<");
for (i = 0; i<600; i++) // Iterate for 600 microsteps
{
digitalWrite(steppin, LOW); // This LOW to HIGH change is what creates the
digitalWrite(steppin, HIGH); // "Rising Edge" so the easydriver knows to when to step.
delayMicroseconds(sensorValue); // Sets the speed based on pot setting
}
}
}