Simple blink LED until interrupt signal, then turn off LED blink (use millis)

Hello.

Trying to create a press button interrupt while blinking an LED using the millis function.

LED blinks until push button (switch) is closed. My code is not working as desired. The press button does not stop the LED blink until the millis “wait” time is over. (So it is acting like a delay not an interrupt).
I do have a hardware debounce circuit for the switch.

Here is my code. It does exactly as told but not what I want. :confused:

// blink LED until interrupt button is pushed then turn off LED using millis instead of delay

int rLED = 6;
int ledState = LOW;

volatile boolean interruptButton = false; //pin D2

unsigned long previousMillis = 0; //will store last time update

const long intervalOn = 1000;
const long intervalOff = 300;

void setup()
{
  attachInterrupt(0, interruptFunction, HIGH);
  pinMode(rLED, OUTPUT);
  digitalWrite (rLED, LOW);
}

void loop()
{
  if (interruptButton == true) //true/false to reverse button action
   {
    digitalWrite (rLED, LOW);
   } 
  else 
{
    previousMillis = millis();
    while ((millis() - previousMillis) <= intervalOn)
  {
    ledState = HIGH;
    digitalWrite (rLED, ledState);
  }
  
   previousMillis = millis();
   while ((millis() - previousMillis) <= intervalOff)
  {
    ledState = LOW;
    digitalWrite (rLED, ledState);
  }
}
}

void interruptFunction()
{
  if (interruptButton == false)
  {
    interruptButton = true;
  }
  else
  {
    interruptButton = false;
  } 
}

Why do you think you need an interrupt for a human-pressed switch? Is there some reason that the LED must stop blink RIGHT NOW? Wouldn't a few microseconds after the human pressed the switch be soon enough?

You need to get rid of the while statement. It is creating a blocking section of code, where the interrupt handler's actions are ignored.

The blink without delay example shows how to blink an LED without delay IF IT NEEDS TO BE BLINKING. The switch press defines whether or not it needs to be blinking. You do not need interrupts for that.

attachInterrupt(0, interruptFunction, HIGH);

HIGH isn't one of the choices here. You get LOW (don't use it it will fire the interrupt repeatedly over and over as long as the button is LOW) or FALLING or RISING. Do you have a pull-down resistor to keep the button reading low when it's not pressed? It would be more common to use INPUT-PULLUP and have the button read LOW when pressed.

while ((millis() - previousMillis) <= intervalOn)
  {
    ledState = HIGH;
    digitalWrite (rLED, ledState);
  }

That’s not blink without delay. You’ve just created a new blocking function just like delay. Blink without delay uses an if so the code isn’t trapped right there and the rest of the loop can run while it is waiting for time to toggle the LED.

Thanks for the feedback Delta_G.

I changed the "HIGH" to "CHANGE". For some reason I thought HIGH & LOW where acceptable to use. Checked my book and RISING, FALLING & CHANGE are allowed.

I'm using a 10k ohm pull-up resistor on the switch.

Ok, the while loop blocks the interrupt. Makes sense. Does a for loop block an interrupt too?

Changed "while" to "if" and no more blinking. Stays on solid until switch is pressed.

I'll take a look at the blink without delay example.

Thanks again.

No, the while loops do NOT block the interrupt. What gave you that impression? The interrupt still happens during the while loop, but it's not until the while loops are done and the loop function repeats that it gets back to that line at the beginning to check the variable you set in the interrupt.

The reason it was always on before was that you're using the same previousMillis variable against both intervals. You'll need to keep up with those separately OR have another variable, a boolean variable, to track which one of the if statements you should run.

Yes, I understand that the interrupt is running but it can’t “interrupt” a while loop while it’s in its “loop”.

I played around with the code some more and cannot see how to make it have two “different blink times” (on & off) with only one “millis”.

currentMillis = millis() and so does currentMillis2 = millis(). How do you get around that one.

Here is the latest code:

// blink LED until interrupt button is pushed then turn off LED using millis instead of delay

int rLED = 6;
int ledState = LOW;

volatile boolean interruptButton = false; //pin D2

unsigned long previousMillis = 0; //will store last update for on
unsigned long previousMillis2 = 0; //will store last update for off

const long intervalOn = 1000;
const long intervalOff = 300;

void setup()
{
  attachInterrupt(0, interruptFunction, CHANGE);
  pinMode(rLED, OUTPUT);
  digitalWrite (rLED, LOW);
}

void loop()
{
  if (interruptButton == true) //true/false to reverse button action
  {
    digitalWrite (rLED, LOW);
  }
  else
  {
    unsigned long currentMillis = millis();
    
    if (currentMillis - previousMillis <= intervalOn)
    {
      ledState = HIGH;
      digitalWrite (rLED, ledState);
      
      unsigned long currentMillis2 = millis();
      if (currentMillis2 - previousMillis2 >= intervalOff)
   
      ledState = LOW;
      digitalWrite (rLED, ledState);
      previousMillis = currentMillis;
      previousMillis2 = currentMillis2;
    }
  }
}

