micros()
Description
Renvoie le nombre de microsecondes depuis que la carte Arduino a démarré le programme en cours. Ce nombre déborde (repasse à 0) après approximativement 70 minutes. Sur les cartes Arduino à 16Mhz (par exemple le Duelmilanove et la Nano), cette fonction a une résolution de 4 microsecondes (ce qui veut dire que la valeur retournée est toujours un multiple de quatre, autrement dit que la fonction compte de quatre en quatre). Sur les cartes Arduino à 8Mhz (par exemple Lilypad), cette fonction a une résolution de 8 microsecondes.
Syntaxe
Paramètres
- Aucun
Valeurs Renvoyées
- Le nombre de microsecondes écoulées depuis que le programme en cours a démarré, sous la forme d'un nombre de type unsigned long.
Exemple
Exemple 1
The result on Serial Monitor:
Exemple 2
Print a text one time per second without blocking other codes
※ Remarque:
- Il y a 1000 microseconds dans une milliseconde et 1 000 000 (1 million!) de microsecondes dans une seconde.
- Please note that the return value for micros() is of type unsigned long, logic errors may occur if a programmer tries to do arithmetic with smaller data types such as int. Even signed long may encounter errors as its maximum value is half that of its unsigned counterpart.
- The return value of micros() function rolls over back to zero after roughly 71 minutes. If the sketch is intended to run for longer than that, It needs to make sure the rollover does not make the sketch fail. To solve it, write rollover-safe code. Let's compare the two following inequations:
- micros() >= (previousMicros + TIME_INTERVAL)
- (micros() - previousMicros) >= TIME_INTERVAL.
- Mathematically, they are equivalent to each other. However, in programming, they are not. That is because the size of storage is unlimited in mathematics while it is limited to 4 bytes in Arduino programming. In programming, when the rollover happens, the first inequation makes the sketch fail while the second inequation does NOT. Therefore:
- Do NOT use if (micros() >= (previousMicros + TIME_INTERVAL)),
- Use if(micros() - previousMicros >= TIME_INTERVAL) instead
※ ARDUINO BUY RECOMMENDATION
Arduino UNO R3 | |
Arduino Starter Kit |
Additionally, some links direct to products from our own brand, DIYables.