Problem using external interrupts

Hi!! I’ve one problem using interrupts with my duemilanove, the code is below. When I touch the pushbutton’s body with my fingers the led is blinking in a crazy way, but when I do click without touching it with my fingers the interrupt doesn’t work!! I have connected the pushbutton with an 560 ohms resistor. Anyone knows what’s the problem??

Thanks and greetings!!

#include <interrupt.h> // this line can be deletted and it works!!!

int led = 13;
int button = 2;
volatile int state = LOW;

void setup()
{
pinMode(led, OUTPUT);
pinMode(button, INPUT);
attachInterrupt(0, temperatura, RISING);
}

void loop()
{
digitalWrite(led, state);
delay(100);
}

void temperatura()
{
state = !state;
}

Looks like you need a pullup resistor on the input pin. You can read more here: http://www.arduino.cc/en/Tutorial/DigitalPins

Thanks very much for your answer, I tried using it (high and low), but it doesn't work...

Thanks again!!

void setup()
{
pinMode(led, OUTPUT);
pinMode(button, INPUT);
digitalWrite(button, HIGH); // turn on pullup resistors
attachInterrupt(0, temperatura, RISING);
}

did it solve the blinking problem when touching with your finger?

Have you tried a test to see if you can get the button to light the led without using an interrupt?

Hi again!! I think only exist pullup resistor, not pulldown, so for turn on the led when pressing the button I obtain results using this code, so I will work in this way:

int led = 13;
int button = 2;
int state = LOW;

void setup()
{
pinMode(led, OUTPUT);
pinMode(button, INPUT);
digitalWrite(button, HIGH); // turn on pullup resistors
}

void loop()
{
digitalWrite(led, state);
state =! digitalRead(button); // button connected to ground
}

Thanks very much for your help. Now it isn't crazy blink and I can touch with my fingers, but bounces doing click makes bad working, I can see an very fast blink from the led when I press or left pressing the button.

int led = 13;
int button = 2;
int state = LOW;

void setup()
{
pinMode(led, OUTPUT);
pinMode(button, INPUT);
digitalWrite(button, HIGH); // turn on pullup resistors
attachInterrupt(0, temperatura, FALLING);
}

void loop()
{
delay(100);
}

void temperatura()
{
detachInterrupt(0);
state = !state;
digitalWrite(led, state);
attachInterrupt(0, temperatura, FALLING);
}

You need to debounce the input, see : http://www.arduino.cc/en/Tutorial/Debounce

Interrupts can make things more complicated, do you really need to use an interrupt? What else do you want to do in your sketch?

Hi!! Thanks for your answer. I build a simple hardware debounce with one resistor and one capacitor and it works fine, but I want to do it using software. I'm used to PIC microcontrollers and assembler using interrupts, so left using it... Also for battery operated circuits (arduino pro mini 3.3v or funnel IO) I need sleep the arduino and wake up from interrupts. Now I'm using an SHT15 humidity and temperature sensor and a bluetooth uart operated module for connect it to the computer and watch data with a serial software each 2 seconds, but I would like to have the arduino sleeping and watch data when a button is pressed and go on sleeping. I'm going to try with software debounce.

Greetings!!!

Really I don't know how to use software debounce with interrupts, so I think best is trying with timer interrupts... (delays don't work using interrupts!!!!)

You don't need to do the delay in the interrupt. If you wake on interrupt you can have your main loop do the switch state checking with debounce logic.

Thank you very much!!! I think the problem is solved, the programm seems to work!!!

Greetings!!

Good to hear you have it going.
Why not post your code for others that may want to see how to do something similar.