arduino - Arduino 中断被多次触发 - 使用霍尔传感器进行 RPM
问题描述
我正在使用霍尔传感器来计算车轮的 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)
,但它不工作。我还尝试使用提供的微调器降低霍尔传感器的灵敏度。
我确定问题不在于传感器,而在于中断,我猜。我哪里错了?很感谢任何形式的帮助!
谢谢你。
解决方案
推荐阅读
- java - 在 IntelliJ IDEA 中运行单个 Junit 测试
- python - 在 Python 中解析 XML 文件:检查标签或项目时没有结果
- sql - 运行联合查询时,')' 附近的语法不正确
- css - 如何移动(中心)一个 svg 图标?
- r - 在 R 中绘制均值/平均 ROC 曲线的简单方法
- c# - 整数不应该减少吗?
- reactjs - 使用打字稿,我如何使用reduxForm,connect和withRouter一起使用
- regex - 如何排除部分正则表达式匹配?
- c# - 为什么我在 TextChanged 事件中运行相同的代码而不是 Button Click 时会得到不同的结果?
- reactjs - 无法在 Storybook 中按类名设置我的 React 组件样式