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
  • 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 405 subscribers
  • Views 6058 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

Former Member
Former Member over 12 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 12 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 12 years ago in reply to Former Member +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…
  • Former Member
    Former Member over 12 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…
Parents
  • phoenixcomm
    phoenixcomm over 12 years ago

    lots of ideas...

    but:

    1. you don't want to write the code twice
    2. and using the % function is ok but it is only clouding the issues

     

    you never said how long you wanted to make the led blink.

    blinking, or oscillation is a repetitive process of turning something on and then off.

    so why not write two loops one count the delay the other controls the state of the led

    FINS

    enjoy

    Cris H.image

     

    #define OFF 0

    #define ON 1

    #define FOREVER for(;;)

    long delay 3000; //msecs

    // set the digital pin as output:

    pinMode(ledPin, OUTPUT);

     

    // we are going to start in the ON state

    int led = ON;

    long millis = 0;

     

    FOREVER {

      

         for( ; millis < delay; millis++ );                              //  this  is the delay loop

              millis = 0;                                      

    // following could have been done differantly by using the =! operator and changing a var name

         if( led == ON ) {                                                  //  switch logic

              led = OFF;

              int LedPin = LOW;}

         else if( led == OFF ){

              led = ON;

               LedPin = HIGH;}

    //  LedPin =! LedPin; // no if() no else if()..

         digitalWrite(ledPin,LedPin);}       // write to the led

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • billabott
    billabott over 12 years ago in reply to phoenixcomm

     

    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.  It has been tested and runs on UNO using IDE 0023.


    #define OFF 0

     

    #define ON 1

     

    unsigned long int timecheck;

    unsigned long int mydelay = 3000;  //  3000 msecs = 3 secs

    int ledPin =13;   //  onboard led blinking with a 50 % duty cycle @ 0.166 Hz

                              //  tells user that the program is running normally

     

    int Led = ON;  //  start in the ON state

     

    void setup()

    {

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

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

       timecheck = millis() + mydelay;       // initialize timecheck 

    }   // putes

     

     

    void loop()

    {

       if (millis() > timecheck) {      // have 3 seconds gone by?

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

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

             timecheck = millis() + mydelay;     // update timecheck   

             }  // fi

    // else {

            ;     // other tasks needing to be done between led transistions are placed here

            ;     //  could use a counter to limit number of iterations thru this code subblock

            ;     //  so process returns in time to catch the above timecheck condition.

    //  }  //  esle

     

    }  // pool

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • phoenixcomm
    phoenixcomm over 12 years ago in reply to billabott

    bill, I was not writing a Duino application. I don't like to spoon feed people.. I just wanted to show how to do the programing logic. one comment about yours thou, is the you created a function mills in the 8 bit arena that is a call (push) and a return (pop) which with limited memory and a slow cpu I would not have done.

    one other thing... I do not like the 'Duino IDE at all. It hides a lot of things. Like the real interrupt structure ( The Select Statement ) and you loose the 30 or 40 years of Leverage with the C library. I use for the Duinos the AVR - Eclipse tools, and I use Eclipse for all my other work as well.

     

    BTW I have been writing with C for few decades at least.

     

    Cris H.

     

    UPDATE I really like your  use of the =! operator.. no muss no fuss. But I didn't know if a nubie would get it so i did it the long way with the two if()s.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • R_Phoenix
    R_Phoenix over 12 years ago in reply to billabott

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

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • R_Phoenix
    R_Phoenix over 12 years ago in reply to billabott

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

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Children
  • billabott
    billabott over 12 years ago in reply to R_Phoenix

    R_Phoenix wrote:

     

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

    The sketch I wrote does basicly the same thing but with my personal coding style and flavor.  It is the best I can possibly do.  I like the official arduino tutorial examples because they comment the code extensively.  I believe that my code comments also provide a reasonable justification to adopt it as a basic structure for all sketches.  Who doesn't benefit from a visual indication that the program is alive and doing the tasks assigned to it?  One thing I personally dislike are complicated condition tests,  I try to follow the KISS design philosophy.   

    • 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 © 2025 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.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube