Using two LEDs and a tactile switch

My question might be very basic. Unfortunately, I am yet to get the understanding. Here's the scenario. There are two LEDs.

Case 01: One of them blinks for ever at 1000 mSec interval

Case 02: The tactile switch is connected to digital input. When the digital input is HIGH (when the switch is pressed) the second LED blinks rapidly at 100 mSec interval for 1 Sec and turns LOW. Again, when the input becomes HIGH this activity by LED 02 restarts

Now, what-so-ever is the LED 02's activity (HIGH/LOW), the LED 01 shouldn't stop blinking (the loop needs to go on)

I want to get the hint at how this works. I mean how the multi-tasking is done (is this possible?). Can someone please provide the sketch (bear with me :slightly_frowning_face: )? (just not able to understand at what goes inside the void loop(){})

I want to get the hint at how this works.

That depends on how you have written the code. If you have not written anything, read, understand, and embrace the blink without delay example.

Using that approach, the two actions are complete independent, although the decisions are sequential.

Can someone please provide the sketch

No. You'll learn far more doing it yourself.

Just found the right link for my query.

Demonstration code for several things at the same time - Project Guidance - Arduino Forum ("SeveralThingsAtTheSameTimeRev1")

Was not able to findout without the right words. Thanks @PaulS.

Write some code, try it out, if you run into trouble ask for help.
Go through all the examples that are shipped with the IDE.

Promise us you will never use the delay() function.

I _______________ will never use the delay() function in any of my sketches.

@Larry "Promise us you will never use the delay() function". Finding millis() easier just like delay. Ok, atleast getting the essence of it.

Thanks again

However, I am still confused on when to use Interrupts and millis(). At many a times, I feel the nano timer precision in Interrupts is an over kill

The secret is that turning on an LED is just a single event. (something like digitalWrite(somePin, HIGH). Once you've executed such a task, you don't have to sit and watch it. It will stay on, just fine all by itself.

When you turn it off again. Once more just a simple task (digitalWrite(somePin, LOW). Having done so the sketch can continue to go about it's business, nothing more to see here folks.

So to blink an LED all you have to do, is drop in now and again, to see if it needs attention.

Think of security guard that locks all the doors at night and unlocks them the next morning. How does he keep them ALL unlocked for the whole day. After all he can only stand by one of them? Now you see how rdiculous such a problem sounds?

I made it a little more complicated with using the enumerated data type (enum) but you can visualize the logic better, I feel:

this compiles but I did not test it:

typedef enum { 
  BLINKING, NOT_BLINKING} 
BlinkState;

BlinkState ledTwoState;

int led1 = 8;
int led2 = 9;
int buttonPin = 4;
int lastButtonState;

void setup() 
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() 
{
  blinkLedForever();
  int buttonState = digitalRead(buttonPin);
  if (buttonState == LOW && lastButtonState == HIGH && ledTwoState == NOT_BLINKING)
  {
    ledTwoState = BLINKING;
  }
  if (ledTwoState = BLINKING) 
  {
    blinkFast();
  }
  lastButtonState = buttonState;
}

void blinkLedForever()
{
  static unsigned long lastBlinkTime;
  if (millis() - lastBlinkTime >= 1000UL)
  {
    digitalWrite(led1, !digitalRead(led1));
    lastBlinkTime += 1000UL;
  }
}

void blinkFast()
{
  static unsigned long LedTwoLastTime;
  static int counter = 0;
  if (millis() - LedTwoLastTime >= 100UL && counter <= 10)
  {
    digitalWrite(led2, !digitalRead(led2));
    LedTwoLastTime += 100UL;
    counter++;
  }
  else if (counter > 10)
  {
    digitalWrite(led2, LOW);
    counter = 0;
    ledTwoState = NOT_BLINKING;
  }
}