霍尔效应传感器测速仪

时间:2019-11-16 18:52:05

标签: arduino

我正在尝试为我们团队的卡丁车创建一个速度计。我正在使用Arduino和霍尔效应传感器,但遇到了问题。传感器将检测到磁铁,但不会在串行监视器上打印任何内容。我正在尝试使用检测代码来打印在网上(https://maker.pro/arduino/tutorial/how-to-use-a-hall-effect-sensor-with-arduino)找到的“检测”。

代码如下:

 volatile byte half_revolutions;
 unsigned int rpm;
 unsigned long timeold;
 void setup()
 {
   Serial.begin(115200);
   attachInterrupt(0, magnet_detect, RISING);//Initialize the intterrupt pin (Arduino digital pin 2)
   half_revolutions = 0;
   rpm = 0;
   timeold = 0;
 }
 void loop()//Measure RPM
 {
   if (half_revolutions >= 20) { 
     rpm = 30*1000/(millis() - timeold)*half_revolutions;
     timeold = millis();
     half_revolutions = 0;
     //Serial.println(rpm,DEC);
   }
 }
 void magnet_detect()//This function is called whenever a magnet/interrupt is detected by the arduino
 {
   half_revolutions++;
   Serial.println("detect");
 }

非常感谢您的帮助!

0 个答案:

没有答案