

Code: Alles auswählen
/*
ATTiny PB3=Eingang RFID
ATTiny PB1=Ausgang Relais
ATTiny PB0=Ausgang LED
ProgrammMode = LED AN
Slave1 programmiert 1xBlinken (Freq 1Sek)
Slave2 programmiert 2xBlinken (Freq 1Sek)
Slave3 programmiert 3xBlinken (Freq 1Sek)
Speicher voll: 3xBlinken (Freq 300ms)
Zwei Ausgänge sind über, eventuell OLED als Anzeige
Aktueller Stromverbrauch gemessen mit Multimeter Metex M3900: 0,040 A
Ohne Sleepfunktion
*/
#include <EEPROM.h>
#include "ReceiveOnlySoftwareSerial.h"
ReceiveOnlySoftwareSerial RFID = ReceiveOnlySoftwareSerial(PB3);
byte buffer[100];
byte RFID_Master[10] = {'0', '2', '0', '0', '6', 'E', '2', 'F', 'E', '0'}; //the master RFID fob (key) to look for
byte RFID_Slave1[10], RFID_Slave2[10], RFID_Slave3[10];
byte i;
byte i2;
byte checkPosition;
boolean RFID_Master_Correct;
boolean RFID_Slave1_Correct, RFID_Slave2_Correct, RFID_Slave3_Correct;
boolean RFID_SaveNextRead;
const int PIN_RELAIS = 1;
const int PIN_LED = 0;
void setup() {
RFID.begin(9600);
pinMode(PIN_RELAIS, OUTPUT);
pinMode(PIN_LED, OUTPUT);
digitalWrite(PIN_RELAIS, LOW);
digitalWrite(PIN_LED, LOW);
EEPROM_Read_Slaves(); //Varible used to read EEPROM
i = 1; //sets the varible to 1, and thereby skip the start byte (0x0A)
}
void loop() { //the main loop
// Seriel data
while (RFID.available() > 0) { //check if the RFID reader sends anything
// Serial.println(buffer[0]);
if (buffer[0] != 2) { //check if it the start bit is 0x0A
buffer[0] = RFID.read(); //write bit to buffer
} else {
//Modtag
buffer[i] = RFID.read(); //write next bit to buffer
if (buffer[i] == 3) { //if end bit is send, disable the RFID reader temporary
RFID.end();
RFID_Master_Correct = true;
RFID_Slave1_Correct = true;
RFID_Slave2_Correct = true;
RFID_Slave3_Correct = true;
//RFID
// We have read all bytes - we are now going to check them
for (checkPosition = 0; checkPosition < 10; checkPosition++) {
if (buffer[checkPosition + 1] == RFID_Slave1[checkPosition] && RFID_Slave1_Correct == true) { // compares the written bits to "RFID1"
RFID_Slave1_Correct = true; //Slave1 RFID tag is detected
} else {
RFID_Slave1_Correct = false; //Slave1 RFID tag is not detected
}
if (buffer[checkPosition + 1] == RFID_Slave2[checkPosition] && RFID_Slave2_Correct == true) { // compares the written bits to "RFID2"
RFID_Slave2_Correct = true; //Slave2 RFID tag is detected
} else {
RFID_Slave2_Correct = false; //Slave2 RFID tag is not detected
}
if (buffer[checkPosition + 1] == RFID_Slave3[checkPosition] && RFID_Slave3_Correct == true) { // compares the written bits to "RFID3"
RFID_Slave3_Correct = true; //Slave3 RFID tag is detected
} else {
RFID_Slave3_Correct = false; //Slave3 RFID tag is detected
}
//Master
if (buffer[checkPosition + 1] == RFID_Master[checkPosition] && RFID_Master_Correct == true) { // compares the written bits to "Master"
RFID_Master_Correct = true; //Master RFID tag is detected
} else {
RFID_Master_Correct = false; //Master RFID tag is detected
}
}
if (RFID_SaveNextRead == false && (RFID_Slave1_Correct == true || RFID_Slave2_Correct == true || RFID_Slave3_Correct == true) && RFID_Master_Correct == false) { //see if the right RFID fob (key) is detected
digitalWrite(PIN_RELAIS, HIGH);
delay(1000);
digitalWrite(PIN_RELAIS, LOW);
delay(1000); // Wait for you to remove the RFID fob (key)
} else if (RFID_Master_Correct == true && RFID_SaveNextRead == false) { // If the Master Card is scanned when not in programming mode
delay(1000);
RFID_SaveNextRead = true; // Enable programming mode
digitalWrite(PIN_LED, HIGH); // PROGRAMM MODE
} else if (RFID_Master_Correct == false && RFID_SaveNextRead == true) { // If another card is scanned when in programming mode
// Save the Card
if (RFID_Slave1[0] == 0) { // Is the Slave1 Card slot empty?
for (checkPosition = 0; checkPosition < 10; checkPosition++) { //Read bit fra 0-9
RFID_Slave1[checkPosition] = buffer[checkPosition + 1]; // Save the scanned card as Slave1
}
digitalWrite(PIN_LED, LOW);
delay(1000);
digitalWrite(PIN_LED, HIGH);
delay(1000);
digitalWrite(PIN_LED, LOW);
delay(1000);
} else if (RFID_Slave2[0] == 0) { // Is the Slave2 Card slot empty?
for (checkPosition = 0; checkPosition < 10; checkPosition++) { //Read bit fra 0-9
RFID_Slave2[checkPosition] = buffer[checkPosition + 1]; // Save the scanned card as Slave2
}
digitalWrite(PIN_LED, LOW);
delay(1000);
digitalWrite(PIN_LED, HIGH);
delay(1000);
digitalWrite(PIN_LED, LOW);
delay(1000);
digitalWrite(PIN_LED, HIGH);
delay(1000);
digitalWrite(PIN_LED, LOW);
delay(1000);
} else if (RFID_Slave3[0] == 0) { // Is the Slave3 Card slot empty?
for (checkPosition = 0; checkPosition < 10; checkPosition++) { //Read bit fra 0-9
RFID_Slave3[checkPosition] = buffer[checkPosition + 1]; // Save the scanned card as Slave3
}
digitalWrite(PIN_LED, LOW);
delay(1000);
digitalWrite(PIN_LED, HIGH);
delay(1000);
digitalWrite(PIN_LED, LOW);
delay(1000);
digitalWrite(PIN_LED, HIGH);
delay(1000);
digitalWrite(PIN_LED, LOW);
delay(1000);
digitalWrite(PIN_LED, HIGH);
delay(1000);
digitalWrite(PIN_LED, LOW);
delay(1000);
} else {
digitalWrite(PIN_LED, LOW);
delay(300);
digitalWrite(PIN_LED, HIGH);
delay(300);
digitalWrite(PIN_LED, LOW);
delay(300);
digitalWrite(PIN_LED, HIGH);
delay(300);
digitalWrite(PIN_LED, LOW);
delay(300);
digitalWrite(PIN_LED, HIGH);
delay(300);
digitalWrite(PIN_LED, LOW);
}
EEPROM_Save_Slaves();
RFID_SaveNextRead = false;
//Master
} else if (RFID_Master_Correct == true && RFID_SaveNextRead == true) { // If the Master Card is scanned when in programming mode
delay(1000);
// Remove all Slave Cards
for (checkPosition = 0; checkPosition < 10; checkPosition++) { //Read bit fra 0-9
RFID_Slave1[checkPosition] = 0;
}
for (checkPosition = 0; checkPosition < 10; checkPosition++) { //Read bit fra 0-9
RFID_Slave2[checkPosition] = 0;
}
for (checkPosition = 0; checkPosition < 10; checkPosition++) { //Read bit fra 0-9
RFID_Slave3[checkPosition] = 0;
}
EEPROM_Save_Slaves();
RFID_SaveNextRead = false;
digitalWrite(PIN_LED, LOW);
}
RFID.begin(9600);
EmptySerialBuffer(); //erase the buffer
}
i++; //used in the beginning to write to each bit in the buffer
}
}
}
void EmptySerialBuffer() { //replaces all bits in the buffer with zeros
while (RFID.available()) {
RFID.read();
}
for (i2 = 0; i2 <= i; i2++) {
buffer[i2] = 0;
}
i = 0;
}
void EEPROM_Read_Slaves() {
byte EPROMaddr;
for (EPROMaddr = 0; EPROMaddr < 10; EPROMaddr++) { //Read bit from 0-9
RFID_Slave1[EPROMaddr] = EEPROM.read(EPROMaddr);
}
for (EPROMaddr = 10; EPROMaddr < 20; EPROMaddr++) { //Read bit from 0-9
RFID_Slave2[EPROMaddr - 10] = EEPROM.read(EPROMaddr);
}
for (EPROMaddr = 20; EPROMaddr < 30; EPROMaddr++) { //Read bit from 0-9
RFID_Slave3[EPROMaddr - 20] = EEPROM.read(EPROMaddr);
}
}
void EEPROM_Save_Slaves() {
byte EPROMaddr;
for (EPROMaddr = 0; EPROMaddr < 10; EPROMaddr++) { //Read bit from 0-9
EEPROM.write(EPROMaddr, RFID_Slave1[EPROMaddr]);
}
for (EPROMaddr = 10; EPROMaddr < 20; EPROMaddr++) { //Read bit from 0-9
EEPROM.write(EPROMaddr, RFID_Slave2[EPROMaddr - 10]);
}
for (EPROMaddr = 20; EPROMaddr < 30; EPROMaddr++) { //Read bit from 0-9
EEPROM.write(EPROMaddr, RFID_Slave3[EPROMaddr - 20]);
}
}