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 When is the function delay() a 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 41 replies
  • Subscribers 389 subscribers
  • Views 7381 views
  • Users 0 members are here
  • delay
  • arduino_code
Related

When is the function delay() a delay?

colporteur
colporteur over 2 years ago

else if (MainDepartureState == LOW && MainArrivalState == LOW) {
        if (startupMain == HIGH) {
          // pulse turnout for Main train
          digitalWrite(Main_Turnout, HIGH);
          relayDelay();//<-----1
          digitalWrite(Main_Turnout, LOW);
          if (deBug == HIGH) {
            Serial.println("The Main Turnout Activated");
          }
          // Engage Motor for Main
          motors.setSpeedB(minSpeedB);  // Start Motor for Main
          motors.forwardB();
          PowerMain = LOW;
          startupMain = LOW;
          delay(300000);//<----2
          if (deBug == HIGH) {
            Serial.println("Motor Delay Complete");
          }

According to what I have read “The delay() function allows you to pause the execution of your Arduino code for a specified period.”

Why are the delays I am using not working or the values are so extreme? How can I best accomplish a delay?

I tried using the delay() function at point <----1 in my code and it failed miserably. I wanted to activate a relay by turning it on, waiting for relay to engage (delay(2000), then turn it off. The delay(2000) two seconds didn’t work. I wrote a little relay routine that accomplish what I wanted but why did delay(2000) not give me two seconds?

I am using delay() function at <----2 in the code but the value doesn’t reflect the actual delay. I needed to generate some wait time for the motor to engage and trigger a sensor. My desire was a three second delay(3000) but the delay(300000) was a value that actually worked. That would be 300 seconds yes/no?

I'm a code resurrectionist and not an actual programmer. I will try and muddle through discussions 

  • Sign in to reply
  • Cancel

Top Replies

  • colporteur
    colporteur over 2 years ago +6
    Based on GL shared insight, I used a different set of PWM pins so as not to muck with the delay() timer. I also took the BE suggestions and used subtraction in the millis routines. I am happy to report…
  • Gough Lui
    Gough Lui over 2 years ago in reply to colporteur +5
    //user input variables int turnout_delay = 10000; //delay for relay contacts to engage int motor_delay = 900000; //delay for motor to move train unsigned long time_now = 0; int type cannot store outside…
  • Gough Lui
    Gough Lui over 2 years ago in reply to colporteur +3
    I think that it helps if one can see names like "TCCR0B" and know inside that it is short for "Timer Control Counter Register" then that should immediately set-off alarm bells in one's head. Gough Lui…
Parents
  • ntewinkel
    ntewinkel over 2 years ago

    I would start by putting a serial.println before and after the delay just to make sure there’s no question of it being the right delay line you’re looking at.

     Also make sure you have selected the correct board for your sketch. There’s a good chance delay() just counts clock cycles, so a faster chip might whip through them faster than a standard old Uno might.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • colporteur
    colporteur over 2 years ago in reply to ntewinkel

    That was my low tech way to confirm the code was actually running.

    I drop these lines of code in to use serial print

        if (deBug == HIGH) {  //tell status
          Serial.println("Mark");
        }

    By setting deBug variable I can make the serail print disappear to reduce any impact to the code operation when in production.

    The delay() is not the three seconds I hoped for when I bookend it with the serial print lines.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • colporteur
    colporteur over 2 years ago in reply to ntewinkel

    That was my low tech way to confirm the code was actually running.

    I drop these lines of code in to use serial print

        if (deBug == HIGH) {  //tell status
          Serial.println("Mark");
        }

    By setting deBug variable I can make the serail print disappear to reduce any impact to the code operation when in production.

    The delay() is not the three seconds I hoped for when I bookend it with the serial print lines.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Children
  • ntewinkel
    ntewinkel over 2 years ago in reply to colporteur

    I don't think you've mentioned yet (unless I missed it) which board you are using?

    The delay() function seems to use SysTick, which I think is (or is related to) the CPU speed, so if in the Arduino IDE something is selected wrong, that might just mess things up.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • ntewinkel
    ntewinkel over 2 years ago in reply to ntewinkel

    Actually, the clock speed/board setting is easily tested by using the Blink example - if that works fine, then you can rule out board related settings.

    ie, if Blink works properly, then it's something in your code. You could then try testing chunks of your code in a separate test app, to narrow down where that issue might be. I often start small and then add back in bits at a time until it breaks again.

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