Hello everyone,
I'm fairly new to Arduino and I've never worked with the AccelStepper Library before but I really need it for this project I'm working on (because it allows me to accelerate and decelerate at the beginning and end of each movement).
Here's what I'm trying to do for this part of the code:
When I press a button (Green Button - bGreen), I want 3 LED's and the board's pin 13 LED to turn on for one second, off for one second and then spin the stepper motor 2958 steps. And I need this to repeat X number of times (3 to be exact) on a SINGLE button press.
So basically: I press the button once: LED's blink, motor spins 2958 steps, LED's blink again, Motor spins 2958 steps again, LED's blink last time and motor spins 2958 steps for the last time. After that, it stops and waits for me to press the button again to do another 3 LED-blinks + turns.
As of right now, it blinks the LED's 3 times and THEN moves the motor 2958 steps only ONCE. I think that's because the Stepper1.run() is telling it to run only at the end... but then again, it seems like Stepper1.run() HAS to be at the end to get the motor to do anything.
I've been researching this for a while and have gone through the AccelStepper library function definitions but I still can't figure out what I'm doing wrong, which is why I'm finally reaching out for help. Maybe I'm misunderstanding some of the definitions or something.
HARDWARE: Arduino Uno, NEMA 17 stepper motor and a4988 stepper driver - black edition.
Anyway, any help would be GREATLY APPRECIATED! Here's my code so far:
#include <AccelStepper.h>
#define ITERATIONS 3
AccelStepper Stepper1(1,12,11); //use pin 11 and 12 for dir and step, 1 is the "external driver" mode (A4988)
int led1 = 8;
int led2 = 9;
int led3 = 10;
int ledPin13 = 13;
int bGreen = 4; //Green button
int stepCounter = 0;
int stepping = false;
void setup() {
Stepper1.setMaxSpeed(3000); //set max speed the motor will turn (steps/second)
Stepper1.setAcceleration(13000); //set acceleration (steps/second^2)
pinMode(ledPin13, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(bGreen, INPUT);
digitalWrite(bGreen, LOW);
}
void loop() {
if(digitalRead(bGreen) == HIGH && stepping == false)
{
stepping = true;
}
if (stepping == true)
{
delay(1000); //wait 1 second
digitalWrite(ledPin13, HIGH); // turn the LED on
digitalWrite(led1, HIGH); // Activate led1
digitalWrite(led2, HIGH); // Activate led2
digitalWrite(led3, HIGH); // Activate led3
delay(1000); // wait for 1 second
digitalWrite(ledPin13, LOW); // turn the LED off
digitalWrite(led1, LOW); // Deactivate led1
digitalWrite(led2, LOW); // Deactivate led2
digitalWrite(led3, LOW); // Deactivate led3
delay(1000); // wait for 1 second
Stepper1.move(2958); //move 2958 steps
stepCounter = stepCounter + 1;
if(stepCounter == ITERATIONS){
stepCounter = 0;
stepping = false;
}
}
Stepper1.run(); //run the stepper. this has to be done over and over again to continously move the stepper
}