How to make a duty cycle using 2 LEDs using arduino?

I want to make a duty cycle of 2 LEDs using Arduino in tinkercad. I have 2 LEDs 1 is red and another is green. I want both the LEDs on at the same time but a glowing delay of red led is 1second and green led is 1.3second just like the graph below and having both duty cycle 50%.
automaticchina
IMAGE (NEW USER CAN ONLY ATTACH ONLY 1 MEDIA)

but I cant be able to do that I had tried with 2 blocks of if-else but it doesn't work due to its take if-else synchronously then I tried to calculate the graph and want to put it as a delay but it is not an easy solution

I've learned about millis() is the solution but how would I use it? Please help me to solve this problem

Another Media IMAGE 1

To me "duty cycle" means "the ratio of on time to off time".

What does it mean to you?

I'm getting bored of typing "post code, not pictures of code"

1 Like

Looks like you need a modified version of the blink without delay sketch.
let me see...

posting code as a code-section means the effort of having your code inside the arduino-IDE is two steps small taking 15 seconds:

-1. Click Copy-Button of the code-section

    1. press Ctrl-V into arduino-IDE
      done

compare this to downloading a picture starting two write down your code new

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

Your leds have two different frequencies = two different times to be on/off. These are timeperiods

Your code uses two different timers to measure
how much time has passed by since last timeperiod

first timer measures "has 1 seconds passed by?"

second second timer measures "has 1,5 seconds passed by?"

Each time the timeperiod is over invert the state of the LED

from ON to OFF
or
from OFF to ON

that creates the signal-pattern that the picture shows

measuring time-differences on a microcontroller is done with the same principle as you look onto your watch:

How much time has passed by since midnight.

A microcontroller doesn't have a "standard-watch 23:59
it has a milliseconds counter counting up from 0 to 4.294.967.296

if you calculate timedifferencies the absolute value is irrelevant
example

24 milliseconds - 3 milliseconds = 21 milliseconds

1024 milliseconds - 1003 millisecocds = 21 milliseconds

9024 milliseconds - 9003 milliseconds = 21 milliseconds

1.000.009.024 milliseconds - 1.000.009003 milliseconds = 21 milliseconds

This is valid for any time-difference

calculating a time-difference is what this function is doing

boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();  
  if ( currentMillis - periodStartTime >= TimePeriod )
  {
    periodStartTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  } 
  else return false;            // not expired
}

you define a timer-variable which must be of type unsigned long!!

unsigned long MyTimer1 = 0;                   // variables MUST be of type unsigned long
unsigned long MyTimer2 = 0;                   // variables MUST be of type unsigned long

here is the complete sketch

boolean TimePeriodIsOver (unsigned long &periodStartTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();  
  if ( currentMillis - periodStartTime >= TimePeriod )
  {
    periodStartTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  } 
  else return false;            // not expired
}

unsigned long MyTimer1 = 0;                   // variables MUST be of type unsigned long
unsigned long MyTimer2 = 0;                   // variables MUST be of type unsigned long

const byte LED1_Pin = 11;
const byte LED2_Pin = 12;

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  pinMode(LED1_Pin, OUTPUT);
  pinMode(LED2_Pin, OUTPUT);
}


void loop() {
  if ( TimePeriodIsOver(MyTimer1,1000) ) {
    digitalWrite( LED1_Pin, !digitalRead(LED1_Pin) );
  }  

  if ( TimePeriodIsOver(MyTimer2,1500) ) {
    digitalWrite( LED2_Pin, !digitalRead(LED2_Pin) );
  }  
}
digitalWrite( LED1_Pin, !digitalRead(LED1_Pin) );

does this

digitalRead(LED1_Pin)

read current state of LED1_Pin. Can be done with an IO-pin configured as output too

The attention-mark "!" means logical NOT

!HIGH = LOW
!LOW = HIGH
which does inverting the current state of the IO-pin

digitalWrite( LED1_Pin,   NEWSTATE

NEWSTATE is the result of !digitalRead(LED1_Pin)

all in all inverst the state of the IO-pin ON-OFF-ON-OFF....

1 Like

And here is a copy-paste of the blink without delay sketch, only added a 2nd led with different interval period
I have tested it with the built in led buth not with 2 leds at the same time. It seems to work just fine.

/*
  Blink without Delay

  Turns on and off a light emitting diode (LED) connected to a digital pin,
  without using the delay() function. This means that other code can run at the
  same time without being interrupted by the LED code.

  The circuit:
  - Use the onboard LED.
  - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
    and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
    is set to the correct LED pin independent of which board is used.
    If you want to know what pin the on-board LED is connected to on your
    Arduino model, check the Technical Specs of your board at:
    https://www.arduino.cc/en/Main/Products

  created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/

// constants won't change. Used here to set a pin number:
const int ledPin =  12; // the number of the LED pin
const int ledPinB =  11;

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
int ledStateB = LOW;

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated
unsigned long previousMillisB = 0;

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)
const long intervalB = 1300;

void setup() 
{
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  pinMode(ledPinB, OUTPUT);
}

void loop() 
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

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

    // 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);
  }

  unsigned long currentMillisB = millis();

  if (currentMillisB - previousMillisB >= intervalB) 
  {
    // save the last time you blinked the LED
    previousMillisB = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledStateB == LOW) 
    {
      ledStateB = HIGH;
    } else 
    {
      ledStateB = LOW;
    }

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