Hi I am new to the forum and also pretty new to Arduino and Programming.
Since I started I learned a lot but as a newbie I have my problems.
I recreated a RFID doorlock with my own addons like a display which opens the door to my flat.
I am using a relay to open the door and as I used a dual relay module I also thought it would be a good idea to be able to switch on and of the venting and light in my toilet. Reasons are that I am living in an 250 year old house and power outlets are short, I want to change the lightswitch with an outlet and add a push button on the case I mounted next to the door which houses the arduino and the relays.
I merged the codes and it compiled just fine, but for some reason Iam only able to turn the relay on and off when the RFID is active ( holding the tag to the reader so the electric doorlock opens)
I am not sure whats the problem so I hope you guys can help me, sorry for my bad english.
The whole RFID stuff works like a charm, only the added button switch stuff isnt working as it should.
//Arduino RFID Access Control
// adding a Pushbutton for the toilett light and ventilation
int pinButton = 8;
int Relay = 2;
int stateRelay = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 500;
#include <Wire.h>
#include <EEPROM.h> // We are going to read and write PICC's UIDs from/to EEPROM
#include <SPI.h> // RC522 Module uses SPI protocol
#include <MFRC522.h> // Library for Mifare RC522 Devices
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // added a lcd and works just fine
#define COMMON_ANODE
#ifdef COMMON_ANODE
#define LED_ON LOW
#define LED_OFF HIGH
#else
#define LED_ON HIGH
#define LED_OFF LOW
#endif
#define redLed 7 // Set Led Pins
#define greenLed 6
#define blueLed 5
#define relay 4 // Set Relay Pin
#define wipeB 3 // Button pin for WipeMode
boolean match = false; // initialize card match to false
boolean programMode = false; // initialize programming mode to false
int successRead; // Variable integer to keep if we have Successful Read from Reader
byte storedCard[4]; // Stores an ID read from EEPROM
byte readCard[4]; // Stores scanned ID read from RFID Module
byte masterCard[4]; // Stores master card's ID read from EEPROM
/*
We need to define MFRC522's pins and create instance
Pin layout should be as follows (on Arduino Uno):
MOSI: Pin 11 / ICSP-4
MISO: Pin 12 / ICSP-1
SCK : Pin 13 / ICSP-3
SS : Pin 10 (Configurable)
RST : Pin 9 (Configurable)
look MFRC522 Library for
other Arduinos' pin configuration
*/
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
///////////////////////////////////////// Setup ///////////////////////////////////
void setup() {
lcd.begin(20, 4); // LCD stuff again, works fine
lcd.noBacklight();
delay(250);
pinMode(pinButton, INPUT); //Buttonswitch stuff that wont work as it should
pinMode(Relay, OUTPUT);
//Arduino Pin Configuration
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(wipeB, INPUT_PULLUP); // Enable pin's pull up resistor
pinMode(relay, OUTPUT);
//Be careful how relay circuit behave on while resetting or power-cycling your Arduino
digitalWrite(relay, HIGH); // Make sure door is locked
digitalWrite(redLed, LED_OFF); // Make sure led is off
digitalWrite(greenLed, LED_OFF); // Make sure led is off
digitalWrite(blueLed, LED_OFF); // Make sure led is off
//Protocol Configuration
Serial.begin(9600); // Initialize serial communications with PC
SPI.begin(); // MFRC522 Hardware uses SPI protocol
mfrc522.PCD_Init(); // Initialize MFRC522 Hardware
//If you set Antenna Gain to Max it will increase reading distance
//mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
Serial.println(F("Access Control v3.3")); // For debugging purposes
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
//Wipe Code if Button Pressed while setup run (powered on) it wipes EEPROM
if (digitalRead(wipeB) == LOW) { // when button pressed pin should get low, button connected to ground
digitalWrite(redLed, LED_ON); // Red Led stays on to inform user we are going to wipe
Serial.println(F("Wipe Button Pressed"));
Serial.println(F("You have 5 seconds to Cancel"));
Serial.println(F("This will be remove all records and cannot be undone"));
delay(5000); // Give user enough time to cancel operation
if (digitalRead(wipeB) == LOW) { // If button still be pressed, wipe EEPROM
Serial.println(F("Starting Wiping EEPROM"));
for (int x = 0; x < EEPROM.length(); x = x + 1) { //Loop end of EEPROM address
if (EEPROM.read(x) == 0) { //If EEPROM address 0
// do nothing, already clear, go to the next address in order to save time and reduce writes to EEPROM
}
else {
EEPROM.write(x, 0); // if not write 0 to clear, it takes 3.3mS
}
}
Serial.println(F("EEPROM Successfully Wiped"));
digitalWrite(redLed, LED_OFF); // visualize successful wipe
delay(200);
digitalWrite(redLed, LED_ON);
delay(200);
digitalWrite(redLed, LED_OFF);
delay(200);
digitalWrite(redLed, LED_ON);
delay(200);
digitalWrite(redLed, LED_OFF);
}
else {
Serial.println(F("Wiping Cancelled"));
digitalWrite(redLed, LED_OFF);
}
}
// Check if master card defined, if not let user choose a master card
// This also useful to just redefine Master Card
// You can keep other EEPROM records just write other than 143 to EEPROM address 1
// EEPROM address 1 should hold magical number which is '143'
if (EEPROM.read(1) != 143) {
Serial.println(F("No Master Card Defined"));
Serial.println(F("Scan A PICC to Define as Master Card"));
do {
successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
digitalWrite(blueLed, LED_ON); // Visualize Master Card need to be defined
delay(200);
digitalWrite(blueLed, LED_OFF);
delay(200);
}
while (!successRead); // Program will not go further while you not get a successful read
for ( int j = 0; j < 4; j++ ) { // Loop 4 times
EEPROM.write( 2 + j, readCard[j] ); // Write scanned PICC's UID to EEPROM, start from address 3
}
EEPROM.write(1, 143); // Write to EEPROM we defined Master Card.
Serial.println(F("Master Card Defined"));
}
Serial.println(F("-------------------"));
Serial.println(F("Master Card's UID"));
for ( int i = 0; i < 4; i++ ) { // Read Master Card's UID from EEPROM
masterCard[i] = EEPROM.read(2 + i); // Write it to masterCard
Serial.print(masterCard[i], HEX);
}
Serial.println("");
Serial.println(F("-------------------"));
Serial.println(F("Everything Ready"));
Serial.println(F("Waiting PICCs to be scanned"));
cycleLeds(); // Everything ready lets give user some feedback by cycling leds
}
///////////////////////////////////////// Main Loop ///////////////////////////////////
// I did put the loop on first place so I thought it would not get in the way, but it looks like it somehow does :(
void loop () {
{
stateButton = digitalRead(pinButton);
if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
if(stateRelay == HIGH){
stateRelay = LOW;
} else {
stateRelay = HIGH;
}
time = millis();
}
digitalWrite(Relay, stateRelay);
previous == stateButton;
}
do {
successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
if (programMode) {
cycleLeds(); // Program Mode cycles through RGB waiting to read a new card
...................................
There is plenty more code but I am not allowed to post more than 9000characters :(