Using INT pin as both INT and Digital input simulateously

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 ?


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.
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.


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


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.

That really isn't something that you should do in an ISR, particularly as interrupts are disabled when in an ISR so I hope that your sketch does not depend on them being enabled whilst waiting for the button to be pressed

Depends on the type /size of the machine and it's environment but you may need to consider that if your "stop" is an emergency in any way, requirements are normally they be hard wired, not software stop.
Your requirement for interrupt suggests that it falls into this catagory.

1 Like

Yes. But you do not need any interrupt for that.

So you have absolutely no idea what an interrupt is then?

Here's my "canned" explanation:

That is not what interrupts are [b]for[/b]!

As a beginner, it is incredibly unlikely that interrupts will be useful to you.

A common "newbie" misunderstanding is that an interrupt is a mechanism for altering the flow of a program - to execute an alternate function. Nothing could be further from the truth! :astonished:

An interrupt is a mechanism for performing an action which can be executed in "no time at all" with an urgency that it must be performed immediately or else data - information - will be lost or some harm will occur. It then returns to the main task without disturbing that task in any way though the main task may well check at the appropriate point for a "flag" set by the interrupt.

Now these criteria are in a microprocessor time scale - microseconds. This must not be confused with a human time scale of tens or hundreds of milliseconds or indeed, a couple of seconds. A switch operation is in this latter category and even a mechanical operation perhaps several milliseconds; the period of a 6000 RPM shaft rotation is ten milliseconds. Sending messages to a video terminal is clearly in no way urgent,

Unless it is a very complex procedure, you would expect the loop() to cycle many times per millisecond. If it does not, there is most likely an error in code planning; while the delay() function is provided for testing purposes, its action goes strictly against effective programming methods. The loop() will be successively testing a number of contingencies as to whether each requires action, only one of which may be whether a particular timing criteria has expired. Unless an action must be executed in the order of mere microseconds, it will be handled in the loop().

So what sort of actions do require such immediate attention? Well, generally those which result from the computer hardware itself, such as high speed transfer of data in UARTs(, USARTs) or disk controllers.

An alternate use of interrupts, for context switching in RTOSs, is rarely relevant to this category of microprocessors as it is more efficient to write cooperative code as described above.