Multiple ISR calls with comparator interrupts

Hey!

I am doing a project where I need to measure speed of an RC car and I am planning on doing so by measuring the time it takes a wheel to complete one revolution. (I made a thread about how to accomplish this in the project guidance section and you guys recommended a hall effect sensor so that’s the route I am going with).

My problem now is that I get multiple interrupt calls when I move a magnet by the sensor. But I don’t understand why since the analog readout from the hall sensor and the potentiometer which I use to generate the compare voltage seem to be very clean:
screenshot of the serial plotter as produced by this code:

void setup() {
  Serial.begin(9600);
}

void loop() {
  int hallSensorReading = analogRead(A0);
  int voltageDividerReading = analogRead(A1);
  Serial.print(hallSensorReading);
  Serial.print("\t");
  Serial.println(voltageDividerReading);
}

But when I run this code:

volatile boolean triggered = false;

ISR(ANALOG_COMP_vect) {
  triggered = true;
}

void setup() {
  Serial.begin(9600);

  ACSR = 
  (0<<ACD) |   // Analog Comparator: Enabled
  (0<<ACBG) |   // Analog Comparator Bandgap Select: AIN0 is applied to the positive input
  (0<<ACO) |   // Analog Comparator Output: Off
  (1<<ACI) |   // Analog Comparator Interrupt Flag: Clear Pending Interrupt
  (1<<ACIE) |   // Analog Comparator Interrupt: Enabled
  (0<<ACIC) |   // Analog Comparator Input Capture: Disabled
  (1<<ACIS1) | (1<ACIS0);   // Analog Comparator Interrupt Mode: Comparator Interrupt on Rising Output Edge
}

void loop() {
  if(triggered) {
    triggered = false;
    Serial.println("Triggered");
  }
}

I get multiple interrupts when I move the magnet across the sensor. Could you please help me out?

Best regards and many thanks in advance!

The hall sensor is a digital device no? It just goes HIGH and LOW right? So why use the comparator interrupt? Just use one of the hardware pin interrupts with attachInterrupt. That's how Hall sensor stuff is normally done. Or am I missing something?

Delta_G: The hall sensor is a digital device no? It just goes HIGH and LOW right? So why use the comparator interrupt? Just use one of the hardware pin interrupts with attachInterrupt. That's how Hall sensor stuff is normally done. Or am I missing something?

There are two types of hall effect sensors. The first one works as you described but the second measures the magnetic field around it and adjusts its output accordingly. If you look at my screenshot you can see the curve.

Ok. For RPM type of thing though I’d be more likely to go for the digital type of Hall sensor. It would certainly make things less complicated.

Delta_G: Ok. For RPM type of thing though I'd be more likely to go for the digital type of Hall sensor. It would certainly make things less complicated.

I absolutely agree with you. But that's the type I've got and I see no reason why my approach with the comparator interrupt shouldn't work. Have you got any idea what I am doing wrong?

My best guess would be something akin to contact bounce. The other code is probably too slow to see it. But the interrupt catches it.

Power supply?

Delta_G:
My best guess would be something akin to contact bounce. The other code is probably too slow to see it. But the interrupt catches it.

You are probably right. I did some software debouncing and now everything seems to work fine. Is this an okay way to do this?

volatile boolean triggered = false;
volatile boolean debounced = true;

ISR(ANALOG_COMP_vect) {
  if(debounced) {
    triggered = true;
    debounced = false;
  }
}

void setup() {
  Serial.begin(9600);
  while(!Serial);

  ACSR = 
  (0<<ACD) |   // Analog Comparator: Enabled
  (0<<ACBG) |   // Analog Comparator Bandgap Select: AIN0 is applied to the positive input
  (0<<ACO) |   // Analog Comparator Output: Off
  (1<<ACI) |   // Analog Comparator Interrupt Flag: Clear Pending Interrupt
  (1<<ACIE) |   // Analog Comparator Interrupt: Enabled
  (0<<ACIC) |   // Analog Comparator Input Capture: Disabled
  (1<<ACIS1) | (1<ACIS0);   // Analog Comparator Interrupt Mode: Comparator Interrupt on Rising Output Edge

  Serial.println("Started...");
}

void loop() {
  if(triggered) {
    triggered = false;
    Serial.println("Triggered");
  }

  if(!debounced) {
    int hallRead = analogRead(A0);
    if(hallRead < 510)
      debounced = true;
  }
}

[quote author=Coding Badly date=1506815596 link=msg=3431803]
Power supply?[/quote]

I just hooked the sensor to the 5v output of the arduino. Do you think the sensor causes a voltage dip? Could a capacitor help?

MarkGoingToSpace:
I just hooked the sensor to the 5v output of the arduino.

How is the Arduino powered?