I want to ask you if I have voltage sensor from analogread() and I want to take 3 samples from it every 2 ms using timerone library and every sample store it in array with 3 elements
Try here
ok I try it but their is time latency between samples
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
Topics merged
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.
Continued cross posting could result in a time out from the forum.
Could you also take a few moments to [url=https://forum.arduino.cc/index.php?topic=710766.0]Learn How To Use The Forum[/url].
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.
To read three inputs will take a significant portion of (> 300us) of your 2000us sample period.
This needs to be taken into account.
Please remember to use code tags when posting code.
Start TC1 and wait for 2 ms and then acquire the analog signal. The acquisition of the analog signal (int x = analogRead(A0)) takes about 208 us (3328*(1/16000000)) time. This is the "Time Latency" that you have to take into account as mention in Post#7.
The end of 2 ms Time Tick can be known through polling or interrupt strategy.
sp. "106us"
Your 300 us and mine 208 us are not too far off. I took the help of the following program to measure the execution time of int x = analogRead(A0) instruction.
void setup()
{
Serial.begin(9600);
TCCR1A = 0x00; //Normal UpCounting Mode
TCCR1B = 0x00; //T1 is OFF
TCNT1 = 0x00; //Initial Value
TCCR1B = 0x01; //1/1 clck
int x = analogRead(A0);
TCCR1B = 0x00;
Serial.println(TCNT1, DEC); //3328 cycles
}
void loop()
{
}
Your 208 referred to a single analogRead
My 300 referred to three reads.
Yes! Three consecutive read operations on three analog channels take about 432 us (> 300).
What is this 106 us? Is it for reading a single analog channel? How do you know about this figure?
By measuring it, and by reading the datasheet ![]()
You are getting 106 us; but, I am getting 208 us to read a single channel; why is here such a big difference?
Maybe the datasheet has your answer... (Hint: you only timed one specific read)
No, I want three reads between them 2 ms and the signal sine wave is 20 ms
You can set the ADC to start a conversion when Timer1 overflows. Set Timer1 to overflow at 32000 ticks (TOP = 31999). In the ADC Conversion Complete interrupt, grab the ADC results and store them in the buffer if room is available.
At what clock frequency (clkTC1)?
Arduino default: 16MHz. ![]()
Or you could do the math yourself:
2 milliseconds = 32000 ticks
32000 / 2 = 16000 ticks per millisecond = 16 MHz
Use any of the PWM Waveform Generation Modes that let you set TOP in one of two registers:
ICR1: WGM8, WGM10, WGM14
OCR1A: WGM9, WGM11, WGM15.
I can see how setting a "Preset Count" could be used for a one-time timer but I don't see an easy way to get a periodic timer (every 2 milliseconds) from it. In the overflow ISR you would have to read the timer count, add the number of clock cycles it will take you to calculate and load an Adjusted Preset Count, subtract that from the Preset Count, and load that new Adjusted Preset Count. (Things get really weird if the timer has passed the Preset Count, leaving you with a negative Adjusted Preset Count.)
