Mys project involves reading the speed of a wheel using a Hall sensor, calculating the speed of the wheel, and moving a gauge needle using a servo to indicate the speed. The wheel has 5 button magnets to trigger the Hall sensor.
**The code snippets below are reduced to bare minimum to illustrate the problem I am having. The problem being that the first bit of code works perfectly, but when I add the servo code in the second bit of code, the program still works while the wheel is moving, but if the wheel stops with a magnet and Hall sensor aligned, the interrupt service routine is continuously triggered. Move the wheel slightly off the sensor and the ISR triggering stops.
Any ideas why this is happening?
**
**// Hall_test_no_servo** **This code works correctly**
int n_rev;
int magnets_per_rotation=5;
int n_magnets = 5;
void setup()
{
Serial.begin(115200);
attachInterrupt(0, detect, RISING);//Initialize the intterrupt pin (Arduino digital pin 2)
n_rev = 0;
}
void loop()//Measure RPM
{
Serial.print("n_rev=");
Serial.println(n_rev);
} // end void loop
//This function is called whenever a magnet/interrupt is detected by the arduino
void detect() { n_rev++; }
**// Hall_test_servo** **This code does not work correctly**
#uncategorized include <Servo.h>
int n_rev;
int magnets_per_rotation=5;
int n_magnets = 5;
int servo_pin=4;
int low_pwm=450; // use low and high to set range
int high_pwm=2400; // of motion of speedo servo
Servo myservo; // create servo object to control a servo
void setup()
{
Serial.begin(115200);
attachInterrupt(0, detect, RISING);//Initialize the intterrupt pin (Arduino digital pin 2)
n_rev = 0;
// adding this line of code causes the problem to occur whenever a magnet stops directly in line with the Hall sensor
myservo.attach(servo_pin,low_pwm,high_pwm);
}
void loop()//Measure RPM
{
Serial.print("n_rev=");
Serial.println(n_rev);
} // end void loop
//This function is called whenever a magnet/interrupt is detected by the arduino
void detect() { n_rev++; }