Go Down

Topic: setup vs loop (Read 941 times) previous topic - next topic


i have a beginner question!  i understand that setup() runs when the board is initialized and that loop() runs once per cycle.  here's an example to illustrate my question:

let's say i'm building a time machine (obviously this is top secret).  when i start up the machine, i want to enter the current date, then i want to set the day, month and year of my destination.  once this is set, i want to push the "go" button to travel through time.

of course i can do all this in the loop() function, checking my pins to set the date and checking when "go" is pushed.  but i don't want anyone to be able to mess with the dates while the time_travel() function is being run!  that would be dangerous.  also, it seems a bit unnecessary for the loop() to continuously check the date and go controls once they are no longer needed.  so, should i use the setup() function for this?

an example of setup() being used to gather initialization data is the analog calibration sketch that comes with the arduino software:
Code: [Select]
void setup() {
  while (millis() < 5000) {
     ... gather data for calibration

but the time machine example would theoretically wait an undefined amount of time for you get ready for the trip before entering your dates and pressing "go".  something like this perhaps:
Code: [Select]
void setup() {
  for(int i=0; goButton == HIGH; i++) {
     ... read date inputs
     ... read goButton

so my question is: should i be doing this in the setup() function?  or is there a way to do it all within the loop() function?  what is best practice and why?


Oct 14, 2009, 10:35 am Last Edit: Oct 14, 2009, 10:36 am by AWOL Reason: 1
If it's a once-only setup, then there's no reason not to do it in "setup ()".

(make sure your time-travellers don't have access to the reset button)

PS I won't tell a soul about your Top Secret project. Promise.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


thanks, AWOL!

that was a quick reply... or maybe... this thing is working?!


This thread just made my day!


Go Up