problem with code for arduino voltage reading

4 次查看(过去 30 天)
Doing a project with pulse sensor on arduino followed a code online it hit an error im unable to understand im still learning matlab at a basic level can anyone help this is the coding error
Undefined function 'readVoltage' for input arguments of type 'matlab.graphics.axis.Axes'.
Error in mypulsesensor (line 85)
data(i) = readVoltage(a,'A0'); % read voltage on pin A0, store as next data point
  2 个评论
Geoff Hayes
Geoff Hayes 2020-1-2
Muhammad - what is variable a in your
data(i) = readVoltage(a,'A0')
? The error message is suggesting that this may be an axes and not the arduino (which you are trying to read from). You may need to post more of your code so that we can see how you have defined variables for the axes and arduino.
Muhammad Alief Nasarudin
a for the arduino connection ill give the full code here.this is base on a code i found
clear a
a=arduino();
% choice = 1 tells the program to collect data; (automatically calculates heart rate)
% choice = 2 closes all figures
choice = 1;
points = 150; % specify the number of times to measure the output voltage
% Since data is acquired approximately every 0.03 sec, 350
% data points should be about 10 seconds of pulse data
%for some reason 350 points of data is too much
%150 plots a little over 10 seconds of pulse data
% choose the upper and lower bounds of the y axis, the lowest and highest
% Vout values you expect to see. **CHANGE THESE VALUES HERE IF DESIRED.**
lowestvoltage = 1.8;
highestvoltage = 4.5;
% set up a row vector "data" to collect the voltage due to the reflected light
% receieved by the photodiode. Set up row vector "time" to store the time
% index of each data point
format shortg;
timestart = clock; %format to user timers (attempt to change time axis)
begxaxis = 0;
endxaxis = 10;
while (choice == 1) % while user wants to collect new waveforms, poll pulse sensor and plot output
data = zeros(1, points); % initialize vector to hold sensor voltagae data
time = zeros(1, points); % initialize vector to hold time index of each voltage
figure; % create a new figure window for plotting the Vout values
axis([begxaxis endxaxis lowestvoltage highestvoltage]); % set the lower and upper limits of the x and y values
xlabel('time, seconds');
ylabel('Output voltage, volts');
hold on % freeze the graph so that you can plot each new Vout value on the same graph
grid on
grid minor
tstart = tic; % start a timer
data(1) = readVoltage(a,'A0'); %read the voltage on pin A0 and store as the first data point
time(1)= toc(tstart); %store the time index of that data point in "time"
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The following lines of code read the A0 voltage and note the time
% index. These values are stored in "data" and "time", respectively
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
minVoltage = data(1);
maxVoltage = data(1);
beattime = zeros(1,15);
beatcount = 1;
intervals = zeros(1,14);
%{
timenow = clock;
if (etime(timenow,timestart) >= 2)
fprintf('This worked');
begxaxis = begxaxis + 1;
endxaxis = endxaxis + 1;
%will this work:
figure;
axis([begxaxis endxaxis lowestvoltage highestvoltage]);
timestart = timenow;
end
%}
for i=2:points
data(i) = readVoltage(a,'A0'); % read voltage on pin A0, store as next data point
if (data(i) > maxVoltage)
maxVoltage = data(i);
elseif (data(i) < minVoltage)
minVoltage = data(i);
end
time(i) = toc(tstart); % store corresponding time index
%formatting for scrollable plot
dx = 2;
a = gca;
plot(time(i-1:i), data(i-1:i)); %draw the line from the last Vout value to the current Vout value
%actual scroller
%Set appropriate axis limits and settings
set(gcf, 'doublebuffer', 'on');
%Avoiding flickering when updating
set(a,'xlim',[0 dx]);
set(a,'ylim',[min(50) - 1, max(200) + 1]); %REMEMBER TO CHANGE X AND Y
%Generate constants for use in UI Control intialization
pos = get(a, 'position');
Newpos = [pos(1) pos(2)-0.1 pos(3) 0.05];
%Slider and leader room for axis labels
xmax = max(100);
S = ['set(gca,''xlim'',get(cbo,''value'')+[0 ' num2str(dx) '])'];
%UI Control
h = uicontrol('style','slider',...
'units', 'normalized', 'position', Newpos,...
'callback',S,'min',0,'max',xmax-dx);
drawnow;
thresh = maxVoltage * 0.95;
if (data(i) > thresh) && (time(i) > beattime(beatcount)+0.4)
beatcount = beatcount + 1;
beattime(beatcount) = time(i);
end
for i=1:beatcount-1
intervals(i)= beattime(i+1)-beattime(i);
end
avgbeat=median(intervals);
bpm=60/avgbeat; % Calculate average heart rate in beats per minute
title(['Heart Rate = ', num2str(bpm, 4), ' beats per minute (using threshold = ', num2str(thresh,3),' volts)']);
end
choice = menu('Would you like to keep collecting data?', 'Collect a new set of pulse waveforms','Close');
end
%{
while (choice == 2) (Changing axis would be too slow
Replot too much data
finish=false;
set(gcf,'CurrentCharacter','@'); % set to a dummy character
while ~finish
% do things in loop...
% check for keys
k=get(gcf,'CurrentCharacter');
if k~='@' % has it changed from the dummy character?
set(gcf,'CurrentCharacter','@'); % reset the character
% now process the key as required
if k=='q', finish=true; end
end
end
if choice == 2,
close all;
end
%}

请先登录,再进行评论。

采纳的回答

Geoff Hayes
Geoff Hayes 2020-1-2
Muhammad - the problem is with this line of code
a = gca;
where you overwrite the arduino assigned variable with the current axes...and so when you reference a again with
data(1) = readVoltage(a,'A0');
the error occurs because now a is the axes. Consider an alternative name for the axes variable (one that perhaps describes what it references)
hCurrentAxes = gca;
plot(time(i-1:i), data(i-1:i)); %draw the line from the last Vout value to the current Vout value
%actual scroller
%Set appropriate axis limits and settings
set(gcf, 'doublebuffer', 'on');
%Avoiding flickering when updating
set(hCurrentAxes,'xlim',[0 dx]);
set(hCurrentAxes,'ylim',[min(50) - 1, max(200) + 1]); %REMEMBER TO CHANGE X AND Y
%Generate constants for use in UI Control intialization
pos = get(hCurrentAxes, 'position');
or just use gca instead.
You could also rename the variable for the arduino "handle" to something that is more descriptive.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by