【发布时间】:2013-06-28 22:21:23
【问题描述】:
我想运行我的 Arduino 一段特定的时间(比如 60 秒)并收集 来自具有特定采样率的模拟引脚的数据(例如每秒四个样本)。 我得到了在 matlab 中工作的代码......但我想使用 arduino 环境。 请帮我转换一下。
a_pin = 0;
fs = 4; % sampling frequency (samplings per second)
mt = 20; % time for measurements
ind = 1;
nind = 1;
last_beep = 0;
tic;
while toc < mt
time(ind) = toc;
v(ind) = a.analogRead(a_pin);
% wait for appropriate time for next measurement
while( nind == ind )
nind = floor(toc*fs) + 1;
end
ind = nind;
end
好的,这就是我目前的草图。这个测量 10 秒可以吗 每 5 次读数?
int sensePin = 0;
unsigned long starttime = 0;
unsigned long endtime = 0;
int i = 0;
int n;
const int sizeofv = 50;
int v[sizeofv];
void setup(){
pinMode(sensePin, INPUT);
Serial.begin(9600);
}
void loop() {
starttime = millis();
endtime = starttime;
while ((endtime - starttime) <= 10000) // do this loop for up to 1000mS
{
i = i + 1;
v[i] = analogRead(sensePin);
endtime = millis();
delay(5000);
}
for(n=0; n < sizeofv; n++)
{
Serial.print(v[n]);
Serial.print('\n');
}
while(1) { }
}
【问题讨论】:
-
如果您尝试自己先转换它并在遇到困难时返回,这可能是最好的。从 arduino ide 附带的示例草图之一开始,并确保在 arduino.cc 上查看良好的语言参考
-
好的,我分享了我目前所拥有的。不知道它是否在做我想做的事
-
如果不运行它,我认为你所拥有的或多或少会起作用。几个小 cmets... 实际上,您可以在 setup() 函数中完成所有这些操作,并在最后省略 while(1) 循环。您还可以用 Serial.println() 替换您的第二个 Serial.print。为了帮助调试/测试代码,您可以在每次模拟读取之前添加一个 Serial.println 调试语句。然后检查您所期望的时间。
-
还有一个......您应该在调用 delay() 后移动更新 endtime 的位置
-
有没有一种快速的方法可以将我的数组“v”导出为 .mat 文件?
标签: loops time arduino sampling