Become a leader in the IoT community!

Join our community of embedded and IoT practitioners to contribute experience, learn new skills and collaborate with other developers with complementary skillsets.

Step 1 of 5

CREATE YOUR PROFILE *Required

OR
Step 2 of 5

WHAT BRINGS YOU TO DEVHEADS? *Choose 1 or more

Connect & collaborate 🤝with other tech professionals
Learn & Grow 📚
Contribute Experience & Expertise 🔧
Step 3 of 5

WHAT'S YOUR INTEREST OR EXPERTISE? *Choose 1 or more

Hardware Design 💡
Embedded Software 💻
Edge Networking
Step 4 of 5

Personalize your profile

Step 5 of 5

Read & agree to our COMMUNITY RULES

  1. We want this server to be a welcoming space! Treat everyone with respect. Absolutely no harassment, witch hunting, sexism, racism, or hate speech will be tolerated.
  2. If you see something against the rules or something that makes you feel unsafe, let staff know by messaging @admin in the "support-tickets" tab in the Live DevChat menu.
  3. No age-restricted, obscene or NSFW content. This includes text, images, or links featuring nudity, sex, hard violence, or other graphically disturbing content.
  4. No spam. This includes DMing fellow members.
  5. You must be over the age of 18 years old to participate in our community.
  6. You agree to our Terms of Service (https://www.devheads.io/terms-of-service/) and Privacy Policy (https://www.devheads.io/privacy-policy)
By clicking "Finish", you have read and agreed to the our Terms of Service and Privacy Policy.

How to Execute a Timed LED On/Off Sequence Using millis() Function

Hello ,I’m struggling with a problem. I want to execute a sequence of turning an LED on and off, but not just a simple on/off; I want a timed sequence that turns the LED on and off at specific intervals. My requirement is to use `millis()` because other actions need to run in parallel.

Here’s what I’ve done:

(currentMillis - timer2 >= 1000 && currentMillis - timer2 < 1100) ? digitalWrite(motor, LOW) : digitalWrite(motor, HIGH);
(currentMillis - timer2 >= 1100 && currentMillis - timer2 < 1500) ? digitalWrite(motor, HIGH) : digitalWrite(motor, LOW);
(currentMillis - timer2 >= 1500 && currentMillis - timer2 < 2000) ? digitalWrite(motor, LOW) : digitalWrite(motor, HIGH);
(currentMillis - timer2 >= 2000 && currentMillis - timer2 < 3000) ? digitalWrite(motor, HIGH) : digitalWrite(motor, LOW);
(currentMillis - timer2 >= 3000 && currentMillis - timer2 < 5000) ? digitalWrite(motor, LOW) : digitalWrite(motor, HIGH);

But this isn’t working as expected. Sequentially, it should look like:
- from 0 to 1000 ms = off
- from 1000 to 1100 ms = on
- from 1100 to 1200 ms = off
- from 1200 to 1500 ms = on
- etc.
Where am I going wrong?🤔

  1. Enthernet Code#0000

    @camila_9900 Hello I see what you’re trying to achieve, and it’s great that you’re using `millis()` for non-blocking timing to allow other actions to run in parallel. However, there seems to be a logic issue in your code with the intervals and the use of the ternary operators. The conditions are overlapping and might be causing unexpected behavior.

    Try this out

    unsigned long currentMillis = millis();  // Get the current time
    unsigned long interval = currentMillis - timer2; // Calculate the time elapsed
    
    if (interval < 1000) {
        digitalWrite(motor, LOW);  // 0 - 1000 ms: off
    } else if (interval < 1100) {
        digitalWrite(motor, HIGH); // 1000 - 1100 ms: on
    } else if (interval < 1200) {
        digitalWrite(motor, LOW);  // 1100 - 1200 ms: off
    } else if (interval < 1500) {
        digitalWrite(motor, HIGH); // 1200 - 1500 ms: on
    } else if (interval < 1600) {
        digitalWrite(motor, LOW);  // 1500 - 1600 ms: off
    } else if (interval < 2000) {
        digitalWrite(motor, HIGH); // 1600 - 2000 ms: on
    } else {
        timer2 = millis(); // Reset timer2 when the sequence ends (>= 2000 ms)
    }
    

CONTRIBUTE TO THIS THREAD

Browse other Product Reviews tagged 

Leaderboard

RANKED BY XP

All time
  • 1.
    Avatar
    @Nayel115
    1620 XP
  • 2.
    Avatar
    @UcGee
    650 XP
  • 3.
    Avatar
    @melta101
    600 XP
  • 4.
    Avatar
    @chitour
    600 XP
  • 5.
    Avatar
    @lifegochi
    250 XP
  • 6.
    Avatar
    @Youuce
    180 XP