PaulS:
Not without changing your code. When you change code, you need to re-post it.
I have decided to start over, to be honest my first sketch wasn't so clear so I have decided to make a step back, as I was saying in my previous post this code works well:
int directionPin = 6;
int stepPin = 10;
int buttonpin = 2;
boolean buttonpressed; //false or true
int 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);
//If a pull-down resistor is used, the input pin will be LOW when the switch is open and HIGH when the switch is closed.
//If a pull-up resistor is used, the input pin will be HIGH when the switch is open and LOW when the switch is closed.
pinMode(buttonpin, INPUT_PULLUP);
}
void loop() {
curMillis = millis();
readButtons();
actOnButtons();
}
void readButtons() {
buttonpressed = false; //false is defined as 0 (zero), THE SWITCH IS OPEN
buttonpressed = true; //Any integer which is non-zero is true, THE SWITHCH IS CLOSED
if (digitalRead(buttonpin) == LOW) { // low means 0V
buttonpressed = true;
}
if (digitalRead(buttonpin) == HIGH) { // high menas 5V
buttonpressed = false;
}
}
void actOnButtons() {
if (buttonpressed == false)
{
digitalWrite(directionPin, LOW);
singleStep();{
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
}
}
if (buttonpressed == true)
{
digitalWrite(directionPin, HIGH);
singleStep();{
digitalWrite(stepPin,HIGH);
delayMicroseconds(20);
digitalWrite(stepPin,LOW);
delayMicroseconds(20);
}
}
}
void singleStep() {
if (curMillis - prevStepMillis >= millisBetweenSteps) {
prevStepMillis += millisBetweenSteps;
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
}
but on my new test using accelstepper library I haven't got the same satisfaction, the stepper doesn't move at all, do you have any advice?
#include <AccelStepper.h>
#include <MultiStepper.h>
AccelStepper Stepper1(1,6,10); //use pin 6 and 10 for dir and step, 1 is the "external driver" mode (A4988)
int dir = 1; //used to switch direction
/*float pressLength_milliSeconds = 0;
int optionOne_milliSeconds = 100;
int optionTwo_milliSeconds = 2000;*/
void setup() {
Stepper1.setMaxSpeed(3000); //set max speed the motor will turn (steps/second)
Stepper1.setAcceleration(13000); //set acceleration (steps/second^2)
}
void loop() {
if(Stepper1.distanceToGo()==0){ //check if motor has already finished his last move
Stepper1.move(1600*dir); //set next movement to 1600 steps (if dir is -1 it will move -1600 -> opposite direction)
dir = dir*(-1); //negate dir to make the next movement go in opposite direction
delay(1000); //wait 1 second
}
Stepper1.run(); //run the stepper. this has to be done over and over again to continously move the stepper
}