setup vs loop
i have beginner question! i understand setup() runs when board initialized , loop() runs once per cycle. here's example illustrate question:
let's i'm building time machine (obviously top secret). when start machine, want enter current date, want set day, month , year of destination. once set, want push "go" button travel through time.
of course can in loop() function, checking pins set date , checking when "go" pushed. but don't want able mess dates while time_travel() function being run! that dangerous. also, seems bit unnecessary loop() continuously check date , go controls once no longer needed. so, should use setup() function this?
an example of setup() being used gather initialization data analog calibration sketch comes arduino software:
but time machine example theoretically wait undefined amount of time ready trip before entering dates , pressing "go". something perhaps:
so question is: should doing in setup() function? or there way within loop() function? what best practice , why?
let's i'm building time machine (obviously top secret). when start machine, want enter current date, want set day, month , year of destination. once set, want push "go" button travel through time.
of course can in loop() function, checking pins set date , checking when "go" pushed. but don't want able mess dates while time_travel() function being run! that dangerous. also, seems bit unnecessary loop() continuously check date , go controls once no longer needed. so, should use setup() function this?
an example of setup() being used gather initialization data analog calibration sketch comes arduino software:
code: [select]
void setup() {
while (millis() < 5000) {
... gather data calibration
}
}but time machine example theoretically wait undefined amount of time ready trip before entering dates , pressing "go". something perhaps:
code: [select]
void setup() {
for(int i=0; gobutton == high; i++) {
... read date inputs
... read gobutton
}
}so question is: should doing in setup() function? or there way within loop() function? what best practice , why?
if it's once-only setup, there's no reason not in "setup ()".
(make sure time-travellers don't have access reset button)
ps won't tell soul top secret project. promise.
(make sure time-travellers don't have access reset button)
ps won't tell soul top secret project. promise.
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > setup vs loop
arduino
Comments
Post a Comment