Hi All,
First post and first time delving into Arduino & code with a little project I have embarked on. Has been a reasonable learning curve so far. But enjoying the challenge
1x Uno scrapped already. , and I think a bad stepper driver. In any case.
I look forward to any help users can provide to this noob.
I have used the search function and looked over other posts with similar titles/ objectives but go about it differently, so thought I should make my own project post.
Project: Using a Arduino Uno + easy driver (V4.4 A3967 set at 1/8 step) + 2x momentary switches. Move a Nema 11 stepper motor linear rail when Up or Down button is pressed at a set RPM only when and for as long as either button is pressed in the desired direction.
Issue/ Troubleshooting: I have simulated this device using wokwi and run the code. Once either button is pressed the wokwi simulation continues to run the motor in the direction without stopping once the button is released. It does notice both Up / Down directions.
This is also the case on the physical project. Once the Up or down button is pressed, the Motor will continue turning until the Uno board is reset (Don't want linear rail to hit stops)
Question: Do I simply have some code in the wrong order, or missing a If statement?
What is the best way to have the code continuously check for the release of the Up or down button?
Viewing the original code, the If currentTime - Last StepTime is possibly where I am getting issues?
Visual examples help me a great deal as I can interpret how it works in the code (if possible).
Troubleshooting: I have tried the single button press code (Original) and altered it but have not had any success. Along with different changes to the Up/Down code.
My most recent try had the motor start as soon as power was applied. Feel I am going backwards.
Thanks for any help you can provide with the project or pointing me in the direction to alter what is required.
Code example from:
https://www.schmalzhaus.com/EasyDriver/Examples/EasyDriverExamples
Code (Motor continues in direction of button push Up/Down:
#define RPMS 104.0
#define STEP_PIN 9 // Tells motor to move
#define DIRECTION_PIN 8 // Tells Board/ Motor Direction using HiGH (clockwise)/LOW (Anticlockwise)
#define UP_PIN 3 // Up Pin = Up Momentary Switch
#define Down_PIN 2 // Down Pin = Down Momentary Switch
#define STEPS_PER_REV 200
#define MICROSTEPS_PER_STEP 8
#define MICROSECONDS_PER_MICROSTEP (1000000/(STEPS_PER_REV * MICROSTEPS_PER_STEP)/(RPMS / 60))
int Stepping = false;
uint32_t LastStepTime = 0;
uint32_t CurrentTime = 0;
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIRECTION_PIN, OUTPUT);
digitalWrite(STEP_PIN, LOW);
digitalWrite(DIRECTION_PIN, LOW);
pinMode(UP_PIN,INPUT);
pinMode(Down_PIN,INPUT);
}
void loop() {
if (digitalRead(UP_PIN) == LOW && Stepping == false)
{
digitalWrite(DIRECTION_PIN, LOW); // Tells board Up on direction Pin using LOW
Stepping = true; // Applies above only if true
}
if (digitalRead(Down_PIN) == LOW && Stepping == false)
{
digitalWrite(DIRECTION_PIN, HIGH); // Tells board Down on direction Pin using HIGH
Stepping = true; // Applies above only if true
}
if (Stepping == true)
{
CurrentTime = micros();
if ((CurrentTime - LastStepTime) > MICROSECONDS_PER_MICROSTEP)
{
LastStepTime = CurrentTime;
digitalWrite(STEP_PIN, HIGH); // When stepping True - Step Pin set HIGH Motor moves at Set Speed in Down Direction
delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
digitalWrite(STEP_PIN, LOW); // When stepping True - Step Pin set LOW Motor moves at Set Speed in Down Direction
delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
}
}
}
Original Code: (Only set for One button)
/* This example assumes a step/direction driver with Step on pin 9, Direction on pin 8
* And an input switch on pin 3. The switch is a switch to ground, with pin 3 pulled
* high with a pullup resistor. When the switch is turned on (closed, i.e. goes low)
* the the sepper motor steps at the rate specified (104 RPM in this code, with
* 1/8th microstepping of a 200 steps/rev motor)
*/
#define RPMS 104.0
#define STEP_PIN 9
#define DIRECTION_PIN 8
#define GO_PIN 3
#define STEPS_PER_REV 200
#define MICROSTEPS_PER_STEP 8
#define MICROSECONDS_PER_MICROSTEP (1000000/(STEPS_PER_REV * MICROSTEPS_PER_STEP)/(RPMS / 60))
uint32_t LastStepTime = 0;
uint32_t CurrentTime = 0;
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIRECTION_PIN, OUTPUT);
digitalWrite(STEP_PIN, LOW);
digitalWrite(DIRECTION_PIN, LOW);
pinMode(GO_PIN,INPUT);
}
void loop() {
if (digitalRead(GO_PIN) == LOW)
{
CurrentTime = micros();
if ((CurrentTime - LastStepTime) > MICROSECONDS_PER_MICROSTEP)
{
LastStepTime = CurrentTime;
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds((MICROSECONDS_PER_MICROSTEP * 0.9)/2);
}
}
}
Cheers,
Gav