Electronics > Beginners

How to initialise INPUT_PULLUP before Interrupt runs in Arduino?

<< < (4/4)

Psi:
You could also add code to the interrupt handler to block any code running until millis() returns  >100  (100ms after starting).
But this would be a dirty dirty hack.
Best to do it the proper way.

If this project was not being done in Arduino i doubt you would have this issue.
The micro starts up with all interrupts disabled and you have to turn each of them on and also enable the global interrupt system as well. Which you normally only do after everything has been setup. I suspect Arduino is turning this on behind the scenes before any of your code runs and this is causing your problem.
It's either that, or as others have said you need a delay after setting the pull up and before you attach the interrupt to account for slow rise time from pullup.
It's ok to have hard delay functions as part of setting up hardware in setup().
It's only bad to have them inside loop()

If you add the delay and it doesn't help try this, it will block any interrupts from happening until after both lines run.
cli(); // global interrupt disable
<your code to enable pullup>
<your code to attach interrupt>
sei(); //global interrupt enable

KL27x:
If it works on a falling edge, it would probably not matter.* If it triggers on an input low, then of course you need some delay. The chip can execute a number of instructions during the rise time of the input pin. There's a discrete amount of time between turning on the pullup before the voltage rises above the logic set point, depending on the capacitance of the pin circuitry and the value of the internal pullup.

*if the interrupt were activated properly, with appropriate flags cleared. That might be hidden from you to some extent.

If speed really matters, what you can do to speed things up is make the input pin output high for a tick, then change it to input with internal pullup activated. Yes, that is ok even connected directly to a grounded button, IMO.

rstofer:
Maybe page 59 and 60 of the User Manual will provide a hint.  Among other things, there is no pull-up until the input port is written high. I suspect this will be the user's responsibility.  See the schematic where PUD must be 0, the data direction must be 0 and the port value must be 1.  Top 3 input NAND gate with 2 inverted inputs.

http://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf

The PUD bit in MCUCR must be zero.  If not, it disables ALL pull-ups. Zero is the default value according to 11.2.2 so everything should be fine unless something messed with it.  Maybe there is a way to read the register and verify that the library code hasn't changed it.

On the Arduino Mega 2560, there is (or was) a problem using interrupt on change with pins 20 and 21.  I scanned it, I didn't read it.

https://forum.arduino.cc/index.php?topic=158434.0

It might be worth changing to a different pin, just for giggles.



janoc:
If the entire point of this exercise is button debouncing, then just use one of the many Arduino libraries for that - e.g. Bounce2:
https://github.com/thomasfredericks/Bounce2/

But I do recommend reading the Jack Ganssle's series on this topic in order to understand the subject (it is pretty essential when it comes to handling user input with digital logic) and to possibly implement a better debouncer than what is in the library above.

Navigation

[0] Message Index

[*] Previous page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod