How to plot a graph? I'm getting a blank graph.

1 visualizzazione (ultimi 30 giorni)
clear
close all
clc
a = arduino('COM6','MEGA2560');
v=0;
i = 1;
while (i<50)
v = readVoltage(a,'A11');
if(v>2.2)
writeDigitalPin(a,'D13',0);
else
writeDigitalPin(a,'D13',1);
plot(i,v,"r")
ylim([0 5]);
xlim([0 5]);
hold on
i = i +1 ;
end
end
  1 Commento
Star Strider
Star Strider il 15 Feb 2020
I cannot run your code, so I am not posting this as an Answer.
However, it appears that you are plotting in a loop, so plot with a marker instead:
plot(i,v,'pr')
This should produce a series of red stars.

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 15 Feb 2020
I'm not sure how long the loop takes, but it appears that it should take just milliseconds since I don't see any pause() command in there. So why can't you store all the values and plot them all at once after the loop? If it does take some time, you can put a drawnow inside the loop to force it to refresh the screen (otherwise it might just wait until it gets a chance):
clear
close all
clc
% Instantiate the arduino object.
arduinoObject = arduino('COM6','MEGA2560');
% Start polling the arduino device.
numPoints = 50; % Max number of points to get. The failsafe.
v = zeros(numPoints, 1);
i = 1;
while (i < numPoints)
% Read from the arduino pin.
v(i) = readVoltage(arduinoObject,'A11');
% Write to the arduino pin.
if(v(i) > 2.2)
writeDigitalPin(arduinoObject, 'D13', 0);
% Do we need to reset i to 1 here???
else
writeDigitalPin(arduinoObject, 'D13', 1);
plot(i, v(i), 'r.', 'MarkerSize', 30);
ylim([0, 5]);
xlim([0, 5]);
grid on;
hold on;
drawnow; % Force immediate screen refresh/repaint.
i = i + 1 ;
end
end
% Now finish up by plotting everything.
hold off;
cla;
plot(v, 'r-.', 'MarkerSize', 30);
ylim([0 5]);
xlim([0 5]);
grid on;
xlabel('Index', 'FontSize', 18);
ylabel('PinValue', 'FontSize', 18);
title('Arduino Signal', 'FontSize', 18);

Più risposte (0)

Categorie

Scopri di più su Graphics Objects in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by