Bluetooth - fread issues after flushinput

2 views (last 30 days)
ced chauvet
ced chauvet on 13 Aug 2020
Edited: Walter Roberson on 13 Aug 2020
Good mornig,
I use an accelerometer wich computes values every 0.01 second. In order to develop an application with app designer, I made a code that display the gravitationnal field around -0.98 G. The data is collected from an arduino and goes trough bluetooth to Matlab.
When I don't flush the input incoming from the arduino, my application is too slow then there is a delay between the moment I shake the accelerometer and its display in the application.
So I put a flushinput, the problem mentionned above has been cleared but another appeared.
As you can see there are several errors when I display the data:
This is my code:
properties (Access = public)
myTimer = timer('StartDelay', 0, 'Period', 0.01, 'TasksToExecute', Inf, 'ExecutionMode', 'fixedRate');
obj1;
anim;
i = 0; % Description
etat= 0; % Description
end
methods (Access = private)
function results = drawnow(app,src,event)
if app.etat == 1
y= fread(app.obj1,1, 'float');
addpoints(app.anim,app.i,y)
app.i= app.i+1;
app.UIAxes.XLim = [app.i-20, app.i+20];
drawnow
app.Label.Text = string(y);
flushinput(app.obj1);
if app.i>200
stop(app.myTimer)
end
end
function ConnexionButtonPushed(app, event)
% Find a Bluetooth connection object.
app.obj1 = instrfind('Type', 'bluetooth', 'Name', 'Bluetooth-HC-06:1', 'Tag', '');
% Create the Bluetooth connection object
app.obj1 = Bluetooth('HC-06', 1);
fopen(app.obj1);
app.Label.Text = "connecté";
app.etat=1;
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.myTimer.TimerFcn= {@app.drawnow};
start(app.myTimer)
app.anim = animatedline('Color','k','LineWidth',1,'Parent',app.UIAxes);
end
end
My question is:
How to avoid the wrong data?

Answers (0)

Community Treasure Hunt

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

Start Hunting!