【问题标题】:How do I switch from one loop to another on an arduino如何在 arduino 上从一个循环切换到另一个循环
【发布时间】:2022-01-05 06:37:50
【问题描述】:
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd (3, 4, 5, 6, 7, 8);
RTC_DS1307 RTC;

const int b1 = 9;  //45 minutlik
const int b2 = 10; //40 minutlik
const int  relay = 11; //zvanok

int hourupg;
int minupg;
int dayupg;
int monthupg;
int yearupg;

void setup () {
  Serial.begin (9600);
  Serial.println ("Starting");
  Wire.begin ();
  RTC.begin ();

  pinMode(b1, INPUT);
  pinMode(b2, INPUT);

  if (!RTC.isrunning()) {
    Serial.println ("Don't set");
    RTC.adjust (DateTime(__DATE__, __TIME__));
  }
  else {
    Serial.println ("Set");
  }
}

void SetDataTime () {
  DateTime now = RTC.now();
  Serial.print ("Hour: ");

  if (now.hour() <= 9) {
    Serial.print ("0");
  }
  Serial.print (now.hour(), DEC);
  hourupg = now.hour();
  Serial.print (":");

  if (now.minute() <= 9) {
    Serial.print ("0");
  }
  Serial.print (now.minute(), DEC);
  minupg = now.minute();
  Serial.print (":");

  Serial.println (now.second(), DEC);

  Serial.print ("Date: ");
  if (now.day() <= 9) {
    Serial.print ("0");
  }
  Serial.print (now.day(), DEC);
  dayupg = now.day();
  Serial.print ("/");

  if (now.month() <= 9) {
    Serial.print ("0");
  }
  Serial.print (now.month(), DEC);
  monthupg = now.month();
  Serial.print ("/");
  Serial.println (now.year(), DEC);
  yearupg = now.year();

  delay (1000);
}

void Mode1 () {                                                              //Mode1
  if (hourupg == 8 && minupg == 0) {               
    digitalWrite (relay, HIGH);
    delay (3000);
    digitalWrite (relay, LOW);
    delay (1000);
    return 3;
    digitalWrite (relay, LOW);
  }
}
void Mode2 () {                                           //Mode2
  if (hourupg == 17 && minupg == 40) {                
    digitalWrite (relay, HIGH);
    delay (3000);
    digitalWrite (relay, LOW);
    delay (1000);
    return 3;
    digitalWrite (relay, LOW);
  }
}

void loop () {
  if (digitalRead(b1) == HIGH) {
    Serial.println ("Button1 pressed");
    Mode1;
  }
  if (digitalRead(b2) == HIGH) {
    Serial.println ("Button2 pressed");
    Mode2;
  }
}

//this code by Asilbek Ergashov

我在我的代码中使用了 4 个循环,当我运行它们时,只有 void 循环中的函数起作用,不会切换到其他循环。

例如: 我首先使用了 SetDataTime 循环并编写了将其串行输出到监视器的代码。但这没有用。 void Mode1 和 Mode2 类似。

我的代码的主要功能是按下按钮1时切换到模式1,按下按钮2时切换到模式2。

如果我使用 void 循环函数,该函数可以工作。

我在做什么??? 如何从一个循环切换到另一个循环???

【问题讨论】:

  • 看起来你很困惑。 SetDataTime() 不是循环,它是一个函数。其他的是函数,所以你没有 4 个循环,你有 4 个函数。函数在您或其他人调用它们时执行。 Arduino隐藏代码调用函数loop()(它仍然只是一个函数),这就是它一直被执行的原因。其他你必须在你的代码中调用。
  • 你的代码是compile without warnings吗? 警告示例:“return-statement with a value, in function return 'void'”和“statement is a reference, not call, function 'Mode1'”。 最好解决编译器的问题在要求其他人告诉您编译器已经告诉您的内容之前找到(即警告)。
  • 你从来没有打电话给SetDataTimeMode1; 缺少括号以使其成为函数调用:Mode1();

标签: c++ loops arduino


【解决方案1】:

你不是在写循环,你是在写函数。

Arduino 的工作方式是有两种“特殊”功能,一种称为setup(),一种称为loop()。当板子启动时,引导加载程序调用一次设置函数,然后重复调用循环函数。您可能会认为这有点 ****,但它与 PC 程序的执行方式非常相似,main() 是您通常调用所有代码的地方。

我认为您需要做的是创建一个新函数并将代码从loop() 移动到该新函数中。然后在你loop() 函数中调用你想要的函数,按照你想要调用它们的顺序。请记住,循环功能会一直重复,直到您关闭电源或代码崩溃。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    相关资源
    最近更新 更多