Arduino mills() function
This tutorial will discuss millis()
the use of functions in different applications in Arduino. This tutorial will also discuss some examples to better understand millis()
the functions.
millis()
Checking the elapsed time in Arduino using the
millis()
The function returns unsigned long
an unsigned variable of type that contains the number of milliseconds that have passed since the Arduino board started running the code. Since the variable returned is of type unsigned long
, the number will overflow and reset to zero after 49 days.
unsigned long currentTime;
void setup() { Serial.begin(19200); }
void loop() {
Serial.print("Time Stamp: ");
currentTime = millis();
Serial.println(currentTime);
delay(1000);
}
In the above code, currentTime
is unsigned long
a variable of type which is used to store the time. Check out this link for millis()
more information about the function.
millis()
Blinking LED in Arduino using function
In this example, we will millis()
blink an LED using the delay() function. Consider that you have to blink the LED for a specific time, say 1 second. In this case, you can use millis()
the delay() function to blink the LED for a specific time. If you use the delay() function to blink the LED, it will also pause your code until the delay time is over. So, millis()
instead of using the delay() function delay()
, you can use the delay() function and it will not pause your code.
unsigned long startTime;
unsigned long currentTime;
const unsigned long period = 1000;
const byte ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
startTime = millis(); // initial start time
}
void loop() {
currentTime = millis();
if (currentTime - startTime >= period) // test whether the period has elapsed
{
digitalWrite(ledPin,
!digitalRead(ledPin)); // if so, change the state of the LED.
startTime = currentTime;
}
}
In this example, the LED will turn on and off for exactly one second. You can change the blinking period by changing the value of the variable period in the code above.
millis()
Change the brightness of an LED in Arduino using the
In this example, we will millis()
change the brightness of an LED using the function. We will set a period during which the brightness of the LED will increase. To do this, we need to connect the LED with a PWM pin of the Arduino. We can write the brightness to the LED using the analogWrite() function. We can analogWrite
change the brightness of the LED between 0 and 255 using the function.
unsigned long startTime;
unsigned long currentTime;
const unsigned long period = 10;
const byte ledPin = 10; // using an LED on a PWM pin.
byte LedBrightness = 0; // initial brightness
byte LedIncrement = 1; // amount to change the brightness
void setup() {
pinMode(ledPin, OUTPUT);
startTime = millis(); // initial start time
}
void loop() {
currentTime = millis();
if (currentTime - startTime >= period) {
analogWrite(ledPin, LedBrightness); // set the brightness
LedBrightness += LedIncrement;
startTime = currentTime;
}
}
In this example, the LED brightness will increase every 10 milliseconds. You can change the increment time by changing the variable period in the code above.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Arduino mills() 函数
Publish Date:2024/03/13 Views:296 Category:C++
-
可以使用 millis()函数检查自 Arduino 开发板开始运行代码以来经过的时间。
在C中将整数转换为字符
Publish Date:2024/01/03 Views:135 Category:C语言
-
本教程介绍了在C中将整数转换为字符的不同方法。在C编程语言中,将整数转换为字符在各种情况下都很重要。在C中,字符是以ASCII值表示的,因此转换过程相对简单。
如何在 C++ 中实现毫秒级的睡眠
Publish Date:2024/01/02 Views:238 Category:C++
-
本文介绍了在 C++ 中使用不同方法暂停程序执行,实现睡眠的方法。本文介绍了在 C++ 中睡眠毫秒的方法。使用 std::this_thread::sleep_for 方法在 C++ 中睡眠
如何在 C++ 中将双精度数四舍五入到整数上
Publish Date:2024/01/02 Views:156 Category:C++
-
本文演示了如何在 C++ 中把双精度数四舍五入到整数中。本文将为大家讲解几种在 C++ 中如何将双精度数四舍五入为整数的方法。使用 round() 函数将双精度数四舍五入到整数
如何在 C++ 中以毫秒为单位获取时间
Publish Date:2024/01/02 Views:149 Category:C++
-
本文介绍了如何在 C++ 中获取以毫秒为单位的时间。本文将介绍多种 C++ 方法,介绍如何以毫秒为单位获取时间。
如何在 C++ 中把 Char 数组转换为 Int
Publish Date:2024/01/02 Views:143 Category:C++
-
本文演示了在 C++ 中把 char 数组转换为 int 类型的方法。本文将介绍将 char 数组转换为 int 类型的 C++ 方法。使用 std::strtol 函数将 char 数组转换为 int 类型
如何在 C++ 中将 ASCII 码转换为字符
Publish Date:2024/01/02 Views:526 Category:C++
-
本文介绍了在 C++ 中如何将 ASCII 值转换为 char 的方法。本文将演示关于如何在 C++ 中把 ASCII 值转换为字符的多种方法。在 C++ 中使用赋值运算符将 ASCII 值转换为字符
如何在 C++ 中把十进制转换为二进制
Publish Date:2024/01/02 Views:239 Category:C++
-
本文介绍如何在 C++ 中把十进制数转换成二进制数。本文将介绍几种在 C++ 中如何将十进制数转换为二进制表示的方法。在 C++ 中使用自定义定义的函数将十进制数转换为二进制数
如何在 C++ 中把枚举型转换为字符串
Publish Date:2024/01/02 Views:296 Category:C++
-
本文演示了如何在 C++ 中把枚举转换为字符串。本文将解释几种在 C++ 中把枚举类型转换为 string 变量的方法。使用 const char*数组将枚举类型转换为字符串