Arduino Multitasking Tutorial - How to use millis() in Arduino Code

Hello all,
I am new to the Arduino UNO and MEGA 2560 and electronics in general.

I found this tutorial Arduino Millis Tutorial - How to use millis() in Arduino Code for Multitasking

Arduino Multitasking Tutorial - I created, it as instructed, and the led's blink as shown in the video of the tutorial.

PROBLEM:
When I press on my button all the led's go out, "no lights on", then when I release the button the led's do come back on, and blink normally again. The video in the tutorial does not show all the led's going out "no lights on" when the button is pressed.

I am confused even watching the video what is supposed to happen when the button is pressed. It looks like maybe one led or 2 led's are supposed to go off "no light" the video blinks are very fast hard to see what is really happening to the led's on a button press. Thanks for any help

Have you wired the switch across the supply?

Thank you for your reply,

Yes I did, 5v to the temp on button 1 lead
then pin 2 thru a 10k resistor to the button other lead, to ground

Post your code, post your schematic.
I don't know why you are using a pulldown, when you could use a built-in pullup.

Thank you,

Here are the code and the diagram and the link to the tutorial.

I added the
#include <elapsedMillis.h> library which is not mentioned in the tutorial, unless I missed that.

Please advise

code_original_multi_task.txt (2.95 KB)

if (buttonPushed = true)

Oops

In future, please use code tags when posting code

/* Arduino Multitasking
    Author : CircuitDigest (circuitdigest.com)
*/

int led1 =  6;      // led1 connected at pin 6
int led2 =  7;       // led1 connected at pin 7
int toggleLed = 5;    // push button controlled led connected at pin 5
int pushButton = 2;    // push butoon connected at pin 2 which is also interrupt pin

int ledState1 = LOW;  // to determine the states of led1 and led2
int ledState2 = LOW;

unsigned long previousMillis1 = 0;  //store last time LED1 was blinked
const long period1 = 1000;         // period at which led1 blinks in ms

unsigned long previousMillis2 = 0;  //store last time LED2 was blinked
const long period2 = 200;             // period at which led1 blinks in ms

int debouncePeriod = 20;            // debounce delay of 20ms
int debounceMillis = 0;            // similar to previousMillis

bool buttonPushed = false;     // interrupt routine button status 
int ledChange = LOW;      // to track the led status last 
int lastState = HIGH;    // to track last button state

void setup() {
  pinMode(led1, OUTPUT);               // define pins as input or output
  pinMode(led2, OUTPUT);
  pinMode(toggleLed, OUTPUT);
  pinMode(pushButton, INPUT);
  attachInterrupt(digitalPinToInterrupt(pushButton), pushButton_ISR, CHANGE);  // use interrupt pin2 
}

void pushButton_ISR()
{
  buttonPushed = true;   // ISR should be as short as possible
}

void loop() {
  unsigned long currentMillis = millis(); // store the current time

  if (currentMillis - previousMillis1 >= period1) {    // check if 1000ms passed
    previousMillis1 = currentMillis;   // save the last time you blinked the LED
    if (ledState1 == LOW) {  // if the LED is off turn it on and vice-versa
      ledState1 = HIGH;   //change led state for next iteration
    } else {
      ledState1 = LOW;
    }
    digitalWrite(led1, ledState1);    //set LED with ledState to blink again
  }

  if (currentMillis - previousMillis2 >= period2) { // check if 1000ms passed
    previousMillis2 = currentMillis;   // save the last time you blinked the LED
    if (ledState2 == LOW) { // if the LED is off turn it on and vice-versa
      ledState2 = HIGH;
    } else {
      ledState2 = LOW;
    }
    digitalWrite(led2, ledState2);//set LED with ledState to blink again
  }

  if (buttonPushed = true)    // check if ISR is called
  {
    if ((currentMillis - debounceMillis) > debouncePeriod && buttonPushed)  // generate 20ms debounce delay to avoid multiple presses
    {
      debounceMillis = currentMillis;      // save the last debounce delay time
      if (digitalRead(pushButton) == LOW && lastState == HIGH)     // change the led after push button is pressed
      {
        ledChange = ! ledChange;
        digitalWrite(toggleLed, ledChange);    
        lastState = LOW;
      }
      else if (digitalRead(pushButton) == HIGH && lastState == LOW)     
      {
        lastState = HIGH;
      }
      buttonPushed = false;
    }
  }
}

Also buttonPushed should be qualified volatile.

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R