external interrupt help!

hello ladies, and gents.

i was having some trouble with getting my external interrupts to work with a code of mine, so i created a simple code to make sure i was implementing it correctly…

INT0 is supposed to increase trigger by 1; likewise,
INT1 is supposed to increase trigger2 by 1…

***Note: i have also tried using eimsk eicra isr configuration in place of attachinterrupts… same outcome…

i run:

#include <avr/interrupt.h>

int trigger, trigger2=0;

void setup()
{
  Serial.begin(9600);          //Open the serial port at 9600
  
  attachInterrupt(0, i_Zero, RISING);
  attachInterrupt(1, i_One, RISING); 
}


void i_Zero()
{
  trigger++;
}

void i_One()
{
  trigger2++;
}


void loop()
{

  Serial.print("trigger: \t");
  Serial.print(trigger);
  Serial.print("\ntrigger2: \t");
  Serial.print(trigger2);
  Serial.print("\n\n");
  delay(1500);
}
  • note that i only activated INT0 once… and then-after, INT1 twice…
    INT1 is incrementing properly (“trigger2” by 1) and INT0 is causing the value of “trigger” to continuous change its value - even after i have stopped activating it!

trigger: 484
trigger2: 0

trigger: 484
trigger2: 1

trigger: 511
trigger2: 1

trigger: 538
trigger2: 2

trigger: 624
trigger2: 2

trigger: 710
trigger2: 2

do you think it’s the arduino board itself? it’s brand new… i haven’t had any other arduino boards to run the code on yet, when i get to school, i will ask to borrow someone elses…

thanks for any ideas/input

Switch needs debouncing ?

Depending on what you have attched to the interrupts getting wild figures from a 'bouncy' switch isn't unusual. If this is likely to be the case try a lastcontact variable set from millis(); and ignore it if it isn't at least say 100 ms ago.

thank you, pluggy

i will try it in a moment - i have to go do something real quick...

i am using a pushpin to interrupt it, and using it on pin3 doesn't cause any irratic figures, only on pin2 (and they even go negative - when it should only add up (positively)...

so i'm thinking it's probably not the pushpin causing any bouncy signals...

thanks again, and i'll try in a few moments.

I have uploaded you code and attach buttons from ground to pins 2 and 3.

I have the same problem. Trigger keeps climbing erratically but trigger2 responds as it should when the button is pressed. Even if i use one of the other modes trigger doesn't respond correctly.

BTW the buttons in use are actually relays tied to an RC receiver.

#include <avr/interrupt.h>

[glow]volatile [/glow]int trigger, trigger2=0;

void setup()
{
  Serial.begin(9600);          //Open the serial port at 9600
  
  attachInterrupt(0, i_Zero, RISING);
  attachInterrupt(1, i_One, RISING);
}


void i_Zero()
{
  trigger++;
}

void i_One()
{
  trigger2++;
}


void loop()
{
[glow]  int triggerSave, trigger2Save;

  uint8_t SaveSREG = SREG;   // save interrupt flag
  cli();   // disable interrupts
  triggerSave = trigger;
  trigger2Save = trigger2;
  SREG = SaveSREG;   // restore the interrupt flag[/glow]

  Serial.print("trigger: \t");
  Serial.print([glow]triggerSave[/glow]);
  Serial.print("\ntrigger2: \t");
  Serial.print([glow]trigger2Save[/glow]);
  Serial.print("\n\n");
  delay(1500);
}

Some details here…
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1261124850

the problem really is with the pin 2 & 3 on the arduino board(s) interfering and activating eachother.

solution was to instead us a 3-terminal toggle-switch or 3-terminal pushpin so that for:

pin 2: idle it’s always ground, and when pushed hits +5V; and oppositely
pin 3: idle it’s always +5V, and when pushed hits ground

and then a slight 10microsecond delay to debounce for myself and those of us without ninja-swift clicking speed/skill

thank you all, again
for the help and troubleshooting practices,

and codebadly for your teachings