JIYIK CN >

Current Location:Home > Learning > PROGRAM > C++ >

Stopping a loop in Arduino

Author:JIYIK Last Updated:2025/04/16 Views:

This tutorial will discuss the methods to stop a loop in Arduino. There are two types of loops in Arduino: one is the void loop() provided by default and the other is created by the user in it. The loop created by the user can be ended easily using the break method. To end the loop of Arduino void loop(), you can use the following method.


Use Sleep_n0m1library stopvoid loop()

The method above will probably work on all Arduino boards, but the Arduino will continue to use power. Using Sleep_n0m1the library, you can put the Arduino CPU into a permanent sleep state until you reset it manually or using a timer. Note that this may not work on all Arduino boards.

#include <Sleep_n0m1.h>
Sleep sleep;
unsigned long sleepTime;  // how long you want the Arduino to sleep

void setup() {
  sleepTime = 50000;  // set sleep time in ms, max sleep time is 49.7 days
}

void loop() {
  // Your Code
  sleep.pwrDownMode();          // set sleep mode
  sleep.sleepDelay(sleepTime);  // sleep for: sleepTime
}

Once you have completed the code, make sure to use it Sleep_n0m1 库. This method will only consume a little energy. Use this link for more details.


Use exit(0)Stopvoid loop()

You can use the method after the code exit(0)to end the Arduino void loop(), but please note that Arduino.ccno method is provided to end this loop, so this method may not work for all Arduino development boards.

void loop() {
  // All of your code here

  // exit the loop
  exit(0);  // 0 is required to prevent error.
}

Note that exit(0)after that, your Arduino will stop working until you reset it manually. So make sure to use this method after your code has completed its task.


Stop using infinite loopvoid loop()

The above method may not work on all Arduino boards, so we have to use another method. The infinite loop method will work on all Arduino boards, but the Arduino will remain awake and continue to consume power. In this method, you can insert an infinite loop after your code. The Arduino will process your code, enter an infinite loop and stay there until you manually reset it.

void loop() {
  // All of your code
  while (1) {  // infinite loop
  }
}

If the Arduino goes into an infinite loop it will not return until you reset it, so make sure you do this when you are finished with your code 无限循环.

Previous:Arduino mills() function

Next: None

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.

Article URL:

Related Articles

Arduino mills() function

Publish Date:2025/04/16 Views:156 Category:C++

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 th

Arduino 中停止循环

Publish Date:2024/03/13 Views:472 Category:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 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++ 中使用自定义定义的函数将十进制数转换为二进制数

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial