Пока не работает, даже если это ложь - PullRequest
0 голосов
/ 17 марта 2020

Я написал этот код для коробки конфет. Коробка будет открываться и закрываться в определенные c раз и дней. Это можно настроить, войдя в меню: здесь void menu (). Вы можете выбрать, будет ли это день да или нет. Меню начинается с понедельника. Проблема сейчас в том, что я нахожусь в бесконечном l oop in

while ((B1val != 1) || (B2val != 1) || (aState != aLastState)) {

Если я проверяю значения, они меняются на 1, но ничего не происходит, может быть Arduino не может прочитать или (||).

Вот полный код. Спасибо.

#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';
  }
 }

1 Ответ

0 голосов
/ 17 марта 2020

Вы повторно объявляете B1val внутри вашего l oop в качестве второй переменной! Таким образом, вы никогда не сможете изменить переменную, которую хотите изменить ...

Удалите int из вашего while-l oop.

...