Hi,
I'm new in this forum, and I'm developing a Vault controled by Arduino. I've the all code made, but I've a problem on this. I'm a begginer and I´don´t know how to close its door, I´ve tried with a push button but it doesn't work, and now I'm trying with variables but I continue not solving my problem: close the door (turning 100º with a SG90 Servo) after open it with RFID Cards. In one code (button one) door opens but don't close; in the other code (variables one) door opens and closes after 3 seconds automatically.
al
The 2 codes are attached.
Does someone knows how to solve it?
Many Thanks
Code with button:
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
#include <Servo.h>
Servo mm;
void setup()
{
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Approximate your card to the reader...");
Serial.println();
mm.attach(2);
mm.write(10);
pinMode(4,INPUT);
pinMode(7,OUTPUT); // Green LED
digitalWrite(7,LOW);
pinMode(5,OUTPUT);// Red LED
digitalWrite(5,LOW);
pinMode(6,OUTPUT); // Orange LED - Power Supply
digitalWrite(6,HIGH);
delay(3000);
digitalWrite(6,LOW);
}
void loop()
{
// 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) == "4C 8E 7F 2B" || content.substring(1) == "A6 9F DC A0") //change here the UID of the card/cards that you want to give access
{
mm.write(100);
Serial.println("Authorized access");
Serial.println();
digitalWrite(7,HIGH);
delay(3000);
digitalWrite(7,LOW);
}
else {
Serial.println(" Access denied");
digitalWrite(5,HIGH);
delay(2000);
digitalWrite(5,LOW);
}
if (digitalRead(4) == HIGH) // If button is pressed - SO - close the locker and blink the Red LED
{
mm.write(10);
digitalWrite(15,HIGH);
delay(3000);
digitalWrite(15,LOW);
}
}
Code with variables
#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
Servo mm;
int a = 0; // 0 means closed; 1 means open, (The Door)
void setup()
{
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Approximate your card to the reader...");
Serial.println();
mm.attach(2);
mm.write(10);
pinMode(7,OUTPUT); // Green LED
digitalWrite(7,LOW);
pinMode(5,OUTPUT);// Red LED
digitalWrite(5,LOW);
pinMode(6,OUTPUT); // Orange LED - Power Supply, only blinks at the beggining
digitalWrite(6,HIGH);
delay(3000);
digitalWrite(6,LOW);
}
void loop()
{
// 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) == ("4C 8E 7F 2B")) || (content.substring(1) == ("A6 9F DC A0")) && (a = 0)) //If one of the 2 keys registed were detected and the door is closed, THEN modify its state to open and open it
{
mm.write(100);
Serial.println("Authorized access");
Serial.println();
digitalWrite(7,HIGH);
delay(3000);
digitalWrite(7,LOW);
int a = 1;
}
else { // Otherwise dont open the door
Serial.println(" Access denied / Door Closed");
digitalWrite(5,HIGH);
delay(2000);
digitalWrite(5,LOW);
}
if ((content.substring(1) == ("4C 8E 7F 2B")) || (content.substring(1) == ("A6 9F DC A0")) && (a = 1)) //If one of the 2 keys registed were detected and the door is open, THEN modify its state to close and close it
{
mm.write(10);
Serial.println("Authorized access");
Serial.println();
digitalWrite(7,HIGH);
delay(3000);
digitalWrite(7,LOW);
int a = 0;
}
}