Arduino Uno and Interupts

Recently started a project using my first Aduino Uno with a relay shield. I have been testing programming to see what i can and can not properly accomplish prior to writing the actual code.

So here is a snippit of what i am trying:

void setup()
// setup of inputs outputs
DigitalPin(2,input);
Interrupt()
AttachInterrupt(0,resetoutput,RISING)
etc.....

void resetoutput()
digitalwrite(9,0)

void loop()
button = digitalread(4)
if button == high
digitalwrite(9,1)

Basically in a nut shell when input 4 goes high, it turns on output 9, and on a rising edge trigger of input 2 (interupt 0) it will run the interupt routine to reset output 9.

The problem is that this does not always work the way I want, sometime it resets the output on the rising edge sometimes on a falling edge. Am i missing something like maybe a debounce routine or something? Looking for guidance from other who have more skill in this than i do.

I understand what i wrote will not compile i was trying to remember it from memory, i was working on it at work and that is where the project is, otherwise i would have posted the exact code.

I was hoping that someone could tell me the correct way to use the attach interrupt routine and have it work properly all the time. Or maybe it does not work properly all the time. Like i said this is the first time i have worked with Arduino.

And it was also why i posted what i was trying to do.

Like i said sometimes it works as i anticipate it should on the rising edge of the switch it runs the ISR and resets the output. Other times it does not and resets the output on high or on trailing edge.

I can not have it reset on any other condition other than rising edge and thought this interrupt routine was the way to acheive that.

Agreed my mistake thought if i remembered some of it it might at least be enough to get me started but it was not so i called into work and had them send me the file, it is a culmination of two programs, one program allows me to send and receive inputs and outputs from the connected PC and the other part is the part i am working on that is to call the ISR when there is a falling edge. So here is the program and agreed i am new and it is a work in progress and probably not done as well as some of the more experienced users on this site.

camtest.ino (2.25 KB)

 attachInterrupt(0,resetgate,FALLING);
void resetgate(){
  digitalWrite(divert,0);
}
int divertinputeye = 2;

Is there a jumper from some other pin to pin 2? Is there something in the reading of the serial input which will generate a signal going to pin 2? .

Can you please explain the condition you expect will send a signal to pin 2?

Why do you think you need an interrupt to reset pin 9?

mrtweaver:
I was hoping that someone could tell me the correct way to use the attach interrupt routine and have it work properly all the time. Or maybe it does not work properly all the time. Like i said this is the first time i have worked with Arduino.

As I understand it:

The general rule is that you do not do anything inside your interrupt other than set state variables for your loop to catch. Don't work the relay inside your interrupt. Set a variable "we got an interrupt" and have your loop inspect it. Consider what should happen if there's another interrupt while the loop is dealing with the first one.

Remember that the ++ and -- operators are atomic. If you do "a = a + 1" in your loop, then an interrupt can fire after "a+1" is calculated but before the assignment to a happens. But if you go "a++", this cannot happen.

The problem is that this does not always work the way I want, sometime it resets the output on the rising edge sometimes on a falling edge. Am i missing something like maybe a debounce routine or something?

Quite possibly. Please next time wait until you can actually post the code. Posting some code from memory and asking why it doesn't work doesn't achieve much. The actual code might have a bug that you forgot to post.

How to use this forum

Meanwhile: Gammon Forum : Electronics : Microprocessors : Interrupts

Nick, you are correct and i thought my memory was better than it was. The only thing i was not sure of was how to post the code rather than uploading the file. Still reading and still learning.

As for the way in which things are wired, i followed the wiring example i found on an Aduino web site, since that time i have found other sites that show slight differences in wiring. So with that being said I will tell you what i have and how it is wired.

The base of course is Arduino Uno with the atmel 328. On top of that is a relay input sheild by Velleman part number VMA05.

Here is the link to the data sheet on it:

http://www.vellemanusa.com/downloads/0/modules/usermanual_vma05.pdf

Now as to the wiring if i understand what is being said correctly there is all the way on the right a +5 and a ground, then you have 6 inputs which are normally high and have a diode that you take to gnd to activate the input. And there is a ground to right after the inputs. this ground if i am reading correctly is the common for the inputs and should be tied to the ground for the +5. So i have that done. then i have a push on push off toggle switch connected between the grounds and input 2, i also have a toggle switch connected between input 5 and ground. When i turn on input 5 outputs 9 and 12 turns on. Ouput 12 follows input 5 so when 5 is on 12 is on and when 5 is off 12 is off. 9 on the other hand is like a latch when it is turned on it should wait for the edge of input 2. When the edge of input 2 is detected it will reset output 9.

Now the reason i used interupts is because when i started working with this device I was doing research and found that the interupts can be set for edge sensitive which is what i require.

When i place this in service the toggle switches will be replaced with thru beam sensors so it will be a positive make break. But first i need it to function properly all the time.

Here is a break down of the application, a piece is detected and inspected, if the piece is good it passes. If the piece is failed it will trigger input 5. Output 9 is connect to an actuator that will open a divert slot. The sensor connected to input 2 is just before the divert slot. So now when the piece drops down in the divert slot after the tail end of it has cleared the sensor it will reset the output turning it off and closing the divert slot. This all of course happens internally in the Arduino.

The other part is an application that is being written that i can send and receive data to and from the Arduino. I have tested the serial connection and it works fine.

As a test just to see if it is the USB/Serial communications causing the issues with the ISR routine. I did the following two test.

Test 1, disconnected the USB/Serial cable between PC and Arduino and the problem still presented itself.

Test 2, rewrote the application with just the inputs and outputs and took out all the serial communications. The problem still presented itself.

So to be it could be either i am using the Input output shield incorrectly, or i have the wiring wrong or there is something wrong with the shield, or there is some noise comming from somewhere. That is why i am here to ask the experts who have been doing the Arduino for a long period of time check over what i have and tell me if something other than my poor writing skills is causing the issues with the arduino not doing the edge detect properly all the time.

Thanks again and have a great day.

Ok lets talk about pin 2, if you look at the documentation about the Velleman Input Output shield they give schematics, the schematics show that the 5v is attached thru a diode and a 470 ohn resistor to each input pin and then a diode is also connected to between the input pin and the screw terminal where the end user then takes and connects to ground to activate the pin.

so yes there is a pull up resistor to pin 2. or at least according to the shield documentation.

Here is the one thing i just recently tried.

I first took and in the ISR routine added in
test = !test
digitalWrite(shuttle,test)

when i did this each time i bumped the case the Arduino is mounted in the output shuttle would change state.

So i figured there was something that when it was bumped jarred or otherwise was tripping the ISR and running it.

So I took the following two lines of code out of the program.

interrupts()
attachInterrupt(0,resetgate,FALLING)

And just so you know the case is grounded and bonded and the Arduino is on Nylon standoffs using nylon bolts.

Then i added in the following lines of code:

int trip = 0;
trip = digitalRead(divretinputeye)
if (trip == LOW) {
resetgate()
}

Now of course it does not look for the rising or trailing edge which i need but it does work without fail. So now the dumbest question of all what was causing the ISR to run when you bumped the box the Arduino is mounted in?

And can one make their own edge detect that does not use the interupts?

Please post actual code, in code tags.

Preferably a Minimal, Complete, and Verifiable example.

And can one make their own edge detect that does not use the interupts?

trip = digitalRead(divretinputeye);
if (trip == LOW && oldTrip == HIGH) {
  resetgate();
}

oldTrip = trip;   // oldTrip should be a global variable