r/ArduinoProjects • • Jan 11 '24

Motor doesnt reverse?

Hi Guys this is my current code, i have the motor running in 3 stages with the last stage the motor needs to run in reverese, im not sure what to change with my code to make this work, or if it is regarding my hardware. im using a cnc sheild v3 and the power is connected to a power supply with 12v.

This is the code:

const int StepX = 2;
bool initialRun = false;
bool mainRun = false;
bool reverseRun = false;
void setup() {
pinMode(StepX, OUTPUT);
}
void loop() {
// If the initial run is not completed
if (!initialRun) {
// Move the motor for a short distance
for (int i = 0; i < 200; i++) {
digitalWrite(StepX, HIGH);
delayMicroseconds(500);
digitalWrite(StepX, LOW);
delayMicroseconds(1000);
}
// Stop the motor (optional)
digitalWrite(StepX, LOW);
initialRun = true;
// Introduce a delay before starting the main run
delay(2000); // Adjust this delay as needed
}
// If the initial run is completed and the main run hasn't started
else if (!mainRun && initialRun) {
// Move the motor for a short distance in the opposite direction
for (int i = 0; i < 200; i++) {
digitalWrite(StepX, LOW); // Change direction to LOW for reverse
delayMicroseconds(500);
digitalWrite(StepX, HIGH); // Change direction to HIGH for reverse
delayMicroseconds(1000);
}
// Stop the motor (optional)
digitalWrite(StepX, LOW);
mainRun = true;

// Introduce a delay before starting the reverse run
delay(2000); // Adjust this delay as needed
}
// If the main run is completed and the reverse run hasn't started
else if (!reverseRun && mainRun) {
// Move the motor for a short distance in the opposite direction (reverse)
for (int i = 0; i < 200; i++) {
digitalWrite(StepX, LOW);
delayMicroseconds(500);
digitalWrite(StepX, HIGH);
delayMicroseconds(1000);
}
digitalWrite(StepX, LOW);
reverseRun = true;
}
}

This is the hardware:

1 Upvotes

11 comments sorted by

2

u/QuackSparow Jan 11 '24

Ok first problem, you don’t wanna use “else if” it might cause problems for what it looks like you’re trying to do. Just use 3 if statements.

I’m gonna take a gamble this is your first time using a steppe motor controller like the 1 you have. Well you already did better than me remember to connect the enable pin. I think you just forgot to write to the direction pin. The direction pin does what it sounds like it does, changes the direction. You can look up online which pin on the Arduino mega corresponds to the X motor drive on the cnc shield but here’s your modified code

// If the main run is completed and the reverse run hasn’t started

if (!reverseRun && mainRun) { // Move the motor for a short distance

for (int i=0; i<200< i++) {

digitalWrite(Stepx, LOW);

delayMicroseconds(500);

digitalWrite(Stepx, HIGH);

delayMicroseconds(500); // I did change this delay from 1000 to 500

}

digitalWrite(StepX, LOW);

reverseRun = true;

}

} // :)

3

u/NA_18108 Jan 11 '24

Yeh this is my first ever time, im trying to get to a stage where i have made a linear actuator basically. Thank you for the help, is this the modified code that i just use if instead of else if? Also for it to run in reverse would i just find whatever pin the direction pin on the X is connected to on the arduino and then put that is LOW/HIGH whichever reverses it kinda like this.

digitalWrite(dirPin,LOW); ?

1

u/QuackSparow Jan 11 '24

Also how many years of experience writing Arduino do you have?

Just curious

3

u/NA_18108 Jan 11 '24

Bro years? I got 3 weeks 😅

2

u/QuackSparow Jan 11 '24

Nice, you’re not doing to shabby for 3 weeks

1

u/NA_18108 Jan 11 '24

thaks mate, ive been using these website, reddit and youtube and some ChatGPT (suprisingly very helpful especially breaking things down and giving considerations), i was suprised how much I can do with the interent. These are the resources roughly so far, and i started with tinker cad too.

https://www.youtube.com/watch?v=TMK_fLgpESQ&t=901s

https://howtomechatronics.com/tutorials/arduino/stepper-motors-and-arduino-the-ultimate-guide/

https://forum.arduino.cc/t/cnc-shield-with-o-a-speed-control-potentiometer/689100/5

This too has helped so far as ive just tried to add a potentiometer to the second phase.

3

u/QuackSparow Jan 11 '24

Yeah, those are good. I like to watch DroneBotWorkshop on YouTube, he has a bunch of good videos on basically everything including the A4988 which you’re using

1

u/NA_18108 Jan 11 '24

Also should i connect jumper wire on X STEP/DIR, havnt tried but thought id ask?

2

u/QuackSparow Jan 11 '24

I don’t think I connected the XStep/dir pins on mine

3

u/TheGratitudeBot Jan 11 '24

Thanks for saying thanks! It's so nice to see Redditors being grateful :)

1

u/QuackSparow Jan 11 '24

Yeah you can just copy and paste it in and it SHOULD work. But also change you’re other else if statement to just and if statement. And yes you’re right about the dir pin, I just realized I forgot to write it into the code, but just put the digitalWrite before the for loop. My bad. And if the loops are going to keep running remember to set it to low when the loop is done