Interruption with button

Hey!

I'm having an issue with using the button for interruption in my project. The idea is simple - turn the LED on/off, with every button's toggle). Here's the source:

volatile boolean state = 0;

void setup()
{
  pinMode(13, OUTPUT);  
}

void button(){
  delay(25);
  state = !state;
  while(digitalRead(2) == HIGH);
  delay(25);
} 

void loop()
{
  attachInterrupt(0, button, RISING);
  
  switch (state){
    case 0:
      digitalWrite(13, HIGH);
      break;
    case 1:
      digitalWrite(13, LOW);
      break;
    default:
    break;
  } 
}

I plugged the Arduino's (Pro Mini) PIN2 (int.0) to VCC (5V) through a button switch and to GND through two 4k7 pulldown resestors (together almost 10k, so the everywhere-reccommended value). The button works perfectly fine, since removing button() function and attachInterrupt(0, button, RISING);, and adding

if (digitalRead(2) == HIGH)
  {
    delay(25);
    state = !state;
    while(digitalRead(2) == HIGH);
    delay(25);

before switch() works great - changing the LED states just as I want.

I did try changing the attachIterrupt() mode to FALLING or CHANGE, but it doesn't make much difference (I knew from the very beggining it won't, tried "just in case" :stuck_out_tongue: ).

Any ideas what am I doing wrong?

void button(){
  delay(25);
  state = !state;
  while(digitalRead(2) == HIGH);
  delay(25);
} 

void loop()
{
  attachInterrupt(0, button, RISING);

Don't do delays in an ISR.

The button works perfectly fine, ...

It's not really clear what your problem is.

Oh, sorry, I forgot to mention what’s wrong -.-

While using interruption, the button doesn’t always change the state of LED. Sometimes it does, sometimes not (f.ex. it doesn’t change the value at all, or pressing changes but releasing changes again). I guess it has to do something with unstable state for short period of time after pressing (I read it’s around 20ms), thet’s why I used delay() function to wait that out.

I deleted the delay() functions, problem stayed. I know that using them i not the greatest thing to do, but since I’m just staring with microcontrollers, and it won’t hurt my project if I froze it for some time, I decided to go for it.

Maybe read this then: Gammon Forum : Electronics : Microprocessors : Switches tutorial

What are you trying to do?

It is extremely unlikely that interrupts would be needed to detect humans pushing a button-switch. Humans are very slow.

Polling the switch pin in every iteration of loop() should be quite fast enough to detect EVERY button press.

Why are you attaching the interrupt in loop() ? Normally that only needs to be done once in setup().

...R

Nick, thanks for the links - I've learned a lot, really inreresting articles!

What are you trying to do?

I'm using button to switch between screens on my LCD (screen 1 shows values of gyroscope, screen 2 from accelerometer, etc.). The real code looks a bit different then what I've put in 1st post, I gave only the equivalent of a bigger code.

Yeah, I thought about this issue, and decided interrupts are not that much necessary in my project, so I moved the button-switch part to loop().
However, I learned sth new - interrupts, so I'm glad I even tried ^^