Hello all,
I am working on making a speedometer that is mounted on a snowmobile. The diameter is based on the brake rotor. What I wanted the code to do is show the speed (mph) of the rotor in the serial monitor. What actually happens is that the code spits out some weird numbers. I have attached a photo of the output. Someone said that I should take an average of 10 measurements and then serial print it, but I would think the speed would still be off because of the vast differences in the numbers. I connected the hall effect sensor’s ground to the ground on the Arduino. The sensor is plugged into the 5v port on the Arduino. Then the input wire has a 10k ohm resistor in the spot of the load. The black wire is connected to digital pin 2. I have attached the wiring diagram of the Hall effect sensor.
Have a good day and thanks for reading.
#define PI 3.14159
float diameter = 0.00009121; //miles
float mph;
volatile byte half_revolutions;
float rpm;
unsigned long timenew;
unsigned long timeold;
//unsigned int magnet_detect;
boolean flag=false;
void setup()
{
Serial.begin(500000);
attachInterrupt(0, magnet_detect, RISING);//Initialize the intterrupt pin (Arduino digital pin 2)
half_revolutions = 0;
rpm = 0;
timeold = 0;
Serial.println( "I am Speeed");
//Serial.println( "I am Speeed 2");
Serial.println(half_revolutions);
}
void loop()//Measure RPM
{
if (flag) {
timeold=timenew;
timenew=micros();
rpm=60000000.0/(timenew-timeold);
mph = rpm * (diameter * PI) * 60;
Serial.println(mph);
flag=false;
}
//delay(10);
}
void magnet_detect() //This function is called whenever a magnet/interrupt is detected by the arduino
{
half_revolutions++;
flag=true;
Serial.print("detected. Speed is: ");
Serial.println(mph);
}