element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • 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 Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • 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
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • 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 Delay vs Timer
  • 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 15 replies
  • Subscribers 388 subscribers
  • Views 2753 views
  • Users 0 members are here
  • code
  • arduino
Related

Delay vs Timer

WumblingBorkeys
WumblingBorkeys over 3 years ago

I'm making a laser maze for an event, and I've been trying to work this out in my head...

void section1()
{
  delay(100);// delay to make sure it's a real trigger
  laserstate1 = digitalRead(lasersens1);
  if(laserstate1 == 1)// if it is a trigger, if it isn't make sure the lights are off and go back to the loop
  {
  leds[0] = CRGB (255,255,255);
  leds[1] = CRGB (255,255,255);
  leds[2] = CRGB (255,255,255);//Trigger lights to illuminate those who tripped it
  leds[3] = CRGB (255,255,255);
  leds[4] = CRGB (255,255,255);
  FastLED.show();
  
    //resetButtonState = digitalRead(resetButton);
    while(digitalRead(resetButton) == 0 ||  digitalRead(lasersens1) == 1) {//require the person to leave then press a button to reset the lights
    //resetButtonState = digitalRead(resetButton);

    }

  }
  
  leds[0] = CRGB (0,0,0);
  leds[1] = CRGB (0,0,0);
  leds[2] = CRGB (0,0,0);
  leds[3] = CRGB (0,0,0);
  leds[4] = CRGB (0,0,0);
  FastLED.show(); 

}

void loop() {
    while (1)
    {
        laserstate1 = digitalRead(lasersens1);//Wait for a laser to be blocked
        if(laserstate1 == 1)
            {
            section1(); //Trigger the section sequence
            }
  
        }
  
    }

There would be more lasers, hoping to be in the neighborhood of 10-20

My question is would there be an advantage to running a timer instead of the delay in "section1"?

I know there theoretically there would be but when it's only 100ms it doesn't feel necessary.

It should be noted that each lasersens wouldn't trip its own section but rather a group of "lasersens" would make a section, and each trigger a singular set of lights.

The only other thing would be an addition of a kill switch they could find and press to deactivate the system. for x amount of time.

  • Sign in to reply
  • Cancel

Top Replies

  • fmilburn
    fmilburn over 3 years ago +7
    Hi WumblingBorkeys , In general it isn't considered good practice to use delays like this and a non-blocking timer is better. The delay function can work if the application is simple enough but might…
  • fmilburn
    fmilburn over 3 years ago in reply to WumblingBorkeys +6
    I suspect it is because the variable section1Timer is "int" while the millis() function is "unsigned long". The variable section1Timer is probably rolling over after 30 seconds or so while millis() keeps…
  • fmilburn
    fmilburn over 3 years ago in reply to shabaz +4
    Hi Shabaz, I agree it isn't that clean. Timer callbacks aren't listed in the basic Arduino reference documentation that I could see but a search turns up libraries presumably written by others such as…
Parents
  • fmilburn
    fmilburn over 3 years ago

    Hi WumblingBorkeys,

    In general it isn't considered good practice to use delays like this and a non-blocking timer is better.  The delay function can work if the application is simple enough but might as well do it right, especially if there more code and activity to be added.  A couple of observations:

    The while(1) is not necessary to set up an infinite loop with Arduino.  The void loop() above it is already doing that.

    void loop() {
        while (1)

    Also, take care waiting in the function "void section1()" using the while statement for the player to reset the trigger.  If timers aren't being used this would also be blocking code.  Accordingly, if there is more than one player, they could advance without setting off laser switches.

    • Cancel
    • Vote Up +7 Vote Down
    • Sign in to reply
    • Cancel
  • WumblingBorkeys
    WumblingBorkeys over 3 years ago in reply to fmilburn

    Thanks, as you can tell it's been a minute since I've programmed anything useful...

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • WumblingBorkeys
    WumblingBorkeys over 3 years ago in reply to fmilburn

    Thanks, as you can tell it's been a minute since I've programmed anything useful...

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Children
  • fmilburn
    fmilburn over 3 years ago in reply to WumblingBorkeys

    You are welcome.  I'm sure there would be interest if you post the outcome of your project when you are finished.  Feel free to post a new question in the forum if needed.

    • Cancel
    • Vote Up +2 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