2

I'm having trouble getting consistent results with the code I am using. I want to run my Arduino for a specific amount of time (say 20 seconds) and collect data from the analog pin with a specific sampling rate (say four samples a second). The code is as follows.

a_pin = 0;
tic;
i = 0;

while toc < 20
    i = i + 1;
    time(i) = toc;
    v(i) = a.analogRead(a_pin);
    pause(.25);
end

Is there a way to set the loop to run a specific time and then in the loop sample at a different rate?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2512053
  • 79
  • 4
  • 12

3 Answers3

0

You can try this:

a_pin = 0;

fs = 4;   % sampling frequency (samplings per second)
mt = 20;  % time for measurements

ind = 1;
nind = 1;
last_beep = 0;
tic;
while toc < mt

    time(ind) = toc;
    v(ind) = a.analogRead(a_pin);

    % wait for appropriate time for next measurement
    while( nind == ind )
        nind = floor(toc*fs) + 1;
    end
    ind = nind;

    % beep every second
    if (ceil(toc) > last_beep)
        beep(); % don't know if this one exist, check docs
        last_beep = ceil(toc);
    end
end 
plesiv
  • 6,935
  • 3
  • 26
  • 34
  • Seems to work great ! Thanks a lot ! Do you have any idea what the maximum sampling frequency is ? – user2512053 Jun 23 '13 at 22:32
  • Please, accept answer (green checkmark on the left side of answer) if it helped you to solve your problem... Maximum sampling frequency would be limited by your hardware (my guess). Limitation on the Matlab's end is the resolution of `tic`/`toc` functions which are *very precise* ([more](http://www.mathworks.com/company/newsletters/articles/improvements-to-tic-and-toc-functions-for-measuring-absolute-elapsed-time-performance-in-matlab.html)). – plesiv Jun 24 '13 at 10:07
  • 1
    New to the forum didn't know you accept solutions... but I just did it. Thanks again for the help – user2512053 Jun 24 '13 at 12:47
  • One more question how could i insert the beep command so the loop beeped every 1 second so for a 20 second loop... it beeps 20 times ? – user2512053 Jun 25 '13 at 16:26
  • @user2512053 I've added it to the answer! – plesiv Jun 25 '13 at 18:16
  • Do you have any idea how this would be implemented in the arduino language? – user2512053 Jun 28 '13 at 21:48
  • Lol no problem. I'm trying to do it but i think i'll still post the question. Your code works great, but i might still need fast sampling rate. – user2512053 Jun 28 '13 at 22:16
  • maybe you can solve my latest question lol. you may recognize the bulk of it http://stackoverflow.com/questions/17540688/matlab-reading-from-serial-port-at-specific-sampling-rate. thanks – user2512053 Jul 09 '13 at 05:37
0

Maximal sampling time for a single Arduino analog read command is around 0.04 s, in practice I'd go minimally 0.05. Adding two read operations is in the order of 2*0.04, in practice more like 0.1 s. I think it is mainly limited by the USB communication speeds.

-1

I am also new at arduino, but having implemented a real time analysis for EEG using it, on practice, I was able to sample 2 analog channels with a samplinf frequency between 57 and 108Hz. It was very variable (calculated through tic/toc), but it is still proper for realtime processing in my case.

My code uses a While loop, a series of memory updates, digital pin manipulations, plot of trace (drawnow) and seems to run smoothly enough

My answer is simply here : 0.0283 sec for sampling 2 analog inputs in my case.

Cheers