So far I have written this code but unfortunately I haven't had any responce from the stepper
float pressLength_milliSeconds = 0;
// Define the *minimum* length of time, in milli-seconds, that the button must be pressed for a particular option to occur
int optionOne_milliSeconds = 100;
int optionTwo_milliSeconds = 2000;
//The Pin your button is attached to
int buttonPin = 2;
//Pin your LEDs are attached to
int directionPin = 6;
int stepPin = 10;
unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 25; // milliseconds
void setup(){
Serial.begin(9600); //Start serial communication
Serial.println("Starting Stepper Demo with millis()");
// Initialize the pushbutton pin as an input pullup
(buttonPin, INPUT_PULLUP);
//set the driver pins as outputs
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
} // close setup
void loop() {
curMillis = millis();
actOnButtons();
//Record *roughly* the tenths of seconds the button in being held down
while (digitalRead(buttonPin) == HIGH ){
delay(100); //if you want more resolution, lower this number
pressLength_milliSeconds = pressLength_milliSeconds + 100;
//display how long button is has been held
Serial.print("ms = ");
Serial.println(pressLength_milliSeconds);
}//close while
//every time through the loop, we need to reset the pressLength_Seconds counter
pressLength_milliSeconds = 0;
} // close void loop
//Different if-else conditions are triggered based on the length of the button press
//Start with the longest time option first
void actOnButtons(){
//Option 2 - Execute the second option if the button is held for the correct amount of time
if (pressLength_milliSeconds >= optionTwo_milliSeconds){
digitalWrite(directionPin, HIGH);
singleStep();{
digitalWrite(stepPin,HIGH);
delayMicroseconds(20);
digitalWrite(stepPin,LOW);
delayMicroseconds(20);
}
}
//option 1 - Execute the first option if the button is held for the correct amount of time
if(pressLength_milliSeconds >= optionOne_milliSeconds){
digitalWrite(directionPin, LOW);
singleStep();{
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
}
}
}
void singleStep() {
if (curMillis - prevStepMillis >= millisBetweenSteps) {
prevStepMillis += millisBetweenSteps;
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
}
Any idea where I'm wrong?
I know that the wiring is correct because using this other code the stepper works
// testing a stepper motor with a Pololu A4988 driver board or equivalent
// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW
byte directionPin = 6;
byte stepPin = 10;
byte buttonCWpin = 2;
//byte buttonCCWpin = 11;
boolean buttonCWpressed = true;
//boolean buttonCCWpressed = false;
byte ledPin = 13;
unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 25; // milliseconds
void setup() {
Serial.begin(9600);
Serial.println("Starting Stepper Demo with millis()");
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonCWpin, INPUT_PULLUP);
//pinMode(buttonCCWpin, INPUT_PULLUP);
}
void loop() {
curMillis = millis();
readButtons();
actOnButtons();
}
void readButtons() {
buttonCWpressed = false;
buttonCWpressed = true;
if (digitalRead(buttonCWpin) == HIGH) {
buttonCWpressed = false;
}
if (digitalRead(buttonCWpin) == LOW) {
buttonCWpressed = true;
}
}
void actOnButtons() {
if (buttonCWpressed == true) {
digitalWrite(directionPin, HIGH);
singleStep();{
digitalWrite(stepPin,HIGH);
delayMicroseconds(20);
digitalWrite(stepPin,LOW);
delayMicroseconds(20);
}
}
if (buttonCWpressed == false) {
digitalWrite(directionPin, LOW);
singleStep();{
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
}
}
}
void singleStep() {
if (curMillis - prevStepMillis >= millisBetweenSteps) {
prevStepMillis += millisBetweenSteps;
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
}