and this is the wiring (without the LED cuz i'm using the onboard LED on PIN 13)
The problem is that when I press many times the button, sometimes LED turn ON, some times OFF.
supposed that when I press the button, LED will turn ON and when I release it turns OFF
seems like when I'm pressing the button too fast, the IC going crazy and inverts the button state
and ends up working on reverse.
normaly:
LED is OFF and by pressing the button -> LED ON
but what I got is
LED always ON and by Pressing the button -> LED OFF
I have also try to run the code without INPUT_PULLUP and by using a 10K R but nothing changes...
Your sketch is slightly modified and presented below. In fact, there is no HIGH option for the mode of the triggering signal. Use FALLING; give a meaning name to the ISR(). You may be required to take care of the bouncing of the button (the interrupting device) connected at DPin-2 to prevent from occurring of multiple interrupts.
The problem is caused by contact bounce in your button. You would need to add some debouncing code to fix the issue. Luckily, there is a tremendous amount of great information available on contact bounce and debouncing.
A button is a poor source of an interrupt trigger. It makes sense to use one simply to learn how attachInterrupt() works, but in a real project, you would not likely use an interrupt to capture button presses. Instead, you would simply poll the button's pin in loop().
@TomGeorge you are right!
But I use the internal (onboard) LED, the diagram in the picture is just for reference but you are right on the diagram I need a resistance.
@pert I have done a research about button denouncing and seems like this is my issue!! Never heard about this before! Thank you for this!
But I have a question here.
I want to make a door opening indicator for my freezer by using a magnetic sensor (from my old window alarm), using also the lowpower mode for saving battery power. Do you think attachInterapt() is a good way to solve this?
My idea is to sound a little buzzer when door left open for more than 6 sec
Gask:
I want to make a door opening indicator for my freezer by using a magnetic sensor (from my old window alarm), using also the lowpower mode for saving battery power. Do you think attachInterapt() is a good way to solve this?
Depending on which interrupt you attach where - there are lots of interrupts available.
If you want to use sleep modes, you indeed need an interrupt to wake up. So that's a reason to use such an interrupt.
For minimum power use get a Pro Mini; remove the regulator & on-board LED. The 8 MHz 3.3V version runs fine on 2xAA or 1x LiPo.
Upon startup set up the interrupts, go to sleep.
When the interrupt is triggered (door opened), set a timer interrupt and go to sleep (select appropriate sleep mode for timers to continue running, or use the WDT).
When you wake up from the timer interrupt check whether the door is open, and if you used the WDT whether it's been 6 seconds.
Check whether the door is still open; set new timer or run the buzzer or go to sleep as appropriate.