help with counting pulses with hall effect sensor

Hey I am making a project where i am counting the nr of strokes of a piston in a cylinder from an airsoft gearbox. (im tired of dry firing my airsoft gun)

the code uses attachinterupt()

the output pin from the hall effect sensor u18524, is connected to pin 2 on my arduino mega.

The magnet is attached to the piston inside the cylinder. It effect the sensor every time it performs a stroke

the code counts when the signal from the hall effect sensor is RISING in order to count only one up.

But the arduino keeps counting many hundreds of nr up for every stroke! (it should count only 1 up according to my design idea)

The code goes at follows:

volatile int IRQcount; int pin = 2; int pin_irq = 0; //IRQ that matches to pin 2

void setup() { // Put your setup code here, to run once: Serial.begin (9600); }

void IRQcounter() { IRQcount++; }

void loop() { // Put your main code here, to run repeatedly:

attachInterrupt(pin_irq, IRQcounter, RISING); delay(1); detachInterrupt(pin); Serial.print(F("Counted = ")); Serial.println(IRQcount);

}

Does anybody know how to adjust the code so it only count 1 for each stroke????

Do not cross-post. Other thread removed.

Does it only increment when the strokes are happening, or all the time?

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How fast, what frequency or interval are the pump strokes. Your counter is not counting strokes, it is counting loop cycles when ever the hall effect is ON.

You may be able to leave interrupts out of the code and look for change of state of the hall effect, each change = 1 count.

Tom... :)

That's not the way to use interrupts. Something like this is the way to go:

volatile int IRQcount;
int pin = 2;
int pin_irq = 0; //IRQ that matches to pin 2

void IRQcounter() 
{
  IRQcount++;
}  


int last_count ;
unsigned long last_sample ;

void setup() {
  // Put your setup code here, to run once:
  Serial.begin (9600);
  attachInterrupt(pin_irq, IRQcounter, RISING); 
  last_count = 0 ;
  last_sample = millis () ;
}

int read_count ()  // safely read count
{
  noInterrupts () ;
  int result = IRQcount ;
  interrupts () ;
  return result ;
}
  
void loop() 
{
  int count = read_count () ;  // read the current count without ever losing counts
  sample = micros () ;   // record time too if want to calculate rates

  int new_counts = count - last_count ;   // get difference from last time
  unsigned long time = sample - last_sample ;

  last_count = count ;   // update for next time round
  last_sample = sample ;

  Serial.print (F("Counted = "));
  Serial.print (new_counts);
  Serial.print (F(" in "));
  Serial.print (time);
  Serial.println (F("us"));
  delay(1);

So no detachInterrupt - that's usually wrong, as next time its attached you can get a deferred interrupt from the past. Let the ISR do its thing, take care to read from it with interrupts off to avoid garbling (unless only a single byte being written by the ISR, single byte writes are atomic on an 8-bit micro).

Rather than trying to reset the counter, you can always compare to last value you sampled - this is how we use micros() and millis().