Page 1 of 1

Button press interrupt

Posted: Mon Oct 19, 2020 5:23 pm
by billduck
The attached image shows a 5 button shield plugged into an UNO. When I press a button, the software shows the millivolt range that is tested for, and the actual millivolts read. The 5 buttons on the LCD form a voltage divider with an input to AN0.
In one version of the program, I enable the interrupt and disable the sub call. Compile and run. It only works on 3 right buttons. But if I disable the interrupt and do a sub call in main, compile and run, It works for all 5 buttons. I note that the 2 buttons

Re: Button press interrupt

Posted: Mon Oct 19, 2020 5:48 pm
by mnf
You shouldn't do the display and delays in the interrupt handler....

Interrupt handler needs to check which button is pressed - set a global with this value and then return.

Calling it as a macro - timing is much less critical.

Martin

Re: Button press interrupt

Posted: Tue Oct 20, 2020 2:16 pm
by billduck
I implemented the code to where the interrupt handler sets a global value. I have the same problem, in that 2 of the buttons do not set the MenuPosition (global value) and display text indicating so.
I note that the 2 buttons that do not respond have the highest analog voltage - 2.2 to 3.2 and 3.2 to 4.2.
I do not fully understand the properties / settings for the pushbutton. Conversion speed and aquisition cycles.

Re: Button press interrupt

Posted: Tue Oct 20, 2020 4:27 pm
by medelec35
You need to change your flowchart to the way martin has stated.
You have still got LCD components within the interrupt handler.
You must never add components that contain delays like LCD or delays themselves to interrupts.
Only use interrupts for inputs/outputs and setting flags (bool variable set to 1 then the variable is read outside the interrupt.
Interrupts should be kept as small as possible, and allowed to exit normally i,e no call macros.

Re: Button press interrupt

Posted: Tue Oct 20, 2020 4:43 pm
by LeighM
Also, the interrupt responds to a digital level change, so some of the voltage transitions might not trigger an interrupt

Re: Button press interrupt

Posted: Tue Dec 01, 2020 1:52 am
by billduck
The solution that I chose was to just check if a button press is sensed in Main. Since it detects any 1 of 5 buttons. Forget the interrupt.
I also found out that on the MEGA, that Analog Input 0 is on Port F and IOC PortF (interrupt on change - Port F) is not available.
Thanks for the suggestions.