Time spend to make the loop AND modyfing PWM freq

Hello,

Then here are my two questions: -How can I know how much time have spend between 2 moments in the loop (or after a whole loop,...).

-If I change the PWM frequency (I saw it was possible on other posts), will it also change the resolution of it? Is it proportionnal? So should I care? Last question: are all the PWM on the same clock? If not, how can I do to have all the PWM on the same clock

Thank you

  1. yes, should be pretty simple. Define 2 variables: unsigned long time0 = 0; unsigned long time1 = 0; unsigned long time_ms = 0;

at start of your loop, take a time measurement: time0=millis();

at the end, take a 2nd: time1=millis();

then subtract: time_ms = time1 - time 0;

  1. There are 6 PWM outputs on 3 different ports. Have you read this? http://arduino.cc/en/Reference/AnalogWrite

The 6 pwm outputs are generated by the 3 internal timers of the atmega chip, two of then, timer0 and timer2 are 8bit timers, and the timer1 is a 16 bit one, if you change the frequency of this timer you will loose microSeconds, millis and delay and some other functions that depend on millis/micoseconds. Each timer as its own prescaller that gives to him a clock pulse, if I'm not wrong the prescaller values are 1024,256,128,64,4,2,1 or something near that, the main clock is the 16Mhz crystal that powers the rest of the clock circuits of the atmega. So yes you can have two timers with different frequencies, just read about timers in avrfreaks.com the oficial atmel forum for the atmegas micro-controllers, just go to the tutorials sections, that and download the atmega328p datasheet and go to the timers section and start reading it.

For timing how long some code take to execute, the micros() function is probably more useful than the millis() function, since it returns orders of magnitude smaller values.