This is the code I wrote and the problem is that i fail to do what commands specified in
last lines of command
#include <Stepper.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
int buttonPullupPin_0 = 11;
int butonPullupPin_1 = 10;
int val_0 = 0;
int val_1 = 0;
AccelStepper stepper(AccelStepper::DRIVER, 4, 3);
void setup()
{
pinMode(buttonPullupPin_0, INPUT);
pinMode(butonPullupPin_1, INPUT);
stepper.setMaxSpeed(100);
stepper.setSpeed(90);
stepper.setEnablePin(9);
stepper.disableOutputs();
}
void loop()
{
val_0 = digitalRead(buttonPullupPin_0);
val_1 = digitalRead(butonPullupPin_1);
if (val_0 == HIGH)
{
stepper.enableOutputs();
}
if (val_1 == LOW)
{
stepper.runSpeed(); //I want to continue this action until val_1 == HIGH plus a certain time delay
}
if (val_1 == HIGH) // here i want after reading the button to delay the execution without stopping action before a certain time then execute the action that is conditioned by reading this state of butonPullupPin_1
{
stepper.disableOutputs();//I want to delay this action against reading (val_1 == HIGH)
}
}
if (val_1 == HIGH) // here i want after reading the button to delay the execution without stopping action before a certain time then execute the action that is conditioned by reading this state of butonPullupPin_1
{
delay(5000);
stepper.disableOutputs();//I want to delay this action against reading (val_1 == HIGH)
}
Is that what you want / need?
Now delay() is probably not the way to go as it blocks and you might want to apply the principles of blink-without-delay (example code comes with the IDE).
If i push the button to stop the stepper, i do not want to stop it instantly, I consider that I pressed the stop button to stop the stepper, but stop the with 2 seconds delay from when the button stop was pressed .
TeodorBogdan:
If i push the button to stop the stepper, i do not want to stop it instantly,
You need to save the value of millis() when the button is pressed and then repeatedly compare the saved value with the latest value with code something like this
if (millis() - buttonPressedMillis >= 2000) {
// time is up, so do your stuff
}
The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing.
Thank you, it seems like this is the solution but I do not figure out how to adapt it to my code I spent a lot of hours looking for it and probing codes that my head hurts if you could help me somehow to fit in my code would be very grateful
I know I used delay; it was just to get clear what you need.
So if the button is pushed you set the start time (from Robin2's code) buttonPressedMillis to the currentTime (millis). And next you test if some time has lapsed; you do so independent of the state of the button.
unsigned long currentTime;
unsigned long buttonPressedMillis = 0;
void loop()
{
currentTime = millis();
...
...
// if the button is pressed, set the start time of the delay
if (val_1 == HIGH)
{
buttonPressedMillis = currentTime;
}
// if the delay was started and the delay time has lapsed, stop
if(buttonPressedMillis != 0 && currentTime - buttonPressedMillis >=2000)
{
stepper.disableOutputs();//I want to delay this action against reading (val_1 == HIGH)
buttonPresedMillis = 0;
}
}
This is the code I've written probably did not understand well what I had to do, I am newbie
#include <Stepper.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
int buttonPullupPin_0 = 11;
int butonPullupPin_1 = 10;
int val_0 = 0;
int val_1 = 0;
AccelStepper stepper(AccelStepper::DRIVER, 4, 3);
void setup()
{
pinMode(buttonPullupPin_0, INPUT);
pinMode(butonPullupPin_1, INPUT);
stepper.setMaxSpeed(100);
stepper.setSpeed(90);
stepper.setEnablePin(9);
stepper.disableOutputs();
unsigned long currentTime;
unsigned long buttonPressedMillis = 0;
}
void loop()
{
currentTime = millis();
val_0 = digitalRead(buttonPullupPin_0);
val_1 = digitalRead(butonPullupPin_1);
if (val_0 == HIGH)
{
stepper.enableOutputs();
}
if (val_1 == LOW)
{
stepper.runSpeed(); //I want to continue this action until val_1 == hight plus a certain time delay
}
if (val_1 == HIGH) // here i want after reading the button to delay the execution without stopping action before a certain time then execute the action that is conditioned by reading this state of butonPullupPin_1
{
buttonPressedMillis = currentTime;
}
if(buttonPressedMillis != 0 && currentTime - buttonPressedMillis >=2000)
{
stepper.disableOutputs();//I want to delay this action against reading (val_1 == HIGH)
buttonPresedMillis = 0;
}
}
AND THIS IS THE ERROR I RECEIVE:
Control_motor_start_stop_si_acceleratie_cu_doua_butoane_push.ino: In function 'void loop()':
Control_motor_start_stop_si_acceleratie_cu_doua_butoane_push:27: error: 'currentTime' was not declared in this scope
Control_motor_start_stop_si_acceleratie_cu_doua_butoane_push:41: error: 'buttonPressedMillis' was not declared in this scope
Control_motor_start_stop_si_acceleratie_cu_doua_butoane_push:43: error: 'buttonPressedMillis' was not declared in this scope
Control_motor_start_stop_si_acceleratie_cu_doua_butoane_push:46: error: 'buttonPresedMillis' was not declared in this scope
Buttons work well, so far i have managed to control the stepper well, the problem is that, at the push of the second button, the stepper stops instantly, what I want to do is to press the button, rotate the stepper another 2-3 seconds and then stop. then pressing the first button to start the stepper again.
the problem is, i don't know how to write in the line of code that delay enforcement operations such delays from the time the button was pressed, to stop the stepper.
This is the code that works, but stops the motor instantly, with no delay from pressing the button:
#include <Stepper.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
int buttonPullupPin_0 = 11;
int butonPullupPin_1 = 10;
int val_0 = 0;
int val_1 = 0;
AccelStepper stepper(AccelStepper::DRIVER, 4, 3);
void setup()
{
pinMode(buttonPullupPin_0, INPUT);
pinMode(butonPullupPin_1, INPUT);
stepper.setMaxSpeed(100);
stepper.setSpeed(90);
stepper.setEnablePin(9);
stepper.disableOutputs();
}
void loop()
{
val_0 = digitalRead(buttonPullupPin_0);
val_1 = digitalRead(butonPullupPin_1);
if (val_0 == HIGH)
{
stepper.enableOutputs();
}
if (val_1 == LOW)
{
stepper.runSpeed(); //I want to continue this action until val_1 == hight plus a certain time delay
}
if (val_1 == HIGH) // here i want after reading the button to delay the execution without stopping action before a certain time then execute the action that is conditioned by reading this state of butonPullupPin_1
{
stepper.disableOutputs();//I want to delay this action against reading (val_1 == HIGH)
}
}
I think it's fair that I have previously suggested, but did not know exactly how and where to add the lines suggested, if anyone can help me, i ask him to modify this code of mine, to make it as it should.
#include <Stepper.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
int buttonPullupPin_0 = 11;
int butonPullupPin_1 = 10;
int val_0 = 0;
int val_1 = 0;
unsigned long timeStart; //NEW
unsigned int timeEnd = 2000; //NEW
bool timing = false; //NEW
AccelStepper stepper(AccelStepper::DRIVER, 4, 3);
void setup()
{
pinMode(buttonPullupPin_0, INPUT);
pinMode(butonPullupPin_1, INPUT);
stepper.setMaxSpeed(100);
stepper.setSpeed(90);
stepper.setEnablePin(9);
stepper.disableOutputs();
}
void loop()
{
val_0 = digitalRead(buttonPullupPin_0);
val_1 = digitalRead(butonPullupPin_1);
if(val_1 == LOW) //NEW
timeStart = millis(); //NEW
if(millis() - timeStart < timeEnd) //NEW
timing = true; //NEW
else timing = false; //NEW
if (val_0 == HIGH)
{
stepper.enableOutputs();
}
if (val_1 == LOW)
{
stepper.runSpeed(); //I want to continue this action until val_1 == hight plus a certain time delay
}
// NEW_____________
if (val_1 == HIGH && timing == false) // here i want after reading the button to delay the execution without stopping action before a certain time then execute the action that is conditioned by reading this state of butonPullupPin_1
{
stepper.disableOutputs();//I want to delay this action against reading (val_1 == HIGH)
}
}
Thanks a lot for code changes and prompt response. I could not check the code only today, with the code you wrote this happens:
I push short "buttonPullupPin_0" (first button),---> the stepper starts;
I push short "butonPullupPin_1" (the second button),----> and the stepper stops, and when i release the stepper starts again;<---- i don't need that
i hold long pressed for 2,000 milliseconds "butonPullupPin_1" (the second button), the stepper stops and starts no more, than briefly pressing the first button "buttonPullupPin_0" and the stepper starts again.
What I need is:'--->
When i briefly press the second button (butonPullupPin_1), which is a button with return, to stop the stepper only after 2000 milliseconds, so press the button, release him, stepper still going 2000 milliseconds, and then stops, untill briefly pressing the first button (buttonPullupPin_0).
Otherwise the code is good and functional.
Can you rezolve this part of code please?
Thanks
Hello again, Teodor, sorry for delay, I believe (hope) this modification will work the way you want, let me know if not.
#include <Stepper.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
int buttonPullupPin_0 = 11;
int butonPullupPin_1 = 10;
int val_0 = 0;
int val_1 = 0;
unsigned long timeStart; //NEW
unsigned int timeEnd = 2000; //NEW
bool timing = false, hold = false; //NEW
AccelStepper stepper(AccelStepper::DRIVER, 4, 3);
void setup()
{
pinMode(buttonPullupPin_0, INPUT);
pinMode(butonPullupPin_1, INPUT);
stepper.setMaxSpeed(100);
stepper.setSpeed(90);
stepper.setEnablePin(9);
stepper.disableOutputs();
}
void loop()
{
val_0 = digitalRead(buttonPullupPin_0);
val_1 = digitalRead(butonPullupPin_1);
if (val_0 == HIGH)
{
stepper.enableOutputs();
}
if (val_1 == LOW)
{
stepper.runSpeed(); //I want to continue this action until val_1 == hight plus a certain time delay
}
if(val_1 == HIGH && timing == false) //NEW
{
timeStart = millis(); //NEW
timing = true;
hold = true;
}
if(millis() - timeStart > timeEnd) //NEW
timing = false; //NEW
// NEW___________________________
if(hold == true && timing == false) // here i want after reading the button to delay the execution without stopping action before a certain time then execute the action that is conditioned by reading this state of butonPullupPin_1
{
stepper.disableOutputs();//I want to delay this action against reading (val_1 == HIGH)
hold = false; //NEW
}
}
Hello again, sorry for the delay, but work on this project outside of my fulltime job.
The code you wrote it does its job, but when pressed butonPullupPin_1, the stepper stops for a split second then there goes 2000 milliseconds ... The problem is i don’t want to make this short stop because the button returns to its original state, but i have a sensor, which if stops in that place remains in state HIGH, and at loop resume does not start the stepper, because this condition stops him, and i need that stepper to going to the next least until the sensor detects the object and reaches the Low condition for the loop to resume normal.
So how can I remove the short stop that happening before putting a final end after passing the 2,000 milliseconds?
if (val_1 == LOW)
{
stepper.runSpeed(); //I want to continue this action until val_1 == hight plus a certain time delay
}
The call to runSpeed() should be in loop() and not inside any qualifying code. It should be called as often as possible. There is no need to stop calling it - when the stepper reaches its destination it won't move.