While loop only works when in a if loop. - HELP!

Hi! I am relatively new to Arduino and coding in general. I am having some trouble with a project, more specifically a part of a project where I need to stay in a while loop until the condition is changed, but I can only get into this while loop when I am already in an if loop. for some reason it won't work when it's not in the if loop.
The full code is here.

/*
 * Created in part by ArduinoGetStarted.com
 * Modified by Kittylover6709
 * 
 * This example code is in the public domain
 *
 * Tutorial page for original code: https://arduinogetstarted.com/tutorials/arduino-rfid-nfc-door-lock-system
 */

#include <SPI.h>
#include <MFRC522.h>
#include "pitches.h"
int lr = 1;
 
// notes in the melody:
int melody[] = {
  NOTE_F4, NOTE_C4, NOTE_E4, NOTE_C4};
int duration = 700;  // 500 miliseconds

#define LED_PIN 8
#define LED1_PIN 4
#define LED2_PIN 3
#define SS_PIN 10
#define RST_PIN 5
  int buzzer = 7;
  int varkey1 = 0x00;
  int varkey2 = 0x00;
  int varkey3 = 0x00;
  int varkey4 = 0x00;

MFRC522 rfid(SS_PIN, RST_PIN);

byte cardUID[4] = {0xE5, 0x09, 0x2C, 0x23};
byte varkeyUID[4] = {0x09, 0x19, 0xDE, 0xC2}; 