void interruptFunction()
{
  if (interruptButton == false)
  {
    interruptButton = true;
  }
  else
  {
    interruptButton = false;
  }
}

triode90: Yes, I understand that the interrupt is running but it can't "interrupt" a while loop while it's in its "loop".

If the interrupt is tripped it will fire off even while the while loop is looping. The only thing that will stop that interrupt from firing is if you turn it off. Otherwise, it will fire the instant that pin state changes. It won't cause the while loop to exit, the interrupt will return right into the same line it interrupted. But it will indeed fire during the while loop or any other loop.

triode90: I played around with the code some more and cannot see how to make it have two "different blink times" (on & off) with only one "millis".

currentMillis = millis() and so does currentMillis2 = millis(). How do you get around that one.

The current time is the current time is the current time. You don't need two copies of the current time. It's the time your timing against, the previousMillis, that you need two of. Think of that as the starting point for your "timer". You need to set the starting point for turning it off as soon as you turn it on and set the time to start timing when to turn it on as soon as you turn it off.

Thanks for the clarification on the interrupt. It will not interrupt delays, that why we use millis.

Current time is current time! I got it, Thanks!

Current time is the reference time and the interval "count" is controlled by delta; current - previous or - previous2, etc,

Played around a bit more and the interrupt works perfect if I just use one interval time. (blink on is same as blink off time).

My trouble is merging or integrating the two intervals having different values.

Code with some // so I can see my changes.

// blink LED until interrupt button is pushed then turn off LED using millis instead of delay

int rLED = 6;
int ledState = LOW;

volatile boolean interruptButton = false; //pin D2

unsigned long previousMillis = 0; //will store last update for on
unsigned long previousMillis2 = 0; //will store last update for off

const long intervalOn = 1000;
const long intervalOff = 300;

void setup()
{
  attachInterrupt(0, interruptFunction, CHANGE);
  pinMode(rLED, OUTPUT);
  digitalWrite (rLED, LOW);
}

void loop()
{
  if (interruptButton == true) //true/false to reverse button action
  {
    digitalWrite (rLED, LOW);
  }
  else
  {
    unsigned long currentMillis = millis();
    
  
    if (currentMillis - previousMillis >= intervalOn)
    {
      previousMillis = currentMillis;
      if (ledState == LOW)
      ledState = HIGH;
      //else 
      //ledState = LOW;
      //digitalWrite (rLED, ledState);

      if (currentMillis - previousMillis2 >= intervalOff)
      previousMillis2 = currentMillis;
      if (ledState == HIGH)
      ledState = LOW;
      //else
      //ledState = HIGH;
      digitalWrite (rLED, ledState); 
    }
  }
}

void interruptFunction()
{
  if (interruptButton == false)
  {
    interruptButton = true;
  }
  else
  {
    interruptButton = false;
  }
}

triode90: Thanks for the clarification on the interrupt. It will not interrupt delays, that why we use millis.

Nope. the interrupt will even run during the delay. The ONLY time the interrupt will not fire is if you have specifically turned it off by detachInterrupt or sei().

You were right. The interrupt does “fire” during a delay.

Stuck on integrating the two intervals having different values.

if (currentMillis - previousMillis >= intervalOn)
    {
      previousMillis = currentMillis;

Think about this for a minute. If you set previousMillis to currentMillis here, then when you look at that if statement it is working on how long has it been since the light was turned on. But that's not what you want. You want to know how long it has been since it was turned off. So the previousMillis that you set inside each one of those will not be the same one you tested to get into it.

Stuck on integrating the two intervals having different values.

Study this modification of the "blink without delay" sketch

/* Blink without Delay

 Turns on and off a light emitting diode (LED) connected to a digital
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.

 The circuit:
 * Use the onboard LED.
 * Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO 
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino model, check
  the Technical Specs of your board  at https://www.arduino.cc/en/Main/Products

 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 modified 11 Nov 2013
 by Scott Fitzgerald
 modified 9 Jan 2017
 by Arturo Guadalupi


 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to set a pin number :
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long intervalOn = 1000;           // interval at which to blink (milliseconds)
const long intervalOff= 5000;
unsigned long interval = intervalOff; //start with led off

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT); 
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();
  
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa
    //switch intervals off/on
    if (ledState == LOW) {
      ledState = HIGH;
      interval = intervalOn;
    } else {
      ledState = LOW;
      interval = intervalOff;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Thanks for the responses and input Delta_G and cattledog! I am exhausted tonight but will have a look and try out tomorrow evening.