Time 即使它是假的,它也不起作用
我为一个糖果盒写了这个代码。箱子将在特定时间和日期打开和关闭。 可通过输入菜单进行调整:此处为void menu()。您可以选择是或否日期。菜单从星期一开始。现在的问题是,我在一个无休止的循环中Time 即使它是假的,它也不起作用,time,while-loop,arduino,Time,While Loop,Arduino,我为一个糖果盒写了这个代码。箱子将在特定时间和日期打开和关闭。 可通过输入菜单进行调整:此处为void menu()。您可以选择是或否日期。菜单从星期一开始。现在的问题是,我在一个无休止的循环中 while ((B1val != 1) || (B2val != 1) || (aState != aLastState)) { 如果我检查这些值,它们会变为1,但不会发生任何情况,可能Arduino无法读取or(| |) 这是完整的代码。 谢谢 #包括 #包括 #包括 #包括 液晶I2C液晶显示器(
while ((B1val != 1) || (B2val != 1) || (aState != aLastState)) {
如果我检查这些值,它们会变为1,但不会发生任何情况,可能Arduino无法读取or(| |)
这是完整的代码。
谢谢
#包括
#包括
#包括
#包括
液晶I2C液晶显示器(0x27,16,2);
//按钮销
#定义L 5//锁/继电器
#定义B1 6//按钮的是/和解锁
#为“否”定义B2 7//按钮
#定义SW 4//SW以进入菜单
#定义输出2
#定义输出B 3
//领导
#定义LR 10//Led引脚为红色
#定义LG 11//Led引脚为绿色
//安全是(1)或否(0)的整数
int星期一=1;
int周二=1;
int=1;
int星期四=1;
int星期五=1;
int周六=1;
int Sunday=1;
char Weekday=‘星期一’;
intontime=1;
int offTime=24;
int计数器=0;
int aState;
阿拉斯塔特国际酒店;
无效设置()
{
lcd.begin();
Serial.begin(9600);
setSyncProvider(RTC.get);
aLastState=数字读取(输出);
//设置pinmodes
pinMode(输出、输入);
pinMode(输出B,输入);
数字写入(B1,低电平);
数字写入(B2,低电平);
数字写入(SW,低电平);
引脚模式(B1,输入);
引脚模式(B2,输入);
pinMode(开关,输入);
//打开背光并打印信息。
}
void循环(){
循环:
TMU\t tm;
printDay();
int B1val=数字读取(B1);
int B2val=数字读取(B2);
int SWval=数字读取(SW);
延迟(10);
串行打印(B1val);
串行打印(B2val);
串行打印(SWval);
如果(hour()>=onTime&&hour()=onTime&&hour()=onTime&&hour()24){
onTime=0;
}
lcd.setCursor(9,0);
lcd.打印(实时);
lcd.打印(“:00”);
//lcd.setCursor(9,1);//设置光标
//lcd.打印(关闭时间);
//lcd.打印(“:00”);
}
}
阿拉斯塔特=阿斯塔特;
休息时间:
而(B2!=高){//接受关闭时间
如果(B1=高){
准时去;
}
阿拉斯塔特=阿斯塔特;
aState=数字读取(输出);
如果(数字读取(输出B)!=aState){
如果(aState!=aLastState){
休息时间+1;
串行打印LN(关闭时间);
//lcd.setCursor(9,0);
//lcd.打印(实时);
//lcd.打印(“:00”);
lcd.setCursor(9,1);//设置光标
lcd.打印(关闭时间);
lcd.打印(“:00”);
}
否则{
休息时间-1;
}
如果(关闭时间>24){
关闭时间=0;
}
如果(关闭时间<0){
关闭时间=24;
}
//lcd.setCursor(9,0);
//lcd.打印(实时);
//lcd.打印(“:00”);
lcd.setCursor(9,1);//设置光标
lcd.打印(关闭时间);
lcd.打印(“:00”);
延迟(100);
如果(B1val==1){
后藤环;
}
}
}
阿拉斯塔特=阿斯塔特;
}
作废打印日()
{
国际日;
天=工作日();
如果(天==1){
工作日=‘星期日’;
}
如果(天==2){
工作日=‘星期一’;
}
如果(天==3){
工作日=‘星期二’;
}
如果(天==4){
平日=‘星期三’;
}
如果(天==5){
工作日=‘星期四’;
}
如果(天==6){
工作日=‘星期五’;
}
如果(天==7){
工作日=‘星期六’;
}
}
您正在将循环中的B1val声明为第二个变量!
所以你永远无法改变你想改变的变量
删除while循环中的
int
。“星期一”不是字符。那么如何保护它呢?:)学习C.学习C.C!中字符和字符串的区别无论如何,我不认为这是while中的问题。为了使while循环条件起作用,两个按钮都必须得到1,并且状态必须相等。既然你写的是NOT,那么任何一个没有发生的事情都会导致条件仍然为真。我认为您要么想使用,而不是,要么想重新考虑如何编写条件。不管怎样,我认为你在那种情况下逻辑混乱了。
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//button Pins
#define L 5 //Lock/ Relay
#define B1 6 //Button for yes/ and unlock
#define B2 7 //Button for no
#define SW 4 //SW to enter menu
#define outputA 2
#define outputB 3
//Led
#define LR 10 //Led Pin for Red
#define LG 11 //Led Pin for green
//integers to safe yes(1) or no(0)
int Monday = 1;
int Tuesday = 1;
int Wednesday = 1;
int Thursday = 1;
int Friday = 1;
int Saturday = 1;
int Sunday = 1;
char Weekday = 'Monday';
int onTime = 1;
int offTime = 24;
int counter = 0;
int aState;
int aLastState;
void setup()
{
lcd.begin();
Serial.begin(9600);
setSyncProvider(RTC.get);
aLastState = digitalRead(outputA);
//set pinmodes
pinMode (outputA, INPUT);
pinMode (outputB, INPUT);
digitalWrite(B1, LOW);
digitalWrite(B2, LOW);
digitalWrite(SW, LOW);
pinMode(B1, INPUT);
pinMode(B2, INPUT);
pinMode(SW, INPUT);
// Turn on the backlight and print a message.
}
void loop() {
LOOP:
tmElements_t tm;
printDay();
int B1val = digitalRead(B1);
int B2val = digitalRead(B2);
int SWval = digitalRead(SW);
delay(10);
Serial.print(B1val);
Serial.print(B2val);
Serial.print(SWval);
if (hour() >= onTime && hour() <= offTime) {
digitalWrite(LG, HIGH);
analogWrite(LG, 50); //set light to 50
Serial.println("greeny");
}
else {
digitalWrite(L, LOW);
digitalWrite(LR, HIGH);
analogWrite(LR, 255); //set light to 50
}
if (B1val == 1) { //trying to unlock
Serial.println("unlock");
if (hour() >= onTime && hour() <= offTime && 'Weekday' == 1)
{ //flash green led
digitalWrite(L, HIGH); //unlock Lock
digitalWrite(LG, HIGH); //turn green Led on
delay(200);
analogWrite(LG, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LG, HIGH); //turn green Led on
delay(200);
analogWrite(LG, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LG, HIGH); //turn green Led on
delay(200);
analogWrite(LG, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LG, HIGH); //turn green Led on
delay(200);
analogWrite(LG, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LG, HIGH); //turn green Led on
delay(200);
analogWrite(LG, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LG, HIGH); //turn green Led on
delay(200);
analogWrite(LG, 50); //set light to 50 to not hurt your eyes
}
else { //flash red led
Serial.println("unlock false");
digitalWrite(L, LOW); //lock locked
digitalWrite(LR, HIGH); //turn red Led on
delay(200);
analogWrite(LR, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LR, HIGH); //turn red Led on
delay(200);
analogWrite(LR, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LR, HIGH); //turn red Led on
delay(200);
analogWrite(LR, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LR, HIGH); //turn red Led on
delay(200);
analogWrite(LR, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LR, HIGH); //turn red Led on
delay(200);
analogWrite(LR, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LR, HIGH); //turn red Led on
delay(200);
analogWrite(LR, 50); //set light to 50 to not hurt your eyes
}
}
if (hour() >= onTime && hour() <= offTime) {
Serial.println("unlocked");
analogWrite(LG, 50); //set light to 50
}
else {
Serial.println("locked");
analogWrite(LR, 50); //set light to 50
}
if (SWval == 1) { //to enter menu when rotary encoder switch clicked
menu();
}
else {
lcd.clear();
lcd.noBacklight(); // turn off backlight
}
aLastState = digitalRead(outputA);
}
void menu() {
int B1val = digitalRead(B1);
int B2val = digitalRead(B2);
int SWval = digitalRead(SW);
Serial.println("menu");
Monday:
Serial.println("Monday");
lcd.backlight(); //turn on lcd
aLastState = aState;
aState = digitalRead(outputA);
lcd.print(" Monday"); //display print Monday
while ((B1val != 1) || (B2val != 1) || (aState != aLastState)) {
Serial.println("while");
aState = digitalRead(outputA);
int B1val = digitalRead(B1);
int B2val = digitalRead(B2);
int SWval = digitalRead(SW);
Serial.print(B1val);
Serial.print(B2val);
Serial.print(SWval);
}
if (B1val == HIGH) {
Serial.println("Monday yes");
Monday = 1;
lcd.setCursor(0, 1);
lcd.print(" Yes ");
delay(100);
goto Monday;
}
else if (B2val == HIGH) {
Serial.println("Monday no");
Monday = 0;
lcd.setCursor(0, 1);
lcd.print(" No ");
delay(100);
goto Monday;
}
else if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
goto Tuesday;
}
else {
goto Monday;
}
}
delay(100);
Tuesday:
Serial.println("Tuesday");
aLastState = aState;
aState = digitalRead(outputA);
lcd.clear();
lcd.print(" Tuesday"); //display print Tuesday
while ((B1 != HIGH) || (B2 != HIGH) || (aState != aLastState)) {}
if (B1 == HIGH) {
Serial.println("Tuesday yes");
Tuesday = 1;
lcd.setCursor(0, 1);
lcd.print(" Yes ");
delay(100);
goto Tuesday;
}
else if (B2 == HIGH) {
Serial.println("Tuesday no");
Tuesday = 0;
lcd.setCursor(0, 1);
lcd.print(" No ");
delay(100);
goto Tuesday;
}
else if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
goto Wednesday;
}
else {
goto Tuesday;
}
}
delay(100);
Wednesday:
Serial.println("Wednesday");
aLastState = aState;
aState = digitalRead(outputA);
lcd.clear();
lcd.print(" Wednesday"); //display print
while (B1 != HIGH || B2 != HIGH || aState != aLastState) {}
if (B1 == HIGH) {
Serial.println("Wednesday yes");
Wednesday = 1;
lcd.setCursor(0, 1);
lcd.print(" Yes ");
delay(100);
goto Wednesday;
}
else if (B2 == HIGH) {
Serial.println("Wednesday no");
Wednesday = 0;
lcd.setCursor(0, 1);
lcd.print(" No ");
delay(100);
goto Wednesday;
}
else if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
goto Thursday;
}
else {
goto Wednesday;
}
}
delay(100);
Thursday:
aLastState = aState;
aState = digitalRead(outputA);
lcd.clear();
lcd.print(" Thursday"); //display print
while (B1 != HIGH || B2 != HIGH || aState != aLastState) {}
if (B1 == HIGH) {
Thursday = 1;
lcd.setCursor(0, 1);
lcd.print(" Yes ");
delay(100);
goto Friday;
}
else if (B2 == HIGH) {
Thursday = 0;
lcd.setCursor(0, 1);
lcd.print(" No ");
delay(100);
goto Thursday;
}
else if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
goto Thursday;
}
else {
goto Thursday;
}
}
delay(100);
Friday:
aLastState = aState;
aState = digitalRead(outputA);
lcd.clear();
lcd.print(" Friday"); //display print
while (B1 != HIGH || B2 != HIGH || aState != aLastState)
{}
if (B1 == HIGH) {
Friday = 1;
lcd.setCursor(0, 1);
lcd.print(" Yes ");
delay(100);
goto Friday;
}
else if (B2 == HIGH) {
Friday = 0;
lcd.setCursor(0, 1);
lcd.print(" No ");
delay(100);
goto Friday;
}
else if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
goto Saturday;
}
else {
goto Friday;
}
}
delay(100);
Saturday:
aLastState = aState;
aState = digitalRead(outputA);
lcd.clear();
lcd.print(" Saturday"); //display print
while (B1 != HIGH || B2 != HIGH || aState != aLastState)
{}
if (B1 == HIGH) {
Saturday = 1;
lcd.setCursor(0, 1);
lcd.print(" Yes ");
delay(100);
goto Saturday;
}
else if (B2 == HIGH) {
Saturday = 0;
lcd.setCursor(0, 1);
lcd.print(" No ");
delay(100);
goto Saturday;
}
else if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
goto Sunday;
}
else {
goto Saturday;
}
}
delay(100);
Sunday:
aLastState = aState;
aState = digitalRead(outputA);
lcd.clear();
lcd.print(" Sunday"); //display print
while (B1 != HIGH || B2 != HIGH || aState != aLastState) {}
if (B1 == HIGH) {
Sunday = 1;
lcd.setCursor(0, 1);
lcd.print(" Yes ");
delay(100);
goto Sunday;
}
else if (B2 == HIGH) {
Sunday = 0;
lcd.setCursor(0, 1);
lcd.print(" No ");
delay(100);
goto Sunday;
}
else if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
goto Time;
}
else {
goto Saturday;
}
}
Time:
Serial.println("time");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("turn on: "); //display print
lcd.print(onTime);
lcd.print(":00");
lcd.setCursor(0, 1); //set cursor
lcd.print("turn off: ");
lcd.print(offTime);
lcd.print(":00");
OnTime:
while (B1 != HIGH) { //accept on time
if (B2 == HIGH) {
goto OffTime;
}
aLastState = aState;
aState = digitalRead(outputA);
if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
onTime + 1;
lcd.setCursor(9, 0);
lcd.print(onTime);
lcd.print(":00");
// lcd.setCursor(9, 1); //set cursor
// lcd.print(offTime);
// lcd.print(":00");
}
else {
onTime - 1;
}
if ( onTime < 0) {
onTime = 24;
}
if ( onTime > 24) {
onTime = 0;
}
lcd.setCursor(9, 0);
lcd.print(onTime);
lcd.print(":00");
// lcd.setCursor(9, 1); //set cursor
// lcd.print(offTime);
// lcd.print(":00");
}
}
aLastState = aState;
OffTime:
while (B2 != HIGH) { //accept off time
if (B1 == HIGH) {
goto OnTime;
}
aLastState = aState;
aState = digitalRead(outputA);
if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
offTime + 1;
Serial.println(offTime);
// lcd.setCursor(9, 0);
// lcd.print(onTime);
// lcd.print(":00");
lcd.setCursor(9, 1); //set cursor
lcd.print(offTime);
lcd.print(":00");
}
else {
offTime - 1;
}
if ( offTime > 24) {
offTime = 0;
}
if ( offTime < 0) {
offTime = 24;
}
// lcd.setCursor(9, 0);
// lcd.print(onTime);
// lcd.print(":00");
lcd.setCursor(9, 1); //set cursor
lcd.print(offTime);
lcd.print(":00");
delay(100);
if(B1val == 1){
goto LOOP;
}
}
}
aLastState = aState;
}
void printDay()
{
int day;
day = weekday();
if (day == 1) {
Weekday = 'Sunday';
}
if (day == 2) {
Weekday = 'Monday';
}
if (day == 3) {
Weekday = 'Tuesday';
}
if (day == 4) {
Weekday = 'Wednesday';
}
if (day == 5) {
Weekday = 'Thursday';
}
if (day == 6) {
Weekday = 'Friday';
}
if (day == 7) {
Weekday = 'Saturday';
}
}