在Arduino中,完成特定次数的操作后退出循环。

huangapple go评论127阅读模式
英文:

Exiting loop in Arduino after doing an operation for specific number of times

问题

我可以帮你修改Arduino中的void loop函数,使其在打开和关闭灯/电磁阀30次后退出循环。修改后的代码如下:

  1. int relay = 8;
  2. int count = 0;
  3. void setup() {
  4. // put your setup code here, to run once:
  5. pinMode(relay, OUTPUT);
  6. }
  7. void loop() {
  8. // put your main code here, to run repeatedly:
  9. if (count < 30) {
  10. digitalWrite(relay, HIGH);
  11. delay(5000);
  12. digitalWrite(relay, LOW);
  13. delay(200);
  14. count++;
  15. } else {
  16. // Exit the loop
  17. while (true) {
  18. // Do nothing or add your desired code here
  19. }
  20. }
  21. }

这样修改后,当循环执行了30次后,程序将进入一个无限循环,你可以在其中添加你想要的代码,或者保持空白。这样就实现了在打开和关闭灯/电磁阀30次后退出循环的功能。希望对你有帮助!

英文:

I want to turn on light/solenoid valve on and off 30 times only, and after that I want to exit the loop. How can I modify the void loop in Arduino? My program is given below. Thanks for your help.

  1. int relay = 8;
  2. void setup() {
  3. // put your setup code here, to run once:
  4. pinMode(relay, OUTPUT);
  5. }
  6. void loop() {
  7. // put your main code here, to run repeatedly:
  8. digitalWrite(relay, HIGH);
  9. delay(5000);
  10. digitalWrite(relay, LOW);
  11. delay(200);
  12. }

答案1

得分: 1

只需使用一个for循环。我不会帮你做作业,但你可以查看这个网站:https://www.geeksforgeeks.org/python-for-loops/

英文:

Just use a for loop. I won't do your homework for you, but check this site. https://www.geeksforgeeks.org/python-for-loops/

答案2

得分: 0

这个想法是将你的代码从loop函数中移出(该函数在循环中被无限调用),而是在setup函数中调用你的代码一次(该函数只被调用一次)。

然后,你可以使用一个for循环来重复执行你的代码n次(在这个例子中是30次)。

这里有一个示例(显然可以进行重构),但应该能够演示你想要实现的目标:

  1. int relay = 8;
  2. void setup() {
  3. // 在这里放置你的设置代码,只运行一次:
  4. pinMode(relay, OUTPUT);
  5. // 切换30次
  6. for (int i = 0; i < 30; i++) {
  7. digitalWrite(relay, HIGH);
  8. delay(5000);
  9. digitalWrite(relay, LOW);
  10. delay(200);
  11. }
  12. }
  13. void loop() {
  14. // 在这里放置你的主要代码,重复运行:
  15. }
英文:

The idea is to move your code out of the loop function (which is called forever in a loop), and instead to call your code once in the setup function (which is called once).

You then can wrap your code in a for loop to have it repeat n times (in this case 30).

Here is an example (which obviously can be refactored), but should demonstrate what you are trying to accomplish:

  1. int relay = 8;
  2. void setup() {
  3. // put your setup code here, to run once:
  4. pinMode(relay, OUTPUT);
  5. // toggle 30 times
  6. for (int i = 0; i &lt; 30; i++) {
  7. digitalWrite(relay, HIGH);
  8. delay(5000);
  9. digitalWrite(relay, LOW);
  10. delay(200);
  11. }
  12. }
  13. void loop() {
  14. // put your main code here, to run repeatedly:
  15. }

huangapple
  • 本文由 发表于 2023年8月9日 03:18:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定