Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:programming_fundamentals_rtu:timing [2018/01/31 11:52] Agrisniken:iot-open:programming_fundamentals_rtu:timing [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== Timing ====== ====== Timing ======
-There are two functions for the use of timing in the code of Arduino - //delay()// and //millis()//.+There are two functions for the use of timing in the code of Arduino - //delay()// and //millis()//. Although they might look similar, their functionality is different and should be used in different cases.
  
 ===== delay() ===== ===== delay() =====
Line 10: Line 10:
 </code> </code>
  
-The parameter //ms// is the number of milliseconds to pause.+The parameter //ms// is the number of milliseconds to pause. The type should be //unsigned long//. 
 +The function returns nothing.
  
 The example code: The example code:
 <code c> <code c>
-int ledPin = 13;                 // LED connected to digital pin 13+int ledPin = 13;                // LED connected to digital pin 13
  
 void setup() void setup()
Line 23: Line 24:
 void loop() void loop()
 { {
-  digitalWrite(ledPin, HIGH);   // sets the LED on +  digitalWrite(ledPin, HIGH);   //LED is on 
-  delay(1000);                  // waits for a second +  delay(1000);                  //pause for 1000 milliseconds 
-  digitalWrite(ledPin, LOW);    // sets the LED off +  digitalWrite(ledPin, LOW);    //LED is off 
-  delay(1000);                  // waits for a second+  delay(1000);                  //pause for 1000 milliseconds
 } }
 </code> </code>
Line 33: Line 34:
  
 ===== millis() ===== ===== millis() =====
-The //millis()// function works a bit differently, because it returns the amount if milliseconds that have passed since the program was started. millis() function is often used to determine the interval of time and to check if it has passed. One of the benefits of using millis() is that the program is not paused until the time interval will pass and any other functions can still execute. The second benefit is better accuracy in timing than using Delay() function.+The //millis()// timing function works a bit differently, because it returns the amount if milliseconds that have passed since the program was started. Millis() function is often used to determine the interval of time and to check if it has passed. One of the benefits of using millis() is that the program is not paused until the time interval will pass and any other functions can still execute. The second benefit is better accuracy in timing than using the delay() function.
  
-This function also has the version of using microseconds instead of milliseconds - //micros()//+The syntax of the function is following: 
 +<code c> 
 +time = millis() 
 +</code> 
 + 
 +This function does not have the input parameters in brackets. 
 +The return value of the function is the number of milliseconds that has passed since the program was started. The type is //unsigned long//. 
 + 
 +The example of the function in the code: 
 +<code c> 
 +int period = 1000;          //the interval 
 +unsigned long time_now = 0; //variable that stores the last timing value 
 +  
 +void setup() { 
 +    Serial.begin(9600); 
 +
 +  
 +void loop() { 
 +    if(millis() > time_now + period){ //testing, if the time that has passed exceeds 1000 milliseconds (1 second) 
 +        time_now = millis();          //the newest time value is stored in the time_now variable 
 +        Serial.println("1 second");   //Print out the text 
 +    } 
 +    
 +    //Some other code that executes each time the loop is run 
 +
 +</code> 
 + 
 +This function also has the version of using microseconds instead of milliseconds - //micros()//. The purpose of this is improving the precision by enhancing resolution.
  
  
en/iot-open/programming_fundamentals_rtu/timing.1517399530.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0