tobyb121

You could just wait for the pin state to go low at the end of your setup() function, so it won't enter loop unless the button is pressed.Or create a static or global variable that you set when the button is pressed.

jasonthebass

You could just wait for the pin state to go low at the end of your setup() function, so it won't enter loop unless the button is pressed.Or create a static or global variable that you set when the button is pressed.

void loop() { if(button.uniquePress()){//button.isPressed() will cause the code to execute as long as button is pressed} Button(12,PULLDOWN);Am i correct in thinking after the button is momentary pressed the code runs and "Button(12,PULLDOWN);" holds the button down?

jasonthebass

I made it a function call so you can add debounce and be sure against falsing.You -can- just have a digitalRead(pin) if you don't believe in Murphy.

If i put a function inside "while" I get error on compile.PWM_distiller_working_noLCD_v2.ino: In function 'void setup()':PWM_distiller_working_noLCD_v2:25: error: could not convert 'buttonpress()' to 'bool'

If your power goes out and back on you want your program to be in the state it was previous to the power loss?How about using an EEPROM check to start/stop the desired function at restart?When your function starts it writes a 1 to EEPROM:

I made it a function call so you can add debounce and be sure against falsing.You -can- just have a digitalRead(pin) if you don't believe in Murphy.

If i put a function inside "while" I get error on compile.PWM_distiller_working_noLCD_v2.ino: In function 'void setup()':PWM_distiller_working_noLCD_v2:25: error: could not convert 'buttonpress()' to 'bool'

When I debounce a button/switch I watch for many reads in a row all the same for maybe 5 to 10 ms. There are others who do a more thorough job that works on switches mine might and not and some who check a button and if it's the same 20 to 100 ms later then that's good enough.Debounce doesn't have to be a function, I just wasn't ready to trot out a lot of code and explanation.

For this, consider the pin to be INPUT HIGH and pressing the button grounds the pin to LOW.Not tested, not compiled, it's just easier to write this than make tech sense in English.

while ( buttonNotPressed() ); // this will wait for the button to be pressed and debounced

The function could fit into that while loop but as it is it could also be put into loop() as an if() that does not block other code from running in between checks on the button. This way the while() at the end of setup is the only blocking line, and you -want- it to block!

tobyb121

De-bouncing for this case is not necessary in my opinion. The reason to de-bounce is that when you press the button the electrical contacts will literally bounce due to mechanical vibration in the button, so if you wanted to detect a button press you may get what appears like a couple of very short button presses followed by the actual button press. If you were counting button presses de-bouncing ensures that these spikes are not counted as separate presses. In this case you are just checking if the button is pressed, so as soon as the pin goes HIGH (assuming you're using an active HIGH configuration) you can safely assume that the button has been pressed, and continue regardless of whether the button then bounces.