element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum trying to get an LED to come on for 3 seconds and come off without delay
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 29 replies
  • Subscribers 420 subscribers
  • Views 7444 views
  • Users 0 members are here
  • arduino_code
Related

trying to get an LED to come on for 3 seconds and come off without delay

e14 Contributor
e14 Contributor over 13 years ago

hey guys i'm just trying to get an led to come on for 3 seconds and then comeoff, without using delay, i'm not understanding the millis function but i've tried a bit. kindly let me know what i'm doing wrong. thank you.

 

int count = 0;
long previousMillis = 0;        




long interval = 3000;           


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


void loop()
{
  
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    
    previousMillis = currentMillis;   


     if (count <=1)
      digitalWrite(ledPin,HIGH);
    else
      digitalWrite(ledPin,LOW);
count ++;
    
    
  }
}

  • Sign in to reply
  • Cancel

Top Replies

  • billabott
    billabott over 13 years ago in reply to phoenixcomm +2
    Interesting insight Cris H. Unfortunately, your code does not plug into the 'Duino IDE too well. So, I took some time to create my own completely commented version which I now make available to everyone…
  • ntewinkel
    ntewinkel over 13 years ago in reply to e14 Contributor +1
    To have red and green alternating on, you basically copy the lines of code that turn the ledpin high and low and set opposite values for the other LED. ie, when you turn the red led on, at that time you…
  • e14 Contributor
    e14 Contributor over 13 years ago +1
    Thank you Navin and Nico I will definately try out the two versions! You guys are awesome and I will give you 50 cool points each! Seriously though, I just wish I could one day be as helpful as you two…
  • billabott
    billabott over 13 years ago in reply to mcb1

    @Mark

    Thanks for improving the mouse trap.  I think your suggestion is important for projects that are going to run for many weeks without interruption and I will adopt this method.  Thanks again.  Let us pray for the swift recovery of New Zealand cricketer Jesse Ryder.


    Just to be sure I have understood 100%, it should go like this:

     

    void setup()

    {

       pinMode(ledPin, OUTPUT);     // config pin for output, duh  image   

       digitalWrite(ledPin,Led);      // initialize state of output pin

       timecheck = millis();       // initialize timecheck

    }   // putes

     

     

    void loop()

    {

       if (millis() - timecheck >= mydelay) {      // have at least 3 seconds gone by

                                                                          // or has timer rolled over?

             Led = !Led;      // complement the value  ON or OFF

             digitalWrite(ledPin,Led);  //  update state of output pin

             timecheck = millis();     // update timecheck  

             }  // fi         

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • mcb1
    mcb1 over 13 years ago in reply to billabott

    Bill

    Yep thats pretty much it.

     

    Navin wanted it to be on for 3 secs and not toggle, but I think he has enough info now to make it work.

     

    @lahiboi671

    For the red even, green odd there are multiple ways to achieve it.

    You can simply store the last colour, and change, it when you turn the colour ON.

    You toggle a true/false, or a 0/1 and use it to set which colour led to trun ON.

     

    Good luck.

    Mark.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • e14 Contributor
    e14 Contributor over 13 years ago in reply to mcb1

    Thanks Mr, Beckett! I will try the true/false toggle method. Hopefully I can get the hang of writing in C, soo much to learn so little time!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • mcb1
    mcb1 over 13 years ago in reply to e14 Contributor

    Lahiboi671

    Arduino uses c++, but can use C as as well.

    The reference in the IDe gives you the commands available, even if the examples are not the nicest for beginners.

     

    I found the Arduino Cookbook was written from a 'I have this problem, what can I use' style which makes it easier on beginners.

     

    Hop onto the arduino.cc forum and have a look at some of the examples.

    There is a good search function with lots of code floating around.

     

    I've found you learn much more by trying, rather than having someone do the work for you.

    It wasn't long ago that this was foreign to me, so you'll be surprised at how quick you can pick it up.

     

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • e14 Contributor
    e14 Contributor over 13 years ago in reply to mcb1

    Thanks Mark!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • zseqsc
    zseqsc over 13 years ago

    Can't you use some capacitor for that?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • mcb1
    mcb1 over 13 years ago in reply to zseqsc

    Emir Avcı wrote:

     

    Can't you use some capacitor for that?

    For what exactly.?

     

    You can use analogue circuitry to control leds, and have reasonably long time periods.

    The reality is if you want to just have a led flash the 555 timer IC is perfect, and reduces the component count to 4 or 5 inc the 555.

     

    I'm not sure this all the Op wants.

    Its a good learning experience and can be saved in his personal library for other projects later.

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • e14 Contributor
    e14 Contributor over 13 years ago in reply to mcb1

    Awesome reccomendations Mr. Beckett! I have been borrowing that book off and on from the library for the past few months, before I even settled on getting an Arduino! image I am liking it especially when I get to throw in a book titled, "Make: Arduino Bots and Gadgets by Tero Karvinen. For some reason it seems to be the best prescription for "newbie-nitess", with decent information to digest and absorb. And as disenchanting as 'C for dummies" by Dan Gookin sounds I found it best to throw away my closed-mindedness and try to learn a bit in that arena. I also found from an instructor, and in the intro pages of the books I have read, that I should go to forums at Arduino.CC something that sounds soo familiar. It has been said that someone, somewhere, at some point in time around the world probably came to the same fork in the road as I have. I am tinkering every chance I get! LOL! So far my budget is a little strapped so I went to goodwill and desoldered a bunch of rc cars and cd players and have a scrounge area in my room for project prototypes. Hopefully, I can get a video or something started on my page as a thank you for everyone who gave constructive input without involving too much of their personal touch to their replies. Best regards and happy hunting!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • e14 Contributor
    e14 Contributor over 13 years ago in reply to phoenixcomm

    Hi Mrs. Harrison. Thank you again for your responses from a couple of weeks ago. Greatly appreciate the no-nonsense approach. I noticed that you are somewhat of an avid aviator, and I had a question regarding the Boeing's Li-Ion battery issue. I worked as an aircraft electrician in the military and was intrigued by the issues they were having. Sadly I live in Washington, but do not work for them. Supposedly, they have made leaps and bounds towards fixing the issue which I watched on the news earlier this evening. I guess their fix was to encase the cells so if there was to be an overheat issue it would isolate it from the other cells and vent smoke overboard. Now I am unlear as to how many batteries they have on board but most I've seen have been a main and JFS/ Aux battery. Now if there was to be in a somewhat out of this world scenario where both batteries overheat and vent smoke overboard, aside from the battery fail light and gauges to illuminate (lets say the lights, circuit breakers/stuck closed, and gauges are all inop), how would they know there is an issue? Would they become just a floating brick? Especially considering the altitude at whcih they might fly at. That might not be an observation tower can notice and transmit if they can't see them. I only ask in case I take a flight, once the grounding has been lifted, want to make sure I don't take the plunge literally and figuratively.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
<
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube