Hi all,
The prepose of this code is to connect the stepper motor to a digital pin so that when I select high voltage, the stepper motor will run clockwise once only and stop until I switch the voltage to low and it will run counter clockwise once and stop.
In my current code my motor would run one direction continuously (looping the same command until I switch to low/high and it just continue to run until i switch again).
My question is: Is there anyway to make it run once only when I switch the state . That is including when the board just plugged in, it should not do anything until i switch voltage.
Thanks!
Here is my code
#include <Stepper.h>
#include <EEPROM.h>
int Pin_Digital_Input = 52;
int digitalState = 0;
int distanceCountAbs = 0;
const int stepsPerRevolution = 1600;
Stepper myStepper(stepsPerRevolution, 22, 24);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(Pin_Digital_Input, INPUT);
}
void loop() {
//read the input at the digital input pin
digitalState = digitalRead(Pin_Digital_Input);
//read the input on analog pin 0;
int sensorValue = analogRead(52);
//Convert the analog reading(0-1023)
float voltage = sensorValue * (5.0/1023.0);
if(digitalState == 1) {
distanceCountAbs = (EEPROM.read(2)-2)/.015875;
myStepper.setSpeed(EEPROM.read(1)); /////this will base on the user input from LCD and converted to step/s
myStepper.step(distanceCountAbs); /////set to user input divided .015875mm/micro step
myStepper.setSpeed(10);
myStepper.step(2/.015875);
delay (500);
}
if(digitalStat2 == 0){
myStepper.setSpeed(10);
myStepper.step(-2/.015875);
myStepper.setSpeed(EEPROM.read(1)); /////this will base on the user input from LCD and converted to step/s
myStepper.step(-distanceCountAbs); //// return to home position
delay (500);
}
}