%% serialport Callback Function
% This callback function is triggered everytime a terminator is available to be read. It reads the first 1000 ASCII terminated string, and plots the result.

function readSineWaveData(src, ~)

% Read the ASCII data from the serialport object
data = readline(src);

% Convert the string data to numeric type and save it in the serialport
% object's UserData property.
src.UserData.Data(end+1) = str2double(data);

% Update the Count value of the serialport object
src.UserData.Count = src.UserData.Count + 1;

% If 1001 data points have been collected from the Arduino, switch off the
% callbacks and plot the data
if src.UserData.Count > 1001
    configureCallback(src, "off");
    plot(src.UserData.Data(2:end));
end
end