Arduino 试图找出两个模拟传感器的两个峰值之间的时间差
[在此处输入图像描述][1][在此处输入图像描述][1]我有两个模拟传感器连接到我的Arduino板。每个传感器将随时间产生脉冲波(正弦波) 我希望获取的代码的功能是获取每个传感器峰值的时间,并最终计算两个传感器峰值的时间差(t1和t2的差值,第二个t1和第二个t2的差值等) 我使用的峰值检测方法是通过基于阈值的检测(附加图像以获取方法信息)。这是我目前拥有的代码。我现在面临的问题是,即使没有传感器的输入,串行监视器也会显示时间差(PTT)的随机值 非常感谢您对本主题的帮助。谢谢Arduino 试图找出两个模拟传感器的两个峰值之间的时间差,arduino,Arduino,[在此处输入图像描述][1][在此处输入图像描述][1]我有两个模拟传感器连接到我的Arduino板。每个传感器将随时间产生脉冲波(正弦波) 我希望获取的代码的功能是获取每个传感器峰值的时间,并最终计算两个传感器峰值的时间差(t1和t2的差值,第二个t1和第二个t2的差值等) 我使用的峰值检测方法是通过基于阈值的检测(附加图像以获取方法信息)。这是我目前拥有的代码。我现在面临的问题是,即使没有传感器的输入,串行监视器也会显示时间差(PTT)的随机值 非常感谢您对本主题的帮助。谢谢 const i
const int sensorPin2= A1;
int Max1 = 0;
int Max2=0;
int sensorValue1;
int sensorValue2;
int threshold = 100;
unsigned long time1=0;
unsigned long time2=0;
unsigned long ptt=0;
unsigned long realtime1=0;
unsigned long realtime2=0;
void setup() {
Serial.begin(9600);
}
void loop()
{
sensorValue1 = analogRead(sensorPin1);
sensorValue2= analogRead(sensorPin2);
//for detection of peak for sensor1
if (sensorValue1 > Max1)
{
Max1 = sensorValue1;
time1=millis();
}
if (sensorValue1 <= threshold && Max1> threshold)
{
Serial.print(Max1);
Serial.print("\t");
Serial.print(time1);
Serial.print("\t");
realtime1=time1;
Max1=0;
}
//for detection of peak for sensor2
if (sensorValue2>Max2)
{
Max2= sensorValue2;
time2=millis();
}
if (sensorValue2<=threshold && Max2> threshold)
{
Serial.print(Max2);
Serial.print("\t");
Serial.print(time2);
Serial.print("\t");
realtime2= time2;
Max2=0;
}
//time difference between peaks from both sensors
ptt=abs(realtime1-realtime2);
Serial.println(ptt);
ptt=0;
}```
[1]: https://i.stack.imgur.com/DYJzg.png
const int sensorPin2=A1;
int Max1=0;
int Max2=0;
int-sensorValue1;
int-sensorValue2;
int阈值=100;
无符号长时间1=0;
无符号长时间2=0;
无符号长ptt=0;
无符号长realtime1=0;
无符号长realtime2=0;
无效设置(){
Serial.begin(9600);
}
void循环()
{
sensorValue1=模拟读取(sensorPin1);
sensorValue2=模拟读取(sensorPin2);
//用于检测传感器1的峰值
如果(传感器值1>最大值1)
{
Max1=传感器值1;
时间1=毫秒();
}
if(传感器值1阈值)
{
串行打印(Max1);
串行打印(“\t”);
串行打印(时间1);
串行打印(“\t”);
realtime1=time1;
Max1=0;
}
//用于检测传感器2的峰值
如果(传感器值2>最大值2)
{
Max2=传感器值2;
时间2=毫秒();
}
if(传感器值2阈值)
{
串行打印(Max2);
串行打印(“\t”);
串行打印(时间2);
串行打印(“\t”);
realtime2=time2;
Max2=0;
}
//两个传感器峰值之间的时间差
ptt=abs(实时1-实时2);
序列号。打印号(ptt);
ptt=0;
}```
[1]: https://i.stack.imgur.com/DYJzg.png
一个可能的原因可能是:当您的输入没有连接到任何东西时,它们是浮动的。这里举例说明:
基本上这意味着他们会吐出随机数,这将随机触发你的峰值算法,这反过来会给你随机的ptt结果
另一个有趣的事实是:,它说:
注意事项和警告,因为abs()函数的使用方式
实现时,避免在括号内使用其他函数,可能会
导致错误的结果
另外,当ptt、realtime1和realtime2都是无符号变量时,abs()会给出错误的值,这些变量不能为负
我用Arduino Nano和两个potis建立了一个小测试装置来模拟模拟输入。我已经编辑了您的程序,并在每次更改时插入了注释
const int sensorPin1= A0;
const int sensorPin2= A1;
int Max1 = 0;
int Max2 = 0;
int sensorValue1;
int sensorValue2;
int threshold = 100;
// this deadband resolves jitter when the sensorValue is exactly
// at the threshold value
#define thresh_deadband 2
unsigned long time1=0;
unsigned long time2=0;
// making this signed long will eliminate the error of huge ppt values
// because now you can calculate more easely negative numbers and you
// still have about 600 hours run time before overflow
long ptt=0;
long realtime1=0;
long realtime2=0;
void setup() {
Serial.begin(9600);
}
void loop()
{
sensorValue1 = analogRead(sensorPin1);
sensorValue2 = analogRead(sensorPin2);
//for detection of peak for sensor1
if (sensorValue1 > Max1)
{
Max1 = sensorValue1;
time1 = millis();
}
if (sensorValue1 <= threshold && Max1 > threshold + thresh_deadband)
{
Serial.print(Max1);
Serial.print("\t");
Serial.print(time1);
Serial.print("\t");
realtime1=time1;
Max1=0;
}
//for detection of peak for sensor2
if (sensorValue2>Max2)
{
Max2= sensorValue2;
time2=millis();
}
if (sensorValue2<=threshold && Max2> threshold + thresh_deadband)
{
Serial.print(Max2);
Serial.print("\t");
Serial.print(time2);
Serial.print("\t");
realtime2= time2;
Max2=0;
}
// this block makes sure ptt is only calculated and showed once
// there is new values for realtime1 and realtime2
if(realtime1 > 0 && realtime2 > 0) {
// time difference between peaks from both sensors
// see https://www.arduino.cc/reference/en/language/functions/math/abs/
// apparently your not supposed to execute any funtions inside the abs
// brackets. Either uncomment the abs line or just use at is and see by
// the value if realtime1 or 2 is smaller
ptt=(realtime1-realtime2);
// ptt = abs(ptt);
Serial.println(ptt);
realtime1 = 0;
realtime2 = 0;
}
}
const int sensorPin1=A0;
常数int传感器PIN2=A1;
int Max1=0;
int Max2=0;
int-sensorValue1;
int-sensorValue2;
int阈值=100;
//该死区解决了当传感器值精确设置时的抖动问题
//在阈值处
#定义阈值死区2
无符号长时间1=0;
无符号长时间2=0;
//将此符号设置为长将消除ppt值过大的错误
//因为现在你可以更容易地计算负数
//在溢出之前仍有大约600小时的运行时间
长ptt=0;
长realtime1=0;
长realtime2=0;
无效设置(){
Serial.begin(9600);
}
void循环()
{
sensorValue1=模拟读取(sensorPin1);
sensorValue2=模拟读取(sensorPin2);
//用于检测传感器1的峰值
如果(传感器值1>最大值1)
{
Max1=传感器值1;
时间1=毫秒();
}
if(传感器值1阈值+阈值死区)
{
串行打印(Max1);
串行打印(“\t”);
串行打印(时间1);
串行打印(“\t”);
realtime1=time1;
Max1=0;
}
//用于检测传感器2的峰值
如果(传感器值2>最大值2)
{
Max2=传感器值2;
时间2=毫秒();
}
if(传感器值2阈值+阈值死区)
{
串行打印(Max2);
串行打印(“\t”);
串行打印(时间2);
串行打印(“\t”);
realtime2=time2;
Max2=0;
}
//此块确保仅计算和显示一次ptt
//realtime1和realtime2有新的值
if(realtime1>0&&realtime2>0){
//两个传感器峰值之间的时间差
//看https://www.arduino.cc/reference/en/language/functions/math/abs/
//显然你不应该在腹肌内执行任何功能
//括号。取消对abs行的注释,或仅在is处使用,然后按查看
//realtime1或2的值较小
ptt=(实时1-实时2);
//ptt=abs(ptt);
序列号。打印号(ptt);
realtime1=0;
realtime2=0;
}
}
峰值检测的实现看起来不错
不过,你的计时代码有点不对劲
你在做什么:
read sensor 1
read sensor 2
if value 1 is a new max:
store timestamp 1
if it is a new peak:
report new peak through serial
register timestamp 1 as time for peak 1
if value 2 is a new max:
store timestamp 2
if it is a new peak:
report new peak through serial
register timestamp 2 as time for peak 2
calculate time difference and send it through serial
这方面的一些问题:
a) 您不存储测量时间,而是存储发现它是新的最大值时的时间。由于在传感器2的测量和时间戳的创建之间进行串行通信,因此始终会为传感器2引入延迟。
只要没有1个峰值,代码中串行通信的所有延迟都将随时间累积
b) 您正在报告每个循环周期中的时差。即使你还没有两个峰值
以下是您应该做的:
read sensor 1
if value 1 is a new max 1:
store timestamp 1
read sensor 2
if value 2 is a new max 2:
store timestamp 2
if max1 is a new peak and timestamp1 is > timestamp 2:
report time difference
if max2 is a new peak and timestamp2 is > timestamp 1:
report time difference
每次找到一个峰值时,两个峰值之间就有一个时差。不是每次运行循环时。测量值时要记录时间。如果您说的是“随机”结果,请提供数字示例。我尝试将阈值ptt设置为200,以尝试删除这些随机ptt值。但它仍在真正发挥作用。你有什么建议,我可以如何改变代码,以实现功能,u列在b)<代码>ptt=abs(实时1-实时2);
read sensor 1
if value 1 is a new max 1:
store timestamp 1
read sensor 2
if value 2 is a new max 2:
store timestamp 2
if max1 is a new peak and timestamp1 is > timestamp 2:
report time difference
if max2 is a new peak and timestamp2 is > timestamp 1:
report time difference