void setup() {
  pinMode(buzzer,OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  pinMode(LED1_PIN, OUTPUT);
  pinMode(LED2_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  Serial.begin(9600);
  SPI.begin(); // init SPI bus
  rfid.PCD_Init(); // init MFRC522
  pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output.
  //digitalWrite(RELAY_PIN, HIGH); // lock the door

  Serial.println("Tap RFID/NFC Tag on reader");
}

void loop() {
  int value = analogRead(A0);
  //Serial.println("Analog value : ");
  Serial.println(value);
  delay(400);

 if (value > 70){
    value = analogRead(A0);
    for (int thisNote = 0; thisNote < 2; thisNote++) {
    // pin8 output the voice, every scale is 0.5 sencond
    tone(7, melody[thisNote], duration);
    delay(400);
    
    
    digitalWrite(LED2_PIN, LOW);
    digitalWrite(LED1_PIN, HIGH);
    delay(400);
    digitalWrite(LED1_PIN, LOW);
    digitalWrite(LED2_PIN, HIGH);
    //delay(500);}
    }
    }
    else{
    digitalWrite(LED1_PIN, LOW);
    digitalWrite(LED2_PIN, LOW);
    }
     
  
  
   if (rfid.PICC_IsNewCardPresent()) { // new tag is available
      if (rfid.PICC_ReadCardSerial()) { // NUID has been read
      MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

        if (rfid.uid.uidByte[0] == cardUID[0] &&
            rfid.uid.uidByte[1] == cardUID[1] &&
            rfid.uid.uidByte[2] == cardUID[2] &&
            rfid.uid.uidByte[3] == cardUID[3] ) {
          Serial.println("Access is granted-card");
          digitalWrite(LED1_PIN, LOW);
          digitalWrite(LED2_PIN, LOW);
          digitalWrite(LED_PIN, HIGH);
          delay(5000);
          digitalWrite(LED_PIN, LOW);
          while (value > 70){
          value = analogRead(A0);
          delay(700);
          }
     }
       else
        if (rfid.uid.uidByte[0] == varkeyUID[0] &&
            rfid.uid.uidByte[1] == varkeyUID[1] &&
            rfid.uid.uidByte[2] == varkeyUID[2] &&
            rfid.uid.uidByte[3] == varkeyUID[3] ) {
          Serial.println("Access is granted-key");
          digitalWrite(LED1_PIN, LOW);
          digitalWrite(LED2_PIN, LOW);
          digitalWrite(LED_PIN, HIGH);
          delay(10000);
          digitalWrite(LED_PIN, LOW);
          while (value > 70){
          value = analogRead(A0);
          delay(700);
          }
    } 
        else
      { 
          Serial.print("Access denied for user with UID:");
          for (int i = 0; i < rfid.uid.size; i++) {
           Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
           Serial.print(rfid.uid.uidByte[i], HEX);
          }
          Serial.println();
        }

        rfid.PICC_HaltA(); // halt PICC
        rfid.PCD_StopCrypto1(); // stop encryption on PCD
      }
    }
  }

I need my code to be able to enter the while loop that is part of the if loop for when it sees an RFID card at any time, or at least when it is NOT in the first if loop. To clarify, the while loop that I need to activate is in this if loop

   if (rfid.PICC_IsNewCardPresent()) { // new tag is available
      if (rfid.PICC_ReadCardSerial()) { // NUID has been read
      MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

        if (rfid.uid.uidByte[0] == cardUID[0] &&
            rfid.uid.uidByte[1] == cardUID[1] &&
            rfid.uid.uidByte[2] == cardUID[2] &&
            rfid.uid.uidByte[3] == cardUID[3] ) {
          Serial.println("Access is granted-card");
          digitalWrite(LED1_PIN, LOW);
          digitalWrite(LED2_PIN, LOW);
          digitalWrite(LED_PIN, HIGH);
          delay(5000);
          digitalWrite(LED_PIN, LOW);
          while (value > 70){
          value = analogRead(A0);
          delay(700);
          }
     }

and the only way that while loop will start is if we are in this if loop,

if (value > 70){
    value = analogRead(A0);
    for (int thisNote = 0; thisNote < 2; thisNote++) {
    // pin8 output the voice, every scale is 0.5 sencond
    tone(7, melody[thisNote], duration);
    delay(400);
    
    
    digitalWrite(LED2_PIN, LOW);
    digitalWrite(LED1_PIN, HIGH);
    delay(400);
    digitalWrite(LED1_PIN, LOW);
    digitalWrite(LED2_PIN, HIGH);
    //delay(500);}
    }
    }
    else{
    digitalWrite(LED1_PIN, LOW);
    digitalWrite(LED2_PIN, LOW);
    }

Which with the implementation of this code, will usually not be true.
It could be something with that IF ELSE at the end of the if loop, but I have tried cutting that out and I seem to still have this problem.

I have just seen the smart people in this community, so I figured I would come here. If anyone has any ideas as to why this is happening, please tell me, I just can't figure it out, even though it is probably some obvious problem.

Thanks!!!

Hi @kittylover6709 ,
FYI "if" is not loop, it is condition statement.

The if statement checks for a condition and executes the following statement or set of statements if the condition is 'true'.

Ref: if - Arduino Reference

RV mineirin

Thanks! Can you think of any way that a previous if condition would affect a previous one, if it was not running?

Please slow down and calm down.
"if" is not at all any "loop".

Post the code You want to run. Then make some order in the rather confusing description. Lots of words, word sallad, is not an engineering way to describe things.
Is it "(value > 70)" You refer to? Use the tip below to show that the execution reaches that line of code.

In the mean time. Learn the technic of using Serial.println inside the code to gossip how the execution is going, and where in the code the execution takes place. Open Serial monitor in the IDE.

Perhaps you need to learn how to write a function (then you can call a function).

Add this to the end of your sketch --

void tuneTime ()
{
   for (int thisNote = 0; thisNote < 2; thisNote++) 
   {
    // pin8 output the voice, every scale is 0.5 sec
    tone(7, melody[thisNote], duration);
    delay(400);
    
    digitalWrite(LED2_PIN, LOW);
    digitalWrite(LED1_PIN, HIGH);
    delay(400);
    digitalWrite(LED1_PIN, LOW);
    digitalWrite(LED2_PIN, HIGH);
   }
}

And then, whenever you need that to happen, use the function call --

tuneTime();

For instance,

void loop() {
  int value = analogRead(A0);
  //Serial.println("Analog value : ");
  Serial.println(value);
  delay(400);

 if (value > 70)
 {
   value = analogRead(A0);
   tuneTime();     //  ← ←  !  !  !  !
 }
 else
 {
   digitalWrite(LED1_PIN, LOW);
   digitalWrite(LED2_PIN, LOW);
 }

(or maybe that's not it at all).

You are trying to say that the loop above can only be run in a multi-nested if statement?

try putting this code at the end of setup()

while (value > 70){
Serial.println( "woot wooty woot, I do not need to be in a multi-level nested if statement to run this loop" );
          value = analogRead(A0);
          delay(700);
          }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.