generating interrupt for running a specific code

hi there.....
i am now trying to learn arduino mega 2560 programming....
i want to generate an interrupt on detecting 5 volts and run any specific code.. what will be the code and mechanism to detect 5v in Arduino mega 2560??

Go to the tutorial section and read the examples for analogRead() and attachInterrupt()

they probably contain all the code you need (and much more)

If you are only interested in 5volts or 0volts you don't need to use analogRead(). digitalRead() will do that and the code will be simpler.

Unless you need to detect a very short-lived pulse you probably don't need the complexity of interrupts. Look at the concept in the "blink without delay" example sketch.

...R

Actually ATmega has the perfect mechanism for this. Below is a section from the manual:

  1. AC – Analog Comparator
    The Analog Comparator compares the input values on the positive pin AIN0 and negative pin
    AIN1. When the voltage on the positive pin AIN0 is higher than the voltage on the negative pin
    AIN1, the Analog Comparator output, ACO, is set. The comparator’s output can be set to trigger
    the Timer/Counter1 Input Capture function. In addition, the comparator can trigger a separate
    interrupt, exclusive to the Analog Comparator. The user can select Interrupt triggering on com-
    parator output rise, fall or toggle.

You can find out more on page 271 in the ATmega2560 manual.

Cheers!

Does Arduino/Wiring support this? No but Cosa does. Check github or my posting on the forum.

@kowalski, I strongly suspect that your approach is overkill and a simple loop with digitalRead() will be sufficient and a lot less complex.

...R

@Robin2

Unfortunately this question is poorly specified. We (at least I) simply do not know the full spec/use case/etc. Using digitalRead() does not say that it is 5V. It could be as low as 0.6VCC (3V typical). Below is the original question.

i want to generate an interrupt on detecting 5 volts and run any specific code

The function digitalRead does not generate an interrupt. All this depend on how we interpret the question; logical high signal, analog voltage theshold, button, etc. And an interrupt is intended.

But I fully agree Analog Comparator is the more complex and thus less likely (Occam). The interpretation of the question is then "how do it detect a logical high signal and execute a specific part of code". And the answer is:

if (digitalRead(PIN)) { 
   // the specific code block to execute when the PIN is logical high/on/active/etc
}

Cheers!

[Update] After reading other forum posts by kazmi I am convinced that @Robin2 your interpretation is spot on. The usecase seems to be a trigger to capture an image and send to a mobile phone. But this is with some hand-waving.

thanks for reply...
but i want the arduino mega 2560 in idle mode till it detects a 5v....but by the code you recommended

Code:
if (digitalRead(PIN)) {
// the specific code block to execute when the PIN is logical high/on/active/etc
}

arduino mega 2560 will be constantly busy all the time reading that digital pin.....
so is there any suggestion?
basically i am doing this for the security system of the car.. :slight_smile:

You can use the sleep mode (see the relevant Atmega datasheet for details) and have the MCU "woken" up by an external signal.

However if you don't use the sleep mode the MCU will be running continuously and might as well be executing digitalRead() as anything else.

...R

can you give me some more detail as i want to trigger GSM SIM900D module when ever there will be an interrupt!!

Let's assume for now that you don't really need an "interrupt" then Kowalski has already given you the code

if (digitalRead(PIN)) {
   // trigger GSM SIM900D module
}

As to what "trigger" means that's anyone's guess, but also another question.


Rob

... and if you still think you need an interrupt please explain your thinking.

...R

My objectives are:

  1. Arduino mega 2560 detects a voltage level change (by interrupt).
  2. After detecting (interrupt), i want GSM SIM900D to work for me and send SMS to user....

can you help me with the coding of this!!

Why an interrupt?
Why not polling?
Does the odd few microseconds really make that much difference?

If you are only interested in 5volts or 0volts you don't need to use analogRead(). digitalRead() will do that and the code will be simpler.

@robin2 - This wont work for the OP as anything above 3 (3.3)? volts is high on the At chips.

@op - you don't want the cpu running around doing nothing - BUT theres no os and no "other process" to run so what else can it do!. You can't do anything complex in an ISR. You can't (safely) you can't use I2C SPI or anything else that requires interrupts. And of course you can't use delay(). O and millis does not work correctly inside ISRs as the count is not incremented.

Use polling!.

Read this thread External Interrupt - need to stop execution or jump to another - Project Guidance - Arduino Forum

Mark

holmes4:
@robin2 - This wont work for the OP as anything above 3 (3.3)? volts is high on the At chips.

I don't think we know enough about the OP' requirement to know whether he needs to distinguish between 3 and 5 volts. My guess is he wants to distinguish between 0 and something else which isn't higher than 5v. And I am reluctant to suggest "complex" solutions before they are know to be essential.

But polling does seem to be the sensible staring point.

It would be nice if the OP could read and respond more quickly.

...R