Dual timing millis

Hello all

Looking for some with blinking led.

I want to let to stay on for 50 milliseconds, bit the time btw blinks to be 1000 miliseconds.

Doing it with delay is straightforward but we all know this kill everything else in the program.

How would i go around this to in order to get the time btw blinks?

unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 100;  //the value is a number of milliseconds
const byte ledPin = 13;    //using the built in LED

void setup()
{
  Serial.begin(115200);  //start Serial in case we need to print debugging info
  pinMode(ledPin, OUTPUT);
  startMillis = millis();  //initial start time
}

void loop()
{
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    digitalWrite(ledPin, !digitalRead(ledPin));  //if so, change the state of the LED.  Uses a neat trick to change the state
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }
}

Thank you

Is it 50ms ON, 1000ms OFF or 50ms ON, 950ms OFF?

I've modified your code for 50ms ON, 1000ms OFF but hopefully it's not a prob for you to tweak it for the other timing it that's what you want! :slight_smile:

unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis; 
const unsigned long OFFtime = 1000;  //OFF time
const unsigned long ONtime = 50;  //ON time
const byte ledPin = 13;    //using the built in LED

void setup()
{
  Serial.begin(115200);  //start Serial in case we need to print debugging info
  pinMode(ledPin, OUTPUT);
  startMillis = millis();  //initial start time
}

void loop()
{
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  
  if (digitalRead(ledPin)==HIGH && currentMillis - startMillis >= ONtime)  //test whether the ON period has elapsed
  {
    digitalWrite(ledPin, LOW);  //if so, change the state of the LED to LOW.
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }
  else if (currentMillis - startMillis >= OFFtime)  //test whether the OFF period has elapsed
  {
    digitalWrite(ledPin, HIGH);  //if so, change the state of the LED to HIGH. 
    startMillis = currentMillis; 
  }  
  
}

Awesome. works great.

Thanks a lot!