I have a complicated sketch that runs lights and motors on a kinetic sculpture that I have created.
I have added a button to start the sketch and it works great except for one thing.
When the sketch is running and you press the button it starts from the beginning again. I only want the button to work if the sketch is NOT running.
I have tried using simple if statement in setup but can't get it to work.
This is my void setup code that is working with my attempt at the if statement commented out.
Can anyone help? I should add that I am legally blind sp please be patient with me.
void setup(void)
{
//if (digitalRead (6 = LOW) {
button.setDebounceTime(50); // set debounce time to 50 milliseconds
while (!button.isPressed()) button.loop(); // MUST call the loop() function first
InitTables(); }
void loop(void)
{
static uint8_t nTable = 0;
// check if we need to switch table
if (switchState())
{
nTable++;
InitTables();
}
// run with the selected table
switch(nTable)
{
case 0: MultiBlink(T1, ARRAY_SIZE(T1)); break;
default: nTable = 0;
}
}
Hi @chardifacts
Set a boolean type variable to indicate that your project is stopped (false) or running (true).
Then test this variable and only allow the button to be activated if it is false.
How do you expect to read something when the code that does the reading is not running?
Explain BETTER what you want to do!
E.G. I want the loop to run through, then wait until a button is pressed to begin again, but the button should be ignored while the loop is running.
Thank you for your help.
I think i understand what you are saying, but what i don't understand is the How to do it.
The button uses ezbutton.h to run function. The ezbutton is called out in the beginning of my sketch.
ezButton button(7); // create ezButton object that attach to pin 7;
Should the variable irbe right after the the ezbutton button(7); at the beginning of the setch or somewhere else?
Should the test be an if statement like i have commented out in my setup or something else?
Sorry that I may appear to be dense, but I am just a blind guy trying to learn. and thanks again for your help.
The setup does what I want it to.
Nothing happens until you push the button and then the sketch runs, BUT if you push the button again while the setch is running it starts from the beginning. What I need to add is:
Once the button is pushed and the sketch is running, I don't want the button push to do anything until the setch has stopped ( there is language to stop it after secified time)
Is this clearer?
Drop 'the blind guy' nonsense, as registered legally blind & legally deaf myself, I don't recall ever mentioning it as it has NOTHING to do with Getting or giving help here!
I'm not asking you to to do it! I'm asking you to define what it is you want to do.
OK :
When you push the start button, run the sketch & set a flag variable, when the sketch ends, reset the flag variable and only read the button when the flag is in its reset state.
Try this (with the button library line removed).
Leo..
const byte buttonPin = 2; // button between pin and ground, NO resistor
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // pin is HIGH when button is not pressed
while(digitalRead(buttonPin); // stays in while loop when pin reads HIGH
}
void loop() {
}
@Wawa
Thanks, this does almost everything I need,except that once pushed the sketch never returns to setup so the button cannot be pushed again.
I need it to be able to do this:
Push button and sketch starts and runs until it reaches the stop commands (in the sketch)
While the sketch is running, i.e. power is supplied to pin 6, the button is disabled.
Sketch completes and no power to pin 6 - you can push the button and start sketch again.
The problem with your solution is that it never returns to setup and the button never works again. Thats why I was using the easybutton.
That's normal operation. setup() runs one time when the processor starts up then execution drops into loop() and stays there until a restart. You need to sense the button and run whatever code is needed within loop().
A general outline would be: Sense the switch closure (debounce as necessary), generate a state change on that closure, use the state change to set a boolean true, the true state of the boolean enables some code to run continuously 'til completion, which code then resets the boolean to false in readiness for the next pushbutton closure
The code you were given by Wawa was nothing more than a demonstration of how to get the first part of your requirement going. Obviously the code never gets back to "setup()" because "setup()" is only ever to do what it says, sets up things ready do do what you need to do.
You are now describing a repetitive process, which is exactly for what the loop() is designed. So in the crudest form, your loop() consist of two steps - determining whether the switch is pressed, and executing the code you want when it is pressed. Having completed the second, the loop() automatically comes back to the first.
Of course, the whole process is much more involved than that. There are matters of keeping track (with variables or "switches") of whether the button is still pressed or has been released and pressed again during or since your "payload" code is running, and avoiding the use of "while()" statements.
const byte buttonPin = 2; // any UNO pin, except 0,1,13
boolean stopFlag = true; // start "not executing"
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // button between pin and ground, NO resistor
}
void loop() {
if (!stopFlag) {
// sketch that changes stopFlag to true when finished
// stopFlag = true;
} else {
while (digitalRead(buttonPin)); // wait here until button is pressed
stopFlag = false; // after button was pressed
}
}