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.

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.

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;
  }
}

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;
  }
}

You were right. The interrupt does "fire" during a delay.

Stuck on integrating the two intervals having different values.

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.