首页 > 解决方案 > 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),但它不工作。我还尝试使用提供的微调器降低霍尔传感器的灵敏度。

我确定问题不在于传感器,而在于中断,我猜。我哪里错了?很感谢任何形式的帮助!

谢谢你。

标签: arduinoiotarduino-unoandroid-sensorsarduino-ide

解决方案


推荐阅读