Blink one led with many interval without delay function

Hello there, perhaps someone can help figure out my problem. I am new with arduino, and just about happy using delay function, but now I want avoiding using this function since it halt everything. can someone please help me on how to change the code below without using delay function, I have try many examples millis and interval but my small brain just can't adapted that code into my own will.

void setup()
{
  pinMode(1, OUTPUT); 
}
void loop()
{
  digitalWrite(1, HIGH);
  delay(100);
  digitalWrite(1, LOW);
  delay(100);
  digitalWrite(1, HIGH);
  delay(100);
  digitalWrite(1, LOW);
  delay(2000);  
}

Hi and thanks for using code tags.

Post your best attempt at the code without delay() and tell us what happens when you compile or run it. We can help spot where you are going wrong.

Thanks for the reply PaulRB
here the code

unsigned long previousMillis=0;
 
void setup() {
 pinMode(1, OUTPUT);

}
 
void loop() {
 unsigned long currentMillis = millis() % 2000; 

  if (currentMillis > 0 && currentMillis < 100) {
     digitalWrite (1, HIGH);
  }
  else if (currentMillis > 100 && currentMillis < 200) {
     digitalWrite (1, LOW);
  }
  else if (currentMillis > 200 && currentMillis < 300) {
     digitalWrite (1,HIGH);
  }
  else if (currentMillis > 300 && currentMillis < 2000) {
     digitalWrite (1,LOW);
  }
  else {
    previousMillis = millis();
  }
  
 }

compile the sketch success 100% and when I run it the led blink twice than off, but no loop.

Try this:

 unsigned long currentMillis = millis() % 2000;

PaulRB: Try this:

 unsigned long currentMillis = millis() % 2000;

awesome, it work like a charm, thanks PaulRB.

h4n5V: awesome, it work like a charm, thanks PaulRB.

But do you understand why it works? That is my goal.

PaulRB: But do you understand why it works? That is my goal.

No I don't know why it work, must something to do with %. Can you explain it, i will happy to read that. i try make this code as logic as i can

The millis() function starts at zero when you give power to the Arduino or press reset. After that, it continues to increase forever. It does not go back to zero. Well, actually, "forever" is several weeks.

You want to repeat your pattern of flashes every 2000ms. You need millis() to go back to zero after 1999ms. But millis() does not do that. So I calculated a value from millis() which does go back to zero after 1999. That's what the "%" operator does, it calculates the modulus. You can read about it in the "Language Reference" page from the menu at the top of the forum, under "Resources".

thanks PaulRB you really helpfull