问题描述
我正在使用霍尔传感器来计算车轮的 RPM。我正在使用以下传感器:
我的代码如下:
int hall_pin = 3; // digital pin
float hall_threshold = 5.0;
float count = 0;
void setup() {
pinMode(hall_pin,INPUT);// put your setup code here,to run once:
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(hall_pin),hall_ISR,RISING);
}
void loop() {
count = 0.0;
float start = micros();
while(1){
if(count >= hall_threshold){
break;
}
}
float end_time = micros();
float time_passed = (end_time - start)/1000000.0; // in seconds
float rpm = (count/time_passed)*60.0;
Serial.println(rpm);
delay(10000);
}
void hall_ISR()
{
count+=1.0;
}
我正在使用数字引脚 3 从传感器读取数据并计算它使用中断检测到磁场的次数。由于传感器在检测时输出 1,因此我使用了 RISING
中断。一旦 count
大于 5,则控制退出无限循环并计算 RPM。问题是这个中断被多次触发。我曾尝试使用 detachInterrupt(hall_pin)
但它不起作用。我还尝试使用提供的微调器降低霍尔传感器的灵敏度。
我确定问题不在于传感器,而在于中断,我猜。我哪里错了? 非常感谢任何帮助!
谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)