Оператор IF выполняет только один заказ (arduino)

#if-statement #arduino-uno

Вопрос:

 #include <Keypad.h>
long readUltrasonicDistance(int triggerPin, int echoPin)
{
  pinMode(triggerPin, OUTPUT);  // Clear the trigger
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echoPin, HIGH);
}

const byte ROWS = 4; // Four rows
const byte COLUMNS = 3; // Three columns
char keys[ROWS][COLUMNS] = {

  {'1', '2', '3'},

  {'4', '5', '6'},

  {'7', '8', '9'},

  {'*', '0', '#'}

};
byte pin_ROWS[ROWS] = { 4, 13, 12, 11 };
byte pin_COLUMNS[COLUMNS] = { 10, 8, 7 };
char digit1;
char digit2;
char digit3;
char digit4;
char enter;
int counter = 1;
Keypad keypad = Keypad(makeKeymap(keys), pin_ROWS , pin_COLUMNS , ROWS , COLUMNS );

void setup()
{
  Serial.begin(9600);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(9, OUTPUT);
}

void loop()
{
  int x = 0.01723 * readUltrasonicDistance(3, 2);
  char key = keypad.getKey();
    
 
  while (  x < 30) {
    tone(9, 30);
     digitalWrite(5, HIGH);
     digitalWrite(6, HIGH);
    delay (100); 
    if (key) {
      if (counter == 1) {digit1 = key; }
      if (counter == 2) {digit2 = key;}
      if (counter == 3) {digit3 = key;}
      if (counter == 4) {digit4 = key;}
      if (counter == 5) {enter = key;}
      counter   ;
      Serial.println(key);}
    if (counter == 6) {
      switch (enter) {
        case'#':
          if (digit1 == '2' amp;amp; digit2 == '0' amp;amp; digit3 == '2' amp;amp; digit4 == '1') {
            noTone(9); 
            digitalWrite(5, LOW);
            digitalWrite(6, LOW);
            Serial.println("right pass");
            
          break;
          default:
          Serial.print("wrong pass");
          }
      }
          digit1 = NULL;
          digit2 = NULL;
          digit3 = NULL;
          digit4 = NULL;
          enter = NULL;
          counter = 1;
      }
    
   
     
break;
    }


    
     noTone(9);
   digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    
  }
 

Я столкнулся с проблемой с этим кодом, условие if для пароля выполняется хорошо, и последовательный монитор набирает «правильный проход», но другие действия не сработали так, как предполагалось (noTone(9); digitalWrite(5, НИЗКИЙ);digitalWrite(6, НИЗКИЙ); )

что могло вызвать такое ?

это похоже на систему сигнализации, где считывается расстояние с помощью ультразвукового датчика, и когда оно меньше 30 см, загораются два светодиода и зуммер, и вы должны ввести правильный пропуск, чтобы остановить сигнал тревоги.

Комментарии:

1. исправьте отступ. это может помочь увидеть проблему