Hello,
I'm trying to move a bipolar stepper motor of a cd rom drive with arduino and an l293d. What I want to achieve is that when I press a button (so when it changes state from 'un-pressed' to 'pressed', independently of how much time does it spend being pressed) I want the piece connected to the stepper to go all the way forward, moving to the other end, and then go back to the start.
This is my code:
#include <Stepper.h>
const int buttonPin = 3;
const int stepsPerRevolution = 400;
int buttonStat; // current button status
int prevButtonStat = LOW; //previous button status
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonStat = digitalRead(buttonPin); // read the current button status
myStepper.step(-stepsPerRevolution); //move the stepper all the way back at the start
delay(5000)
if (prevButtonStat == LOW && buttonStat == HIGH) { // if the button goes from the "unpressed" status to the pressed status (detect status change)
myStepper.step(stepsPerRevolution);
delay(5000)
}
}
prevButtonStat = buttonStat;
}
What I get irl is that the motor continually spins towards the start position, and only if I keep the button pressed for some seconds then it goes in the opposite direction and then gets back to the start and continues spinning towards it.
I can't figure out what am I doing wrong.
I'm open to explanations ofc but I really need help urgently because I need to finish this project for tomorrow. Thanks to anyone who will help.
delay(5000)If that code even compiled, you need to get rid of that delay (and its twin)
Code that doesn't compile cannot run.
Please don't waste our time.
shier:
What I get irl is that the motor continually spins towards the start position, and only if I keep the button pressed for some seconds then it goes in the opposite direction and then gets back to the start and continues spinning towards it.
As @TolpuddleSartre has said you need to get rid of the delay()s if you want a responsive program. If you need to manage the timing the demo Several Things at a Time i illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.
And I can't understand why you are sending the motor back to the start before you ever send it out to the other end? Why doesn't what is now line 27 come AFTER what is now line 32?
I put the delays in order to do some kind of debouncing. Anyway, if I get rid of them, the motor only spins towards the start position, and doesn't go the opposite way if I press the button.
Are you then telling me that I need to debounce to solve this, but with millis() instead of delay?
TolpuddleSartre:
Please don't waste our time.
I'm quite new with Arduino, and unfortunately I need to finish the project in a few hours from now, so I'm trying to look for all the support possible. No need to be this harsh, if you feel like I'm wasting your time don't bother answering, no one forces you.
What I was pointing out was that if you post code that doesn't compile, and observations (which must be bogus, because they cannot possibly be relevant) that you ascribe to that code, you're wasting your time and ours.
This is nothing to do with being new to Arduino, it's simple common-sense.
shier:
I put the delays in order to do some kind of debouncing. Anyway, if I get rid of them, the motor only spins towards the start position, and doesn't go the opposite way if I press the button.
Are you then telling me that I need to debounce to solve this, but with millis() instead of delay?
As you have not posted your revised code we cannot see exactly what you did.
If you need to debounce then, yes, use millis() rather than delay()