|
There may be a bug in how you set up your WHILE loop. You have this:
-------
secsInMinute = 60;
handles.acquisitionPeriod = 1.0; % [seconds]
tic;
while toc/secsInMinute < handles.acquisitionPeriod
[...]
-------
toc returns the time elapsed in the tic in SECONDS. You are dividing this by 60 before comparing to handles.acquisitionPeriod (which is in seconds), so your loop will run for 60 times longer, i.e., it will run for a full minute. Try this:
while toc < handles.acquisitionPeriod
[...]
While you are at it, you may want to rename acquisitionPeriod to be something like acquisitionPeriodinSeconds, to prevent any further unit confusions.
Best,
Gautam
"Petteri T" <petteri.teikari@RemOVEinserm.fr> wrote in message <jg76le$a74$1@newscl01ah.mathworks.com>...
> Dear all,
>
> I downloaded the "MATLAB Support Package for Arduino (aka ArduinoIO Package)" http://www.mathworks.com/matlabcentral/fileexchange/32374-matlab-support-package-for-arduino-aka-arduinoio-package , and got it installed nicely for my Matlab 2011a running on 64-bit Ubuntu 11.04, and tested the command available from: "example_io.m" and managed to control the intensity of 2 LEDs with the PWM output.
>
> However, I would like to control the intensity of the LED depending on the input obtained from the analog inputs (2 pins) and from one digital pin (1 pin) dynamically.
>
> The basic idea of the psychophysics program that I want is the following
>
> Loop of 1 sec
> ------------------------------
> 0-500 msec : LED1 ON and LED2 OFF
> 500-100msec: LED1 OFF and LED2 ON
>
> Once per second the code should update the values for both LED1 and LED2 based on the signals from analog inputs 1,2 and digital input 1.
>
> The problem now is that when I run the code below the "while toc/secsInMinute < handles.acquisitionPeriod" -loop takes significantly longer than the 1 sec that I would want it to take.
>
> So my question is that does someone have experience on using the above-mentioned Matlab Interface for Arduino in "real-time" data acquisition?
>
> (Pseudo)CODE:
>
> handles.flickerPeriod = 0.5; % [seconds]
> handles.acquisitionPeriod = 1.0; % [seconds]
>
> % kinda trivial, but maybe more intuitive to read than just random 60
> % in the code
> secsInMinute = 60;
>
> % initialize state
> state = 0;
> % get previous state
> prev = handles.a.digitalRead(handles.arduinoPin_button);
>
> % init values for loops
> i = 1;
> j = 1;
>
> tic
> while toc/secsInMinute < handles.acquisitionPeriod
>
> % read analog input
> ain = handles.a.analogRead(handles.arduinoPin_joyUP); % digital
> v(1) = 100*ain / 1024; % analog voltage
>
> ain = handles.a.analogRead(handles.arduinoPin_joyDOWN); % digital
> v(2) = 100*ain / 1024; % analog voltage
>
> % read current button value
> % note that button has to be kept pressed a few seconds to make sure
> % the program reaches this point and changes the current button value
> curr = handles.a.digitalRead(handles.arduinoPin_button);
>
> if toc/secsInMinute < handles.flickerPeriod
> handles.a.analogWrite(handles.arduinoPin_PWM(1), pwmValue1);
> handles.a.analogWrite(handles.arduinoPin_PWM(2), 0);
> else
> handles.a.analogWrite(handles.arduinoPin_PWM(1), 0);
> % handles.a.analogWrite(handles.arduinoPin_PWM(2), pwmValue2);
> end
>
> % update PWMs (once per second)
> % simplified
> if curr == 1
> pwmValue1 = pwmValue1 + 1;
> end
> end
|