micros()
[Timing]
unsigned long micros();Description
Returns the number of microseconds since the Custom Element was powered up.
Parameters
None.
Returns
The number of microseconds.
Example Code
The code creates a counter that increments the value of
counter every 1000 microseconds (1kHz), and shows the
output using the output pins 1, 2, 3, and 4 (4 bits).
void setup() {
pinLabel(1, "O1"); // sets the pin 1 label to "O1"
pinMode(1, OUTPUT); // sets the digital pin 1 as output
pinLabel(2, "O2"); // sets the pin 2 label to "O2"
pinMode(2, OUTPUT); // sets the digital pin 2 as output
pinLabel(3, "O3"); // sets the pin 3 label to "O3"
pinMode(3, OUTPUT); // sets the digital pin 3 as output
pinLabel(4, "O4"); // sets the pin 4 label to "O4"
pinMode(4, OUTPUT); // sets the digital pin 4 as output
}
void loop() {
static unsigned long lastTime = 0;
static unsigned long counter = 0;
if (micros() - lastTime >= 1000) { // checks if the number of microseconds is greater than or equal to 1000
lastTime = micros();
counter++;
digitalWrite(1, counter & 0x01); // sets the output pin 1 with the first bit of the counter
digitalWrite(2, counter & 0x02); // sets the output pin 2 with the second bit of the counter
digitalWrite(3, counter & 0x04); // sets the output pin 3 with the third bit of the counter
digitalWrite(4, counter & 0x08); // sets the output pin 4 with the fourth bit of the counter
}
}Notes and Warnings
There are 1,000 microseconds in a millisecond and 1,000,000 microseconds in a second.
