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!!!