【发布时间】: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'”。 最好解决编译器的问题在要求其他人告诉您编译器已经告诉您的内容之前找到(即警告)。
-
你从来没有打电话给
SetDataTime?Mode1;缺少括号以使其成为函数调用:Mode1();