Attachinterrupts w/button press act different than internal pullup w/button?

So my code is very simple. I push a button, and make a led blink.
I’m fidling with interupts, and i get this weird acting thing going. It’s like the internal clock speeds up.

This is my setup:

void setup() {
  Serial.begin(9600);
  
  pinMode(2, INPUT);
  attachInterrupt(0, red, RISING);
  
  //pinMode(buttonPinRed, INPUT_PULLUP);
  pinMode(buttonPinYellow, INPUT_PULLUP);
  pinMode(12, OUTPUT);
  digitalWrite(12, LOW);
  
  analogReference(EXTERNAL);
  pinMode(ypin, INPUT);
}

in my main loop i have a digitalRead of the yellow pin, which then sends it to the red() function.

So both buttons, lead to the same function.
The red() function blinks a led. Simple stuff.

But when i push the interupt button, the led doesn’t blink, it just glows, but if i increase the delay by putting some zeroes behind, it blinks. So it acts like it speeds up the internal clock.

When i push the pullup button, it blinks like it should.

Why? I am lost.

Floating pins affect interrupt pins the same as anything else. Make pin 2 use a pullup or add an external pulldown resistor and see what happens.

pin 2 is connected through a resistor to 3v. I don't have any floating pins.

Changed it now and hooked pin 2 to ground and button to 3v. Still the same. Interupt causes it to glow dim, instead of flashing with a slow flash.

Yeah, i changed so pin 2, the interupt pin, is connected to ground, so i actually pull it up.

Sorry for any confusion, byt no, they are not the same pins. Two different pins, with two different buttons. buttonPinYellow is pin 11.

mollekake: Sorry for any confusion, byt no, they are not the same pins. Two different pins, with two different buttons. buttonPinYellow is pin 11.

Yet another reason why posting the whole code is better than posting snippets.

When you talk about pin 2 being connected to ground or pulled up, I assume there is a button involved in there somehow? Is that right? How is that actually wired?

Ardunaut: If you have attachInterrupt(0, red, RISING); and your pins all are pulled high, then its always high = rising.

No, RISING is quite different from HIGH. I can use an active LOW button ( switch connects to ground input is pulled up) with a RISING interrupt. It will just mean that the interrupt fires at the moment the button is released and transitions from LOW to HIGH.

Interupt causes it to glow dim, instead of flashing with a slow flash.

Can we see ALL of your code?

Ok i changed some of the code to make it easier, and removed some irrelevant stuff.

I have it wired like this:
pin 2(interupt 0 is on this pin) is connected to side A of the button, and through a 10k resistor to ground. side B of the button is connected to 3v.

pin 11 is connected to another button on side A. side B of button is connected to ground.

code:

#include <IRremote.h>

const int buttonPin = 11; // Button pin. Pulled up. Triggered when connected to ground.

const int ypin = A3; 

bool blinkloop = false;

void setup() {
  Serial.begin(9600);
  
  pinMode(2, INPUT);
  attachInterrupt(0, blinkLED, RISING);
  
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(12, OUTPUT);//LED
  digitalWrite(12, LOW);
  
  analogReference(EXTERNAL);//3v connected to reference for accelerometer
  pinMode(ypin, INPUT);
  
  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);
}

void loop() {
  Serial.println(analogRead(ypin));
  if ((digitalRead(buttonPin) == LOW)){
    blinkLED();
  }
}

void blinkLED(){
  Serial.println("interrupt");  
  digitalWrite(5, HIGH);//turn accelerometer on
  blinkloop = true;
  while(blinkloop){
    digitalWrite(12, HIGH);
    delay(300);
    digitalWrite(12, LOW);
    delay(450);
    
    Serial.println(analogRead(ypin));
    if(analogRead(ypin) > 520){//when tilted too much, stop
      blinkloop = false;
    }
  }
  digitalWrite(5, LOW);//turn accelerometer off
}

I just want the interupt to work, so i don’t have to use this other button method.

Why 3V and not 5V? What type of Arduino is this?

It's a Nano, only because the accelerometer needs/uses 3v, so just for ease really.

But now i don't use the accelerometer on the same vcc strip as the button, so no need really. I hooked the button up to 5v, no change. The accelerometer has a built-in regulator, since it only needs 3v.

Aha, the problem reveals itself.

blinkLed has delays in it. It is also used as an ISR. Delay relies on the timer 0 overflow interrupt to function. That interrupt is disabled inside an ISR. So when the blinkLed code is called from the interrupt, the delay can't do its job and things lock up.

ISR functions should be super fast, no delays, no waiting around for something to happen, just get in set a flag or change a value and get out.

mollekake:
It’s a Nano, only because the accelerometer needs/uses 3v, so just for ease really.

But now i don’t use the accelerometer on the same vcc strip as the button, so no need really. I hooked it up to 5v, no change.

Well that’s silly since the pin needs a little more than 3V to read HIGH. You should be using 0 and 5V for all the digital stuff on a 5V board.

Aha ok. So how can i do it? i'm just trying to find a way that consumes as little power as possible. Also looking into sleep modes. I just want the led to blink once i press it, stop blinking when i need it to, and go back to waiting for a button press.

Also, changed from 3v to 5v.

Let the interrupt set a flag, in loop test for that flag and if it is true then run the blinkLed routine from there.

Really, this isn't an appropriate place to use an interrupt at all. Interrupts are for things that happen really fast that you might miss while other code is running or where timing is crucial to the microsecond.

Unless your name if Freddy Fast Finger and you can push and release a button in under a millisecond, then there is no need for an interrupt to ever read the state of a button.

But isn't that the same as waiting for a button press by reading the digital pin in the main loop? Or should i wake it up at interrupt, and put it back to sleep and wait for interrupt when it has stopped blinking?

aslo, any tips on how sleep modes work/how to do it would be great

I might have misunderstood the point of an interupt then i guess :P

Read this.

http://www.gammon.com.au/interrupts