Hi guys, I’m new to Arduino and coding but have been having a bit of fun while learning. Recently I made a circuit using a A4899 stepper driver and it does exactly as I want however I’m struggling trying to figure out the code to do different things and was hoping you guys could help me learn. For example, in the code below I can understand to a degree how it’s working. What I’d like to know is what defines the initial speed? I realize that the delay will make my motor go faster or slower but based on what? Is this defined in the stepper.h library definitions? Is max speed when delay is set to 0 and cannot go any faster? Also, what If I wanted it go X speed in one direction and a different speed in the opposite direction? Any help understanding is much appreciated.
#include <Stepper.h>
// define a constant value named stepPin and assign the value 9 to it
// this assumes digital pin 9 of Arduino is attached to the step input of driver
#define stepPin 9
// define a constant value named dirPin and assign the value 8
// this assumes digital pin 8 of Arduino is attached to the step input of your driver
#define dirPin 8
// define the pins on N.O. (normally open) buttons
#define button1 2
#define button2 3
// setup() loop, the Arduino runs through this once
void setup() {
// digital pins on the Arduino can only be either set as an output or input - in our case we want to send data to the driver, so we choose output
pinMode(stepPin , OUTPUT);
pinMode(dirPin , OUTPUT);
// define button pins as input pullup type
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
// set an initial value of low to both our step and dir pins, we could easily write false or 0 instead of LOW
digitalWrite(stepPin , LOW);
digitalWrite(dirPin , LOW);
}
// loop() loop, the Arduino continuously cycles through this as fast as it can
void loop() {
if (digitalRead(button1) == LOW && digitalRead(button2) == HIGH) { // if button1 is pressed and button2 is not pressed
digitalWrite(dirPin, LOW); // move in the LOW direction
} else if (digitalRead(button1) == HIGH && digitalRead(button2) == LOW) { // if button1 is not pressed and button2 is pressed
digitalWrite(dirPin, HIGH); // move in HIGH direction
}
if (digitalRead(button1) == LOW || digitalRead(button2) == LOW) { // if either button is pressed
// set a HIGH value to our step pin, this turns the voltage on for that pin
digitalWrite(stepPin , HIGH);
// let's wait here for 50 milliseconds; note the units, this means 0.05s
delay(2);
// let's set our step pin to false, this turns the voltage off for that pin and gives us the on/off cycle we need
digitalWrite(stepPin , LOW);
// wait another 50 milliseconds after which time we loop back to the beginning of the loop() loop
delay(2);
}
}