¿ªÔÆÌåÓý

Re: Oldie newbie


 

Hi,

If the program you are trying to port has a traditional C main routine and it starts out by doing init stuff then finally has a while loop, like 'while(1){ --lines of code--};.

Then put the stuff before the while loop into the "setup" routine. Put the stuff inside the while loop into the "loop" routine.

The only reason not to put everything into the "setup" routine is the "loop" routine allows the Arduino code, automatically included in addition to your code, to handle the serial I/O background processing for the print and get statements used to sent debug messages to a serial terminal. And likely other tasks that need to be done.?

Using the "loop" routine rather than your own while loop lets these routines to get a chance to run every cycle through your code.

If your code does not use a while loop (and is not already an Arduino program using the "loop" routine, then your code may be a higher level of coding using interrupts or a real time "operating" system. In that case everything is going to go in the "setup" routine and the "loop" routine need to be there but can be empty. However, porting that kind of code to the Arduino system may be beyond what the Arduino system was built for.

There are some Arduino libraries like "SoftTimer" ?that give some of the same feel to program in as a real interrupt driven RTOS. When using this library, for example, you put the things your program does in separate routines, rather than a big loop of code, and leave the "loop" routine empty.

Tom, wb6b

Join [email protected] to automatically receive all group messages.