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 7361 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…
  • Gough Lui
    Gough Lui over 2 years ago

    Well, in theory, delay(2000) should delay for 2 seconds. As per https://www.arduino.cc/reference/en/language/functions/time/delay/

    However, there are some potential caveats - for example:

    What board are you using? (ESP8266's for example, don't like to be kept waiting and will crash out by watchdog timer)
    Are you using the right "core" settings in Arduino for the board? (if the clock crystal on the board is not matching the expected clock rate, delays could be wrong.)
    Are you doing something else with the timer peripherals on the microcontroller that could interfere with the Arduino code?

    Try this - what does a simple LED blinky sketch with delay do? Does it wait the expected amount of time? Or alternatively code your delay by checking millis() - does that work or not?

    - Gough

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • 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
  • robogary
    robogary over 2 years ago

    Im a sw fumbler myself, Ill throw out a couple suggestions. variable relayDelay() wont be recognized as a Arduino delay command. The other delay(300000) should be too big a value, even tho it works. The delay value is an integer, 32767 max (or maybe 65535 if unsigned). There are some online articles that talk about some Arduino commands that interfere with the delay timer time accumulator in background. 

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • baldengineer
    baldengineer over 2 years ago in reply to robogary

    The standard Arduino library defines the millisecond value as a (32-bit) unsigned long. So you can use values considerably larger than 32767/65535.

    The issue people tend to run into is when they multiply two integers. For example, delay(3000 * 1000); Since both values fit inside of a 16-bit data value, the complier treats the operation entirely as 16-bit even though the result overflows. All of that happens despite the (correctly) result fitting inside of a 32-bit value which would work if explicitly written. (or if the math is casted.)

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 2 years ago in reply to ntewinkel

    This. Maybe use a digital output. Flip polarity before the delay. Flip polarity after delay. Use an oscilloscope to measure the time.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • colporteur
    colporteur over 2 years ago in reply to Gough Lui

    The board is a Geekcret Arduino Mega 2560 (on chip) knock-off.

    I'm not manipulating any timers.

    The program reads a set of eight IR sensors divided across two tracks. Four sensors on one track four sensors on the second track. The four sensors per track are divided down into pairs. One pair is departure and the other pair is arrival. The exercise is to read a model train motion as it passes over the sensors. A sensor reads 0, when the train is over it. The sequence: stop (0000), departure(0011), in transit(1111), arrival (1100) and stop (0000) as the train moves from right to left over the sensors.

    I tried the exercise of confirming the timing of delay. If saying one-steamboat is a second (old touch football trick) the Blink sketch from examples flashes on & off for the number of seconds set up in delay().

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

    I didn't explain myself well

    relayDelay() is  void function i created

    void relayDelay() {
      time_now = millis();
      while (millis() < time_now + turnout_delay) {
        //wait approx. [period] ms
      }
    }

    that I could use to test what amount of delay I would need in order to activate a relay so the contacts make. Currently turnout_delay = 10000 and I get about 1 second the relay is engaged. Now this throws a wrench in my understanding of delay() but the time duration works.

    Any reference links on Arduino commands that impact delay timer?

    • 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
  • Gough Lui
    Gough Lui over 2 years ago in reply to colporteur

    What is the type of time_now variable? What is the type of turnout_delay? Are you sure you have enough free RAM?

    If you're not using unsigned long, then you are probably running into overflow issues as millis() reports unsigned long and if working as other data type, will not handle roll-over correctly.

    - Gough

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Gough Lui
    Gough Lui over 2 years ago in reply to colporteur

    Good. This means your board set-up is fine and the problem is most likely in your code ... check data types as I had noted in my other reply.

    - Gough

    • 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