How can I change this code to get the line when plotting the graph? Because I tried adding commands related but still there is no line when I am running the codes

1 view (last 30 days)
function [] = i2c_sensor()
a = arduino('COM3', 'UNO');
imu = mpu6050(a,'SampleRate',50,'SamplesPerRead',10,'ReadMode','Latest');
for i=1:10
accelReadings(i,:) = readAcceleration(imu);
display(accelReadings(i,:));
pause(1);
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');
title('Acceleration values from mpu6050');
x_val = animatedline('Color','r');
y_val = animatedline('Color','g');
z_val = animatedline('Color','b');
end
end

Accepted Answer

dpb
dpb on 2 Feb 2021
You're creating a new animatedline object every time, not adding points to the ones intended...that's not how the examples in the documentation show using it.
function [] = i2c_sensor()
a = arduino('COM3', 'UNO');
imu = mpu6050(a,'SampleRate',50,'SamplesPerRead',10,'ReadMode','Latest');
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');
title('Acceleration values from mpu6050');
hx_val = animatedline('Color','r');
hy_val = animatedline('Color','g');
hz_val = animatedline('Color','b');
for i=1:10
accelReadings(i,:) = readAcceleration(imu);
t(i)=????
display(accelReadings(i,:));
addpoints(hx_val(t(i),accelReadings(i,1));
addpoints(hy_val(t(i),accelReadings(i,2));
addpoints(hz_val(t(i),accelReadings(i,3));
drawnow
pause(1);
end
end
ASSUMPTIONS:
  1. x,y,z components of acceleration are in a 3-vector returned from the readAcceleration routine,
  2. can return the sample time for each reading in some manner into variable t which is needed for an abscissa plotting location by addpoints
Read the documentation for both animatedline and addpoints and look at all the examples carefully for further enhancements in using.
  3 Comments
Walter Roberson
Walter Roberson on 3 Feb 2021
addpoints(hx_val, t(i),accelReadings(i,1));
addpoints(hy_val, t(i),accelReadings(i,2));
addpoints(hz_val, t(i),accelReadings(i,3));

Sign in to comment.

More Answers (0)

Categories

Find more on Fractals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!