0
$\begingroup$

Just wanted to clarify some pretty basic Arduino concept:

If I put this code into an arduino board:

double start, endTime;
long int counter = 0;

void setup()
{
  Serial.begin(19200);
start = micros();
}


void loop()
{
  endTime = micros();
  counter++;

  //Point A

  if((endTime - start) > 1000000)
  {
    Serial.println(counter);
    counter = 0;
    start = micros();
  }

}

...I see >38000 value in my serial monitor (for the 'counter' variable).

However, the moment I put in some heavy calculation in 'Point A', the value drops to 150 - 170. This is expected, I guess.

My question is: Is the only way to push up the operational frequency lies in optimising the code/calculation? Or, is there some other way I can get faster execution?

$\endgroup$

2 Answers 2

3
$\begingroup$

The microcontroller you are using has a certain clock frequency. Every operation you make it compute takes a specific (but constant) number of clock cycles to perform. What your code does is it enters the loop function, it performs all the tasks within it and then it jumps back to it's beginning and does everything again.

As you are limited by the number of operations you can perform per second and not the number of loop iterations, you need to either decrease the number of operations you are performing per loop iteration or find a way to increase the number of operations you can do per second.

In order to increase the number of operations you can do per second you need to increase the clock frequency which might mean buying a new chip if the one you are using is already at it's maximum.

$\endgroup$
5
  • 1
    $\begingroup$ Regarding the chip's max freq, most 5V Arduinos have 16MHz crystals or resonators. The ATmega328 rated max freq (at 70C) is 20MHz but it can be overclocked at lower temperatures, eg to about 30MHz $\endgroup$ Mar 12, 2014 at 20:46
  • $\begingroup$ Ok, got the point. Very nicely explained. Just one more slightly related question: is there anything remotely resembling multi-threading in ATmega328? $\endgroup$
    – metsburg
    Mar 13, 2014 at 16:42
  • 1
    $\begingroup$ You can implement concurrent algorithms by using timer interrupts for example and using them to share resources (work a bit on one task and a bit on another one). However, you do not have multiple cores which means you unfortunately cannot achieve real parallelism. $\endgroup$ Mar 14, 2014 at 1:34
  • 1
    $\begingroup$ @metsburg: Please ask about multi-threading as a separate top-level question. Yes, there are several techniques resembling multi-threading that work great in a ATmega328. There's not enough room in this comment to mention even the few approaches I know. The BlinkWithoutDelay approach to cooperative multithreading is very simple but often more than adequate. $\endgroup$
    – David Cary
    Mar 27, 2014 at 13:29
  • 1
    $\begingroup$ @metsburg: Does "How can I create multiple running threads?" answer your question about multi-threading? $\endgroup$
    – David Cary
    Mar 28, 2014 at 4:44
2
$\begingroup$

Correct: you must optimize your code.

The Arduino can do a fixed number of machine instructions per second, so reducing the number of instructions required for the calculation will increase the number of calculations that can be done per second.

$$\text{operational frequency} = \frac{\text{calculations}}{t} = \frac{\frac{\text{instructions}}{t}}{\frac{\text{instructions}}{\text{calculation}}} = \frac{\text{16 MHz}}{\frac{\text{instructions}}{\text{calculation}}}$$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.