I want to make an ISR with a reed switch. So it should trigger, when the reed switch button pin is rising. It is for angular velocity, so in my original code, the reed switch would be triggered about every second, when the magnet passes the reed switch. The problem is, that the ISR usually triggers 4-5 times not just once.
This is just my code, where I am just playing around with the reed switch. When I pass the reed switch I usually get 1,2,3,4 or even more. So I tried this:
In that case I would get 1, but without triggering the reed switch again, the number 2 would show up in the serial monitor after the two seconds. I also tried to put the detachInterrupt into the beginning of the Interrupt routine, so that is should be impossible to run the interrupt again. but it will show the same results as before.
because I also read torque from a crank and do a lot of calculations in the normal code, and since I need the exact time of when the magnet passes the reed switch to calculate the angular velocity I think an interrupt is the best way. In the original code, the reed switch saves the time and triggers a flag.
Edit
if (millis()-t1 > 2000){
try
if (millis()-t1 > 100){
I don't get why this would help, but I changed now 2000 to 100 and now it usually shows me 1, 100ms later 2 and 100 milliseconds later 3. Without triggering the reed switch again.
I also tried to put an if statement into the ISR, so that the flag just gets triggered when 100ms are over. But I don't think that this is a good solution for the problem
I changed it now to what you recommended with the pullup. Meaning the reed swich is bettween PIN 24 and ground.. and also I changed the code to PULLUP. but still, same results
Nope, it is not parallel. I just found some things to clearing the interrupt register, but could not find out how this exactly works. Does anybody know that? If I would detach the interrupt directly in the beginning of the ISR and also clear the register it would not store the data and fire again later.
Debounce in an interrupt context is usually done with a lock out period in the isr. Do not take action on the interrupt if it occurs close in time to the last interrupt. Yes, the unwanted interrupts will trigger the isr, but they will be ignored and not change the count or flag setting.
.
void reed_trigger()
{
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 200) //pick some appropriate debounce lock out perioid
{
angvelFlag = 1;
p++;
}
last_interrupt_time = interrupt_time;
}