Using INT pin as both INT and Digital input simulateously

Board: Arduino UNO
Context: I'm using Arduino UNO borad with 20*4 LCD and 6 push-buttons. I'm currently trying to design a mechanical device and using Arduino UNO as controller to see if mechanical system is operating as expected or not.
Problem: I'm using 2 pushbuttons for multiple purposes. One of those is used as ENTER/MODE-SELECT/START depending upon what page currently screen is ON and another for BACK/CLEAR/STOP/PAUSE. I need to PAUSE/STOP the program for which I'm using ISR on pin D2 pin (Other INT pin D3 is occupied by rotary encoder of motor).
I need to pin D2 to work as Digital Read while selecting and browsing through menus/modes and also need it to work as interrupt when used as STOP/PAUSE (while machine is working).
Is it possible to do?

Please provide a schematic. That will make it much easier to understand your question.

Why the need to use the same pin for 2 different purposes ? Have you run out of pins and are you sure that you need to use an interrupts at all ?

Your post was MOVED to its current location as it is more suitable.

I have plenty of pins available. But I need to use as less no. of pushuttons as possible. That's the reason to use 2 pushbuttons for multiple purposes.

It seems that you have a number of spare pins so why the need to have a pin do two different things ?

These controls will be used on a control panel that I'm currently using and I have only 2 spots left on there for the switches

That's a hardware problem :grinning:

And the reason for using an interrupt ?

I suggest there is a far better way to approach what you are trying to do, you should never need to stop or pause your program. Here are some tutorials to get you started.

Blink without delay, which is in the Arduino IDE.

I think you're gonna need a bigger boat


Why not switch to a mega pro for $11

This is one reason

Reason for using Interrupt:
on pressing START button loop() will call another declared function RunAuto() / RunSemiAuto() / RunManual(). It will stay in the loop until function is completed. I need an interrupt on stop switch to get out of that loop and move to another page of LCD in display "PAUSED". I can't scan for pushbuttons as above function will stay in while loop.

About Hardware Problem: I'm avoiding to strip off entire control panel just to add extra switch. And if it can be avoided with cleaver piece of code that'll be great!

Well, you could of course scan for button presses anywhere in the code, or you could avoid the use of blocking functions to avoid the need to do so in multiple places

Suppose that you do use an interrupt. What is it going to do when the ISR runs ?

INTzeroY
Figure-1:

1. Before using pinBut as a digital input device, just execute the following codes:

detachInterrupt();  //interrupt pin goes back to digital IO mode
pinMode(2, INPUT_PULLUP);
-----do waht ever you have-------------
attachInterrupt(digitalPinToInterrupt(2), ISRINTZER0, FALLING); //Dpin-2 is back to interrupt
1 Like

I'm planning to include a function Pause() which will execute on INT from PAUSE pin.
Pause()
{
turn motor OFF
update screen
wait until button is pressed
turn motor ON
update screen
}
By using ISR,I don't need to include push-button scanner in every loop of RunAuto() function. There are many loops in these functions. Also bigger problem is I need to keep track of every single variable depending upon in which loop the PAUSE button is pressed.

I'll definitely try this! Thanks!

Look into state machines. Sounds like you need to implement one. Also, you might benefit from understand the basics of non-blocking code and object oriented programming
What you want can been done, without interrupts, just by having your code scan buttons pretty much all the time. But it probably requires some fundamental changes into how you think about code.

2 Likes

Thanks for the suggestion! I'll defiitely check that.

2 Likes

Yes. You can use Pin 2 to cause an interrupt and you can also use digitalRead() to read the state of the pin. Have a global variable you can set so the ISR can tell if you want to ignore the interrupt or not.