title: Arduino/C++ IF/ELSE statements
body: <p>[Later Edit] I have a Arduino Uno together with an RFID-RC522, a servo motor and some LED's + I also made a aluminium foil sandwich/switch. Here is the code BEFORE THE ALUMINIUM SWITCH, with everything working perfect:</p>
  
  <pre><code>#include &lt;SPI.h&gt;
  #include &lt;MFRC522.h&gt;
  #include &lt;Servo.h&gt;
  
  #define SS_PIN 10 // SLAVE SELECT la DIGITAL 10
  #define RST_PIN 9 // PIN RESET la DIGITAL 9
  #define LED_V 5 //LED_Verde la D5
  #define LED_R 4 //LED_Rosu la D4
  #define BUZZER 2 //BUZZER la D2
  MFRC522 mfrc522(SS_PIN, RST_PIN);   // Creaza instanta MFRC522.
  Servo bariera; //defineste nume Servo
  
  void setup() 
  {
    Serial.begin(9600);   // Incepe comunicatia seriala cu PC-ul
    SPI.begin();      // Initializeaza bus-ul SPI
    mfrc522.PCD_Init();   // Initializeaza MFRC522
    bariera.attach(3); //servo pin
    bariera.write(0); //servo pozitie start
    pinMode(LED_V, OUTPUT);
    pinMode(LED_R, OUTPUT);
    pinMode(BUZZER, OUTPUT);
    noTone(BUZZER);
    Serial.println("Apropie cartela...[x][][]");
    delay(2500);
    Serial.println("Apropie cartela...[x][x][]");
    delay(3500);
    Serial.println("Apropie cartela...[x][x][x]");
    delay(4500);
    Serial.println("Astept raspuns utilzator!");
    Serial.println();
  
  }
  void loop() 
  {
    // Cauta cartele noi
    if ( ! mfrc522.PICC_IsNewCardPresent()) 
    {
      return;
    }
    // Selecteaza una dintre cartele
    if ( ! mfrc522.PICC_ReadCardSerial()) 
    {
      return;
    }
    //Arata UID pe monitorul serial
    Serial.print("ID Tag/Cartela: ");
    String content= "";
    byte letter;
    for (byte i = 0; i &lt; mfrc522.uid.size; i++) 
    {
       Serial.print(mfrc522.uid.uidByte[i] &lt; 0x10 ? " 0" : " ");
       Serial.print(mfrc522.uid.uidByte[i], HEX);
       content.concat(String(mfrc522.uid.uidByte[i] &lt; 0x10 ? " 0" : " "));
       content.concat(String(mfrc522.uid.uidByte[i], HEX));
    }
    Serial.println();
    Serial.print("Mesaj : ");
    content.toUpperCase();
    if (content.substring(1) == "A0 B9 8D 7C") //aici schimbam ID-ul (UID-ul) cartelelor autorizate, citite anterior pe monitorul serial
    {
      Serial.println("Acces autorizat! Bine ati venit!");
      Serial.println();
      delay(500);
      digitalWrite(LED_V, HIGH);
      tone(BUZZER, 350);
      delay(300);
      noTone(BUZZER);
      bariera.write(120);
      delay(5000);
      bariera.write(0);
      digitalWrite(LED_V, LOW);
  
    }
  
   else   {
      Serial.println(" Access interzis! La revedere!");
      digitalWrite(LED_R, HIGH);
      tone(BUZZER, 300);
      delay(1000);
      digitalWrite(LED_R, LOW);
      noTone(BUZZER);
    }
  } 
  </code></pre>
  
  <p>So if the RFID CARD/TAG is "A0 B9 8D 7C", LED_V lights, short buzzer tone, and servo (here named bariera) goes to 120 degrees.</p>
  
  <p>If the RFID CARD/TAG is NOT "A0 B9 8D 7C", LED_R lights, long buzzer tone and serv does nothing.</p>
  
  <p>I also made an aluminium foil switch (two sheets of aluminium separated by cardboard, when pressed ((digitalRead(IESIRE)== 0) it should: LED_V lights, short buzzer tone, and servo (here named bariera) goes to 120 degrees.</p>
  
  <p>This is the code for the whole thing with the switch included (#define IESIRE 6):</p>
  
  <pre><code>#include &lt;SPI.h&gt;
  #include &lt;MFRC522.h&gt;
  #include &lt;Servo.h&gt;
  
  #define SS_PIN 10 // SLAVE SELECT la DIGITAL 10
  #define RST_PIN 9 // PIN RESET la DIGITAL 9
  #define LED_V 5 //LED_Verde la D5
  #define LED_R 4 //LED_Rosu la D4
  #define BUZZER 2 //BUZZER la D2
  #define IESIRE 6 // IESIRE la D6
  MFRC522 mfrc522(SS_PIN, RST_PIN);   // Creaza instanta MFRC522.
  Servo bariera; //defineste nume Servo
  
  void setup() 
  {
    Serial.begin(9600);   // Incepe comunicatia seriala cu PC-ul
    SPI.begin();      // Initializeaza bus-ul SPI
    mfrc522.PCD_Init();   // Initializeaza MFRC522
    bariera.attach(3); //servo pin
    bariera.write(0); //servo pozitie start
    pinMode(LED_V, OUTPUT);
    pinMode(LED_R, OUTPUT);
    pinMode(BUZZER, OUTPUT);
    pinMode(IESIRE, INPUT);
    digitalWrite(IESIRE, HIGH);
    noTone(BUZZER);
    Serial.println("Apropie cartela...[x][][]");
    delay(2500);
    Serial.println("Apropie cartela...[x][x][]");
    delay(3500);
    Serial.println("Apropie cartela...[x][x][x]");
    delay(4500);
    Serial.println("Astept raspuns utilzator!");
    Serial.println();
  }
  void loop() 
  {
    // Cauta cartele noi
    if ( ! mfrc522.PICC_IsNewCardPresent()) 
    {
      return;
    }
    // Selecteaza una dintre cartele
    if ( ! mfrc522.PICC_ReadCardSerial()) 
    {
      return;
    }
    //Arata UID pe monitorul serial
    Serial.print("ID Tag/Cartela: ");
    String content= "";
    byte letter;
    for (byte i = 0; i &lt; mfrc522.uid.size; i++) 
    {
       Serial.print(mfrc522.uid.uidByte[i] &lt; 0x10 ? " 0" : " ");
       Serial.print(mfrc522.uid.uidByte[i], HEX);
       content.concat(String(mfrc522.uid.uidByte[i] &lt; 0x10 ? " 0" : " "));
       content.concat(String(mfrc522.uid.uidByte[i], HEX));
    }
    Serial.println();
    Serial.print("Mesaj : ");
    content.toUpperCase();
    if (content.substring(1) == "A0 B9 8D 7C") //aici schimbam ID-ul (UID-ul) cartelelor autorizate, citite anterior pe monitorul serial
    {
      Serial.println("Acces autorizat! Bine ati venit!");
      Serial.println();
      delay(500);
      digitalWrite(LED_V, HIGH);
      tone(BUZZER, 350);
      delay(300);
      noTone(BUZZER);
      bariera.write(120);
      delay(5000);
      bariera.write(0);
      digitalWrite(LED_V, LOW);
  
    }
  
   else if (content.substring(1) != "A0 B9 8D 7C")    {
      Serial.println(" Access interzis! La revedere!");
      digitalWrite(LED_R, HIGH);
      tone(BUZZER, 300);
      delay(1000);
      digitalWrite(LED_R, LOW);
      noTone(BUZZER);
  
   }
  
    else if ((content.substring(1) != "A0 B9 8D 7C") &amp;&amp; (digitalRead(IESIRE)==0))     {
      Serial.println("Drum bun!");
      digitalWrite(LED_V, HIGH);
      tone(BUZZER, 350);
      delay(300);
      bariera.write(120);
      delay(5000);
      bariera.write(0);
      digitalWrite(LED_V, LOW);
  
    }
  }
  </code></pre>
  
  <p>Please ignore the else if statements and stuff, I'm fairly new to coding and I am trying my best. So if the switch is pressed (this translates to <code>digitalRead(IESIRE)==0)</code> )the servo need to go to 120 degrees, regardless of the other statements.</p>
  
  <pre><code>CARD/RFID TAG present &gt; recognised &gt; servo to 120 + led, buzzer
                  present &gt; not recognised &gt; led, buzzer
   IF SWITCH is pressed &gt; servo to 120 + led, buzzer REGARDLESS of the CARD stuff.
  </code></pre>
  
  <p>I hope this makes sense and it's more explicit.</p>