Newbie Questions - loop and clockspeed

Hi,

I just picked up a RedBoard and had some questions on programming.

I read that every Arduino program starts with “setup()” and “loop()”.

Does “setup()” always run first and only once?

Does loop run at the clock-speed of the chip?

Does it basically loop forever?

If I only want an instruction to happen once then it needs to be in a ‘for’ or ‘if’ statement is that correct?

What parameters does “loop()” take that are useful to beginning programs?

Thank you kindly for your reply.

Simon

I’m not an Arduino expert, but I have used it some.

SimonPuleo:
Does “setup()” always run first and only once?

Yes.

SimonPuleo:
Does loop run at the clock-speed of the chip?

loop() cycle time is the core clock speed divided by the cycles required to execute all of the instructions in the loop() statement.

SimonPuleo:
Does it basically loop forever?

Yes

SimonPuleo:
If I only want an instruction to happen once then it needs to be in a ‘for’ or ‘if’ statement is that correct?

Yes, it would need to be in some kind of conditional statement. If it only needs to run once, maybe setup() might be a good place to execute it. Depends on what it’s doing.

SimonPuleo:
What parameters does “loop()” take that are useful to beginning programs?

loop() doesn’t take any paramaters that I’m aware of… but I’m no expert.

Behind the scenes, the Arduino framework provides a main program that looks like

main(void)
{
    setup();

    while (1)
    {
        loop();
    }
}

/mike

Thanks for getting back to me so quickly.

Best Regards, Simon

While the instructions inside the loop() run at clock speed, the loop() includes extra overhead (ckecking for events) after each execution. If you want your loop to repeat the fastest, use an always-true FOR loop.This won’t change the execution speed between commands in the loop, but will skip the overhead and will loop faster.