Go Down

Topic: Using Interrupts to Acquire Data  (Read 154 times) previous topic - next topic

gui93

Jul 05, 2016, 09:34 pm Last Edit: Jul 05, 2016, 09:46 pm by gui93
Hello,

For my current project, I have to acquire data and store it in an array and then analyse the array.

It would be something like:


A[] receives 100 values;

A is analysed and it takes a while to process all the data;


Since the sampling frequency must be as steady as possible, I thought about using Interrupts. After doing a little bit of research (Especially this: http://www.instructables.com/id/Arduino-Timer-Interrupts/?ALLSTEPS ), I came to realize that Interrupts is the way to go for me.

But I do not understand fully how they work. So here are some questions that popped up in my head:

Let's say I set the sampling period to 0.5 seconds, but it takes 1 second to analyse all the data. So I would set the prescaler and the compare match register in such a way that the interrupt runs every 0.5 seconds. So, I would have something like this (I omitted the setup part because it's pretty much copy paste from the link above):

Code: [Select]


volatile float A[100];

void setup(){
//Setting up the registers/prescaler. It's the same code that's in the link above.
//The sampling period is set to be 0.5 seconds.
}

//This should get N samples and store each one of them in the array A.
ISR(TIMER2_COMPA_vect){
  if(i<N){
       A[i]=analogRead(pinA);
       i++;
   }
   else{
       i=0;
   }

}


and in my void loop(), the code would be like:

Code: [Select]

void loop(){
  Analyse(A); // Let's say this takes about 1 second.
}


Does it mean that the first half of my Analyse function is going to be with the old array (the one that was filled out before Analyse was called), then Analyse is going to be interrupted so A can get N new values and then Analyse will resume its calculations with the new data?

How can I make sure that Analyse runs continuously every 100 samples if the ISR is going to interrupt everything the code is doing to acquire new data?

Thank you!

AWOL

#1
Jul 05, 2016, 09:36 pm Last Edit: Jul 05, 2016, 09:37 pm by AWOL
I wouldn't do a 100us ADC read in an ISR.

I don't understand what you mean by an "old array".
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

gui93

I wouldn't do a 100us ADC read in an ISR.

I don't understand what you mean by an "old array".
How can I make sure that the Analyse function runs after every 100 samples without being interrupted if the ISR is going to interrupt everything the code's doing to get new values?

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

gui93

Double-buffering?
I'm sorry for such a dumb question, but could you please elaborate? How can I do that?

Coding Badly


So far, nothing in your description indicates you should be using interrupts.  A busy loop waiting to take the next sample, an outer loop to collect N=100 samples, finished with an analysis seems like what you are describing.



AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

gui93

So far, nothing in your description indicates you should be using interrupts.  A busy loop waiting to take the next sample, an outer loop to collect N=100 samples, finished with an analysis seems like what you are describing.



That's what I did at first, but then I realized that the sampling period wasn't steady enough. That's when I decided to try Interrupts

DrDiettrich

Your code resets the index in the ISR, so that the array will be non-stop overwritten. Set a flag when the array is full, and/or disable the interrupt. Then process the data array.

Is it really required that you don't miss 1-2 readings while processing the array?

Would it be possible to start processing the array, even if it is not full? Then you'd have about 0.5s for the final processing of the data. Or you'll have all the time for processing the values, except for the first few values, which are overwritten by the ISR with the next sample data.

Or double the array capacity, and process the lower half while the upper half is filled.


Coding Badly

That's what I did at first, but then I realized that the sampling period wasn't steady enough.
In which case, you made a mistake.  You can get to within 4 μs of the target without using interrupts.


gui93

In which case, you made a mistake.  You can get to within 4 μs of the target without using interrupts.


How do I do that?
I've tried the delay(), delayMicroseconds() and the micro() (with a while statement), and they all had a 20micros of error between samples

cattledog

Please post the code which had the 20 microsecond sample period error.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy