如何解决从 I2C 总线读取数据失败流量传感器Arduino项目
我正在尝试从旨在用作呼吸采样器的 Arduino 项目上下文中的流量传感器读取数据。
%% BREATH SAMPLER
%
% Start connection with Arduino board:
clear all
clc
a= arduino();
s=serial("COM4");
%%
% If more than one Arduino is connected to your PC,you have to specify the % board type you will be comunicating with:
%
% |a=arduino("com3","uno")|
%% Define variables
valvePin = 'D10';
pumpPin = 'D11';
buttonPin = 'D9';
flow0 = single(0);
%% Start connections I2c bus
addr = scanI2CBus (a,0); %address bus sensor= device(a,'I2CAddress',char(addr)); %% Configure Arduino Pin
configurePin(a,valvePin,'DigitalOutput')
configurePin(a,pumpPin,buttonPin,'DigitalInput')
%% Start data acquisition:
figure
h = animatedline;
ax = gca;
ax.YGrid = 'on';
stop = false;
startTime = datetime('now');
while ~stop
%Read current sensor value
readSensor = read(sensor,3,'uint8'); MSB= readSensor(1);
LSB= readSensor(2);
%Calculate flow from sensor values flow1=((MSB*256+LSB)-32000)/140;
diff = flow1-flow0;
flow0=flow1;
%Get current time
t = datetime('now') - startTime;
if (flow1>0) && (diff< -5)
writeDigitalPin(a,1);
writeDigitalPin(a,1);
else
writeDigitalPin(a,0);
writeDigitalPin(a,0);
end
%Add points to animation
addpoints(h,datenum(t),flow1)
% Update axes
ax.XLim = datenum([t-seconds(15) t]); datetick('x','keeplimits')
drawnow
%Check Stop condition
stop= readDigitalPin(a,buttonPin);
end
%% Save results to a file
[timeLogs,flow] = getpoints(h);
timeSecs = (timeLogs-timeLogs(1))*24*3600;
T = table(timeSecs',flow','VariableNames',{'Time_sec','Flow'});
prompt = 'Please enter the file name: ';
fileName = input(prompt,'s');
fileName1=[fileName,'.xlsx'];
% Write table to file
writetable(T,fileName1)
% Print confirmation to command line
fprintf('Results table with %g flow measurements saved to file %s\n',...
length(timeSecs),fileName1)
这个想法是能够从呼吸中收集样本,激活一个泵,从呼气阶段收集空气。
在样本获取期间,目标是能够在动画图中可视化所有数据。
显然通过I2C总线与Arduino和流量传感器的连接是正确的,但是从I2C总线读取数据的功能存在问题。传感器输出值恒定,无法正常进行实验。
由于我是一名学生,之前没有通过 matlab 或 I2C 总线通信控制 Arduino 的经验,我将非常感谢您提供任何建议或帮助来解决此问题:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。