clear all,clc
global s d t z_v
h = @(~, ~) DIOTimerFcn;
h1 = @(~, ~) DIOStartFcn;
h2 = @(~, ~) DIOStopFcn;
obj1 = timer('StartFcn',h1,'TimerFcn', h,'StopFcn',h2, 'Period', 0.5, 'ExecutionMode', 'fixedRate');
obj1.TasksToExecute = 20;
start(obj1)
function DIOStartFcn
global s d
s = arduino('COM4', 'Uno', 'Libraries', 'I2C')
d = device(s,'I2CAddress','0x19')
dec2hex(readRegister(d,hex2dec('0F')))
writeRegister(d,hex2dec('20'),hex2dec('7F'))
end
function DIOTimerFcn
global s d t z_v
persistent h
if isempty(h)
h = 1;
end
t(h) = datenum(now);
z_v(h)= u2i(readRegister(d,hex2dec('2D')));
h = h +1;
end
function DIOStopFcn
global s d
clear s d;
end
function y = u2i(u)
if u< 127
y = u;
else
y = u -256;
end
y = 4*y/256;
end