an if function cause a problem

Hi everyone,

Why arduino executes this function continuously even if the push button isn’t pressed?

{
  if (digitalRead(Exit) == HIGH)
      {
        if (Available != CAPACITY) {
          Available++;;
          myservo.write(BarUp);;
          delay(3000);
          myservo.write(BarLow);
        }
      }

//========================

This is the sketch:

#include <LiquidCrystal.h>
#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>


Servo myservo;

#define ServoM    6
#define Exit      16

#define BarLow    177       //Low position of the barrier.
#define BarUp     95        //Up position of the barrier.
#define CAPACITY  8         //Capacity 

#define SS_PIN    10
#define RST_PIN   9

#define redLed    14
#define greenLed  15

MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal lcd(8, 7, 5, 4, 3, 2);

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  myservo.attach(ServoM);

  Serial.println("Approximate your card to the reader...");
  Serial.println();
  myservo.write(BarLow);
  pinMode(Exit, INPUT);

  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  lcd.begin(16, 2);


}

int  Available = 9;                   // Number of places available.

//================================================================
void loop() {
  if (digitalRead(Exit) == HIGH)
  {
    if (Available != CAPACITY) {
      Available++;;
      myservo.write(BarUp);;
      delay(3000);
      myservo.write(BarLow);
    }
  }
  if (Available == 0) {
    lcd.setCursor(0, 0);
    lcd.print("Full");
  } else {
    lcd.setCursor(0, 0);
    lcd.print("Empty  : ");
    lcd.print(Available);


  }

  {

    if (Available >= 1) {
      digitalWrite(greenLed, HIGH);
      digitalWrite(redLed, LOW);
    } else {
      digitalWrite(greenLed, LOW);
      digitalWrite(redLed, HIGH);
    }
    {
      // Look for new cards
      if ( ! mfrc522.PICC_IsNewCardPresent())
      {
        return;
      }
      // Select one of the cards
      if ( ! mfrc522.PICC_ReadCardSerial())
      {
        return;
      }
      //Show UID on serial monitor
      Serial.print("UID tag :");
      String content = "";
      byte letter;
      for (byte i = 0; i < mfrc522.uid.size; i++)
      {
        Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
        Serial.print(mfrc522.uid.uidByte[i], HEX);
        content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
        content.concat(String(mfrc522.uid.uidByte[i], HEX));
      }
      Serial.println();
      Serial.print("Message : ");
      content.toUpperCase();
      if (content.substring(1) == "2C 61 A8 B2")
      { if (Available != 0) {
          Available--;
          myservo.write(BarUp);
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("open bar");
          lcd.setCursor(0, 1);
          lcd.print("warning");
          delay(5000);
          myservo.write(BarLow);
          lcd.clear();

        }
      }
    }
  }
}

How is the Exit pin wired ? Do you have a pullup resistor holding it normally HIGH ? If not the use INPUT_PULLUP in the pinMode() function to activate the built in pullup resistor and wire the button to take the pin to GND when pressed.

Wild guess, floating input? You probably need a pull-down resistor.

@UKheliBob, I would guess it will need a pull-down ;)

pulldown ? Yes, you are probably right as currently written, but you and I know that using INPUT_PULLUP and adjusting the logic is easier to do and needs no more hardware.

it's correct? (10k resistor) |500x235

No idea, are the two pins at the right (on the button) connected to each other?

UKHeliBob: pulldown ? Yes, you are probably right as currently written, but you and I know that using INPUT_PULLUP and adjusting the logic is easier to do and needs no more hardware.

Yes, and if you do that, you can troubleshoot the hardware by just using a jumper between ground and the input, to make sure that your switch or switch wiring is not broken.

Also if you have a hardware problem with the pulldown configuration, you can trouble shoot it with a DVM.

Franz0: it's correct? (10k resistor) |500x235

Here it says A3 equals 17 but you have 16 in your code.

Why not simply use the A* pin names and avoid any such confusion.