At the end of setup (a bit of pseudocode here)
while (start_button_ not_ pressed());
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'
The while line in my setup() is then highlighted.
unsigned long time;
//unsigned long Cutout = 16800000;
unsigned long Cutout = 30000;
unsigned long Fanon = 20000;
unsigned long Fanoff = 40000;
int fanrelay = 13;
int duty = 614;
int buttonPin = 12;
int buttonState = 0;
Timer1.initialize(500000); // initialize timer1, and set a 1 second period
if (time >= Fanon)
time = millis();
Timer1.pwm(9,duty); // setup pwm on pin 9, 0-1024 max resolution /100*% to get a % duty eg; 614.4 is 60%
if (time >= Cutout)
while(1); // endless loop
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.
byte buttonNotPressed() // returns the opposite of last debounced button state
static unsigned long lastChangeMillis = 0UL;
static lastPinState = HIGH; // button not pressed, pullup not grounded
int pinState = digitalRead( buttonPin ); // most of these int used should be byte, save RAM
if ( pinState != lastPinState ) // pin state has changed, set debounce start time
lastChangeMillis = millis();
else // pin state has not changed, see if it has been so for 10 ms
if ( millis() - lastChangeMillis >= 10 ) // debounced at last!
buttonState = pinState;
lastPinState = pinState;
return buttonState; // will be 1=TRUE for button NOT pressed
2 if's, some variables and a digital read. not so hard?
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!