About attachInterrupt - help please

Hello everyone - My first post here…

I’m new to Arduino - but have some (very limited) experience with programming. Here’s my question:

I’m trying to create a controller for something similar to this:
Pin 7 (as an example) is Output and High - but is normally attached to a circuit that is open.
Pin 2 is Output and High, but normally attached to a circuit that is closed.

The objective is that when the circuit for Pin 7 goes closed (from an external switch) →
Then Pin 2 is caused to go Low.
In other words, when the sensor (independent) circuit closes, the actuator (dependent) circuit is opened.

I would have preferred to use an If loop for the sensor circuit, but understand that If requires a comparative value, which I don’t have.

I believe I’ll need to use the attachInterrupt command, but can’t understand the required syntax, nor which circuit will go on which pin (does the sensor go on pin 7, or the actuator?)

Thanks in advance!!! I appreciate any wisdom here!

PlumBob

You can use attachInterrupt() to define what interrupt pin to monitor (0 = pin2; 1 = pin3), what function to call when the interrupt occurs, and what pin state change (RISING, FALLING, or CHANGE) is to trigger the interrupt.

So, you really should consider switching the pins around.

On the other hand, you need to define what else the Arduino is doing. If it isn't doing much, or anything, else, the use of an interrupt is probably not necessary.

I'll share some of the code that's working for me. I have a device that produces short positive 5v pulses at irregular intervals conected to pin 2 (INT0), so I put this into my header file:

#define GMC  0                         /* Pin  2 - INT0 Geiger counter input  */

Then in my setup() function I added:

   attachInterrupt(GMC,geiger,RISING); /* Set up GMC interrupts               */

to associate the function geiger() with pin 2 and specify that a transition from low to high (RISING) should trigger that interrupt. The geiger() function will be given control whenever an interrupt is triggered, and all it does is increment a global variable that records the number of such events:

unsigned re;                           /* Radiation event counter             */
/*----------------------------------------------------------------------------*/
/* Interrupt routine called when event detected - use attachInterrupt()       */
/*----------------------------------------------------------------------------*/
void geiger(void)
{  ++re;                               /* Increment radiation event counter   */
}                                      /*  end: geiger()                      */

The Arduino reference lists the interrupt triggering options, so all you really need to do is decide how you want to build your circuit and which signal you'd like to use as a trigger, and then specify the corresponding triggering option.

PlumBob: I would have preferred to use an If loop for the sensor circuit, but understand that If requires a comparative value, which I don't have.

What do you mean? If you test the input you get HIGH or LOW. You compare to those.

Paul - thanks... If this was the only thing the Uno had to do, it would still fit my bill. I'll build other functions later, but this is the primary function. I appreciate your comments. Do I understand that you would put the sensor (intermittently on switch) on the #2 pin, and the controlled (switched) circuit on pin 7?

Morris, that's really helpful - thanks. You've also illuminated something else that was hanging around in the shadows of my head.

Nick, perhaps I didn't understand the material I read, but everything I've seen says that the "if" has to compare something to a "numerical" value. >50, =50, etc. I could reference the 5 volt signal, I suppose, but I just didn't see that as an option. I tested "if" last night, using High and Low, without success - but admittedly there may have been other issues. Have you used the "if" referencing only High or Low successfully? That would be helpful.

Nick, I'm also looking at the thread http://arduino.cc/forum/index.php/topic,91850.0.html about using a microswitch, and see that the original poster used "if" argument, apparently successfully. Any comments about how I could use that in this application would be appreciated.

PlumBob: Have you used the "if" referencing only High or Low successfully? That would be helpful.

Of course. It is hard to imagine a program that doesn't do that.

Something like this:

byte onButton = digitalRead (8);   // read D8 pin

if (onButton == HIGH) 
  {
  // button is high (5v)
  }
else
  {
  // button is low (0v)
  }

If your code didn't work/didn't compile, perhaps post what you had?

Do I understand that you would put the sensor (intermittently on switch) on the #2 pin, and the controlled (switched) circuit on pin 7?

Yes, because that pin can easily trigger an interrupt. Pin 7 can too, but it's a lot more difficult to set it up.

Thanks, Nick and Paul ----

Nick, it came to me in the shower last night that I did need a “==” in the “if” statement - which I had not used. I’ll try that again.

Paul, thanks. Yes, that does make sense.

I’m going to try both ways - I’ll let you all know.

PB

I'm going to use the attachInterrupt routine. It works perfectly.

I believe that the "if" would not work in this case because it is evaluating the state of the pin, as assigned by the program. In my project the pin is set to HIGH, in order to be able to supply voltage. But the normal actual state of the circuit attached to that pin will be an open circuit - no voltage flowing. That is the thing that the program has to be able to evaluate, not the state of the pin as originally assigned.

In any case the programming works very nicely, and is a variation of the programming shown on the Reference Page for attachInterrupt.

THANKS EVERYONE

I hope you are doing this through a resistor.