Error with Interrupt

Hello everyone I was trying to use interrupt!
and ISR should run when FALLING
but it runs when CHANGE

I'll attach my code with just interrupt part
if there's something wrong please let me know!

My arduino board is Arduino101 so digitalPin number 8 could be used as interrupt pin
and pin8 connected to hall sensor module, not hall sensor
this module could read sensor value as digital and analog and count should increase when state
is changed from HIGH to LOW

this is part of my code

volatile int count = 0;
const byte Hall_D = 8;
void setup() {
** attachInterrupt(Hall_D, Calculation, FALLING);**
** Serial.begin(9600);**
}
void loop() {
** Serial.println(count);**
}
void Calculation(){
** count += 1;**
}

Try:

attachInterrupt(digitalPinToInterrupt (Hall_D), Calculation, FALLING)

The Arduino 101 environment is somewhat old an I am not familiar with it. What fireAngel is certainly the recommended way now. Surprisingly, you appear to have got some results as you had it coded.

If using digitalPinToInterrupt() does not help, then I would suspect is that the Hall sensor is delivering a “jittery” output which triggers the interrupt service routine more often than you would like.

Try (a) changing “FALLING” to “CHANGE” and see if the count increases twice as fast as it would have with “FALLING” and (b) adding code in your ISR to disqualify calls to it within say 30 mS of the last successful call, to perform a sort of “debouncing”.

fireAngel:
Try:

attachInterrupt(digitalPinToInterrupt (Hall_D), Calculation, FALLING)

I already did that, but same result

6v6gt:
The Arduino 101 environment is somewhat old an I am not familiar with it. What fireAngel is certainly the recommended way now. Surprisingly, you appear to have got some results as you had it coded.

If using digitalPinToInterrupt() does not help, then I would suspect is that the Hall sensor is delivering a "jittery" output which triggers the interrupt service routine more often than you would like.

Try (a) changing "FALLING" to "CHANGE" and see if the count increases twice as fast as it would have with "FALLING" and (b) adding code in your ISR to disqualify calls to it within say 30 mS of the last successful call, to perform a sort of "debouncing".

this makes me more curious..
FALLING and CHANGE 's results are same
this is really weird..lol

I dont know why but it worked as I wanted when change to RISING

volatile int count = 0;
const byte Hall_D = 8;

void setup() {
  attachInterrupt(Hall_D, Calculation, FALLING);
  Serial.begin(9600);

}

I don't see you setting the pinMode for the sensor's pin
cannot tell if you are using a pull-up/pull-down resistor

Since count is an integer (2 bytes), this code will go wrong ocasionally, as the ISR can run between fetching
each byte of the variable:

void loop() {
  Serial.println(count);

}

So if the ISR updates count from 255 (0x00FF) to 256 (0x0100) during that time the loop() code
sees 511 (0x01FF) or 0 - depending on what order it reads the two bytes.

You can protect against this by reading or writing multi-byte volatile variables in a "critical section":

int read_count ()
{
  noInterrupts() ;
  int result = count ;
  interrupts() ;
  return result ;
}

which prevent the ISR running at the wrong moment. A critical section is a piece of code that is
prevented from overlapping with other ciritical sections (the body of an ISR is automatically a critical section
as interrupts are disabled during an ISR).

MarkT:
Since count is an integer (2 bytes), this code will go wrong ocasionally, as the ISR can run between fetching
each byte of the variable:

void loop() {

Serial.println(count);

}



So if the ISR updates count from 255 (0x00FF) to 256 (0x0100) during that time the loop() code
sees 511 (0x01FF) or 0 - depending on what order it reads the two bytes.

You can protect against this by reading or writing multi-byte volatile variables in a "critical section":



int read_count ()
{
  noInterrupts() ;
  int result = count ;
  interrupts() ;
  return result ;
}



which prevent the ISR running at the wrong moment. A critical section is a piece of code that is
prevented from overlapping with other ciritical sections (the body of an ISR is automatically a critical section
as interrupts are disabled during an ISR).

ah I changed integer to float type and start from 0.000001
and + 0.000001 when function is called and use returned value by multiply 100000

BulldogLowell:

volatile int count = 0;

const byte Hall_D = 8;

void setup() {
 attachInterrupt(Hall_D, Calculation, FALLING);
 Serial.begin(9600);

}




I don't see you setting the pinMode for the sensor's pin
cannot tell if you are using a pull-up/pull-down resistor

ah I forgot to write pinMode here! sry
but I wrote pinMode in my real code and still dont know why RISING works for me lol

kosh09:
ah I changed integer to float type and start from 0.000001
and + 0.000001 when function is called and use returned value by multiply 100000

Is this safe?

How high are you going to let the values get? floating point is not totally accurate - I think you get like 6 significant figures, and beyond that.

If you're incrementing it, eventually the total will get large enough that stuff behaves in a way you probably aren't expecting.

DrAzzy:
Is this safe?

How high are you going to let the values get? floating point is not totally accurate - I think you get like 6 significant figures, and beyond that.

If you're incrementing it, eventually the total will get large enough that stuff behaves in a way you probably aren't expecting.

hmm... I dont know how much data I will get
because I want to use this like function millis() get the current count and then next count
current - prev will be the distance of my project moveed
that will be not safe?

Then if you're only storing whole numbers, don't use a float. Use a float only when you need decimals.

Using a float actually makes the problem MarkT cited much worse, because floats take more instructions to interact with. Do what he suggested instead.

DrAzzy:
Then if you're only storing whole numbers, don't use a float. Use a float only when you need decimals.

Using a float actually makes the problem MarkT cited much worse, because floats take more instructions to interact with. Do what he suggested instead.

Finally I decided to use integer type
and when count >= 254, makes count to 0.
then there will be no problem with this right?

DrAzzy:
Then if you're only storing whole numbers, don't use a float. Use a float only when you need decimals.

Using a float actually makes the problem MarkT cited much worse, because floats take more instructions to interact with. Do what he suggested instead.

Don't even use float if all you need is a decimal. Use float when you have quantities that differ by more than 9 digits of precision and you need to be able to do math between them and don't really care about absolute accuracy.