Hi everybody. I am new to Arduino and am currently building my first project that uses a stepper motor. I have purchased plans for this project so I should be fine with all of that along with the supplied sketch. But I think I need to modify the sketch a little for my own personal use. To make it simple, when the start button is pushed the stepper motor turns but I do not want it to start turning right away and would like to add a variable pause or delay or even maybe 3 choices of delays like 3 seconds and 5 seconds and 8 seconds (all to be determined when I finish the actual project). Can this be done using Delay()? How could I make it variable or have a choice of one out of the 3 presets I want/need?
Thanks,
schemer
schemer:
Hi everybody. I am new to Arduino and am currently building my first project that uses a stepper motor. I have purchased plans for this project so I should be fine with all of that along with the supplied sketch. But I think I need to modify the sketch a little for my own personal use. To make it simple, when the start button is pushed the stepper motor turns but I do not want it to start turning right away and would like to add a variable pause or delay or even maybe 3 choices of delays like 3 seconds and 5 seconds and 8 seconds (all to be determined when I finish the actual project). Can this be done using Delay()? How could I make it variable or have a choice of one out of the 3 presets I want/need?
Thanks,
schemer
Lots of ways to do what you want, but look into the BlinkWithoutDelay example in your IDE for a non-blocking method that will teach you something you will use forever in Arduino-land.
BulldogLowell:
Lots of ways to do what you want, but look into the BlinkWithoutDelay example in your IDE for a non-blocking method that will teach you something you will use forever in Arduino-land.
Ok great, I will check that out. I bought a book or two but am awaiting the arrival of them. I also found this:
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
I will have to get further into the actual project to see where the plans show wire connections etc to see if the outputs are already used etc, but I am sure there will be a way.
Thanks,
schemer
One thing that is not made clear in the BlinkWithoutDelay tutorial is that this line:previousMillis = currentMillis;
Is what starts/resets the timer.
The placement of that line in the tutorial creates a repeating timer which is great for blinking a LED,
but for a one shot timer like you will need for your sketch it needs to be placed so that it is executed after the button has been pressed.
Just remember that it must always be placed within a conditional statement otherwise it will reset the timer every pass thru loop().
Hutkikz:
One thing that is not made clear in the BlinkWithoutDelay tutorial is that this line:previousMillis = currentMillis;
Is what starts/resets the timer.The placement of that line in the tutorial creates a repeating timer which is great for blinking a LED,
but for a one shot timer like you will need for your sketch it needs to be placed so that it is executed after the button has been pressed.Just remember that it must always be placed within a conditional statement otherwise it will reset the timer every pass thru loop().
Good to know. Thanks. Could I use a 3-way toggle switch or a rotary switch with resistors to sense what position it is in to start the timer to my choice?
Example:
1k resistance = 3 seconds
2k resistance = 5 seconds
3k resistance = 10 seconds
Trying to visualize the best electrical hardware (switches or input) to select a delay of my choice.
Thanks,
schemer
schemer:
Good to know. Thanks. Could I use a 3-way toggle switch or a rotary switch with resistors to sense what position it is in to start the timer to my choice?
Example:
1k resistance = 3 seconds
2k resistance = 5 seconds
3k resistance = 10 secondsTrying to visualize the best electrical hardware (switches or input) to select a delay of my choice.
Thanks,
schemer
how about a single button that just makes a led flash 1, two or three times?
BulldogLowell:
how about a single button that just makes a led flash 1, two or three times?
So if I get this right, you are suggesting I add 3 momentary contact switches? Then I could press one of the 3 different switches that will correspond to the code of a 3, 5, or 8 second delay and then the code would run the servo motor continuously as per the code or sketch?
Thanks,
schemer
schemer:
So if I get this right, you are suggesting I add 3 momentary contact switches? Then I could press one of the 3 different switches that will correspond to the code of a 3, 5, or 8 second delay and then the code would run the servo motor continuously as per the code or sketch?
Thanks,
schemer
Yup
BulldogLowell:
Yup
Ok, so that would work but... could a potentiometer be used as a variable time control? Just curious if I could have full control through hardware as opposed to possibly having to update the code here and there if those options were not ideal.
Thanks,
schemer
You will have to write code for any of those choices. but all of them would work (rotary, pot. or buttons).
I'll add another:
A single button that increments a counter that determines the number of seconds to delay.
It could increment more than 1 second per press.
Hutkikz:
You will have to write code for any of those choices. but all of them would work (rotary, pot. or buttons).I'll add another:
A single button that increments a counter that determines the number of seconds to delay.
It could increment more than 1 second per press.
Perfect. Now I know I have many options to set my delay. This is for a rotary welding positioner and with TIG welding and many different materials and thickness options, the table does not need to be turning instantly until the material starts to puddle when heated. Thanks for all the help so far, now I need to build it. I have everything to do it (steel, Arduino UNO, control box, switches, LCD shield, screw shield, motor controller, motor, power supplies, etc etc). This is going to be a fun project.
Thanks,
schemer
p.s. More questions to follow but had to get the time delay sorted for my first potential issue.
When I want to hang things up for awhile, I use code like the following: suppose I want a two second delay before proceeding
unsigned long later;
if(<button pressed>) later = millis() + 2000;
do {} while(millis() < later); //stays right here for 2 sec.
unsigned long later;
if() later = millis() + 2000;
do {} while(millis() < later);
That doesn't work across millis() rollover (search for "millis rollover"). You have to do this:
unsigned long pressTime;
if(<button pressed>) pressTime = millis();
do {} while(millis() - pressTime < 2000);
But don't use that code either. It's equivalent to
if (<button pressed>)
delay(2000);
The whole purpose of Blink Without Delay is never to use delay!
Cheers,
/dev
I appreciate all responses and will have to do some testing for sure, but the method that appeals to me the most right now is the:
A single button that increments a counter that determines the number of seconds to delay.
It could increment more than 1 second per press.
That would be very cool as I could easily add a momentary switch and press it 10 times for 10 seconds, or 5 times for 10 seconds if I changed the variable's value. Is there a limit to maximum delay? I am thinking 10 seconds would be good buy maybe more in a rare case. I suppose a beeper could be added to make it even more cool either when pressing the button or when it is counting down. ![]()
Thanks,
schemer