How to do timer in 0.5sec HIGH and 9.5sec Low?

I can do timer like below

const long interval = 10000;  

unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

   
Serial.println("renewal");
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }

it mean, it trigger LOW and HIGH for every 10sec.
but How can I manipulate to 0.5sec HIGH and rest of time LOW?

Partial code gives partial answer :wink:

But just use two intervals, one of 500ms and a second of 9500ms and use the correct one. Easy when you use an array.

When an interval ends you need to change the value in interval to the number required for the next interval. Something crudely like

if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    if (interval == 500) {
       interval = 9500;
    }
    else {
      interval = 500;
    }
   // etc

...R

Another way to skin this cat

const byte ledPin =  13;
unsigned long previousMillis = 0;
const byte states[] = {HIGH, LOW};
const unsigned long intervals[] = {9500, 500};
const byte NO_OF_INTERVALS = sizeof(intervals) / sizeof(intervals[0]);
byte state;

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= intervals[state % NO_OF_INTERVALS])
  {
    previousMillis = currentMillis;
    digitalWrite(ledPin, states[(state++) % 2]);
  }
}

Another way to El Gato's pelt: :slight_smile:

// To set ON/OFF times, type in top of serial monitor:
// Example: 500 mS ON, 2000 mS OFF (lower case letters, no spaces)
// o500[ENTER], f2000[ENTER] or o500f2000[ENTER]


unsigned long iStart; // interval start
unsigned long onTime = 1000;
unsigned long offTime = 9000;
const byte ledPin = 13; // led pin
void setup()
{
  Serial.begin(9600); //set serial monitor to match
  pinMode(ledPin,OUTPUT);
  prntIt();
}
void loop()
{
  unsigned long iEnd = onTime + offTime;
  digitalWrite(ledPin,millis() - iStart < onTime);
  if(millis() - iStart > iEnd)
    iStart += iEnd;
  while (Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == 'o')
      onTime = Serial.parseInt();
    else if(inChar == 'f')
      offTime = Serial.parseInt();
    else
    {  
       Serial.println("invalid input");
       while(Serial.available() > 0)
         Serial.read();
    }
    iStart = millis();
    prntIt();
  }  
}
void prntIt()
{
  Serial.print("On Time = ");
  Serial.print(onTime);
  Serial.print("  Off Time = ");
  Serial.print(offTime);
  Serial.println("\n");
}

You can use milis() or a timer.

For an example of timer using a library (you can look inside and take your needed code), using GitHub - Naguissa/uTimerLib: Arduino tiny and cross-device compatible timer library

#include "uTimerLib.h"

unsigned char ledControl = 0;
// LED is LED pin global variable

void blinkLed() {
	digitalWrite(LED, ledControl == 0 ? HIGHT : LOW); // You can decide when to blink the led in the period
	ledControl = (ledControl + 1) % 20; // 0,5sec hight, 9,5sec low is 1/20th
}

void setup() {
	// [..whatever, as set led pin as output]

	TimerLib.setInterval_us(blinkLed, 500000); // 500,000 microseconds, as 20 times in 10 seconds
}

// Rest of your program