Add comments, translate some texts from french to english and change some bad variable names

This commit is contained in:
Pierrick C 2017-11-26 22:20:11 +01:00
parent 6fbb763d9c
commit adfdc832c3

View File

@ -9,6 +9,9 @@
const float SERVO_SPEED = 1.8; const float SERVO_SPEED = 1.8;
const float OPEN_ANGLE = 10.0; const float OPEN_ANGLE = 10.0;
const float CLOSED_ANGLE = 160.0; const float CLOSED_ANGLE = 160.0;
const float SERVO_PWR_TIME 500 //time to stop powering servomotor after the end of move
const float DOOR_OPENED_TIME 3000 //time to keep door open after last dectection on IR sensor
const float IR_DETECTION_THRESHOLD 100 //IR dectection threshold
//Pin definitions //Pin definitions
#define RED_LED 12 #define RED_LED 12
@ -21,36 +24,30 @@ const float CLOSED_ANGLE = 160.0;
#define IR_LED 7 #define IR_LED 7
#define IR_RECEIVE A0 #define IR_RECEIVE A0
// Others parameters // SPECIAL TAG DEFINITIONS
#define SERVO_PWR_TIME 500 //time to stop powering servomotor after the end of move
#define DOOR_OPENED_TIME 3000
#define IR_DETECTION_THRESHOLD 100
#define EMPTY_TAG {0,0,0,0,0,0,0,0,0,0,0,0,0,0} #define EMPTY_TAG {0,0,0,0,0,0,0,0,0,0,0,0,0,0}
#define UNWRITTEN_TAG {255,255,255,255,255,255,255,255,255,255,255,255,255,255} #define UNWRITTEN_TAG {255,255,255,255,255,255,255,255,255,255,255,255,255,255}
const byte emptytag[14] = EMPTY_TAG;
const byte unwrittentag[14] = UNWRITTEN_TAG;
//Globals declarations //Globals declarations
SoftwareSerial RFID(RFID_RX, RFID_TX); // RX and TX SoftwareSerial RFID(RFID_RX, RFID_TX); // RX and TX
Servo door_servo; Servo door_servo;
int data1 = 0; int data1 = 0;
int tag_ok = -1; int tag_ok = -1;
float servo_pos = OPEN_ANGLE; float servo_pos = OPEN_ANGLE;
bool door_state = 0; // door opened ? bool door_open = 0; // door opened ?
bool door_move = false; // door moving ? bool door_moving = false; // door moving ?
long door_timer = 0; // timer to close the door long door_timer = 0; // timer to close the door
long servo_timer = 0; //timer to stop powering servomotor long servo_timer = 0; //timer to stop powering servomotor
long red_led_timer = 0; long red_led_timer = 0;
//byte tag1[14] = {2,52,54,48,48,57,49,48,57,56,48,53,69,3};
byte newtag[14] = EMPTY_TAG; // used for read comparisons byte newtag[14] = EMPTY_TAG; // used for read comparisons
const byte emptytag[14] = EMPTY_TAG;
const byte unwrittentag[14] = UNWRITTEN_TAG;
byte * readeepromtag(short tagnb=0) { byte * readeepromtag(short tagnb=0) {
// Read the n-th RFID tag in EEPROM // Read the n-th RFID tag in EEPROM
//First check if the # of tag is out of the capacity of EEPROM //First check if the # of tag is out of the capacity of EEPROM
//Return an null tag if so. //Return an null tag if so.
if ((tagnb+1)*14 > EEPROM.length()) { if ((tagnb+1)*14 > EEPROM.length()) {
@ -60,11 +57,10 @@ byte * readeepromtag(short tagnb=0) {
return tag; return tag;
} }
byte tag[14] = EMPTY_TAG; byte tag[14] = EMPTY_TAG;
for (int i=0; i<14; i++) for (int i=0; i<14; i++) {
{
tag[i]=EEPROM.read(i + tagnb*14); tag[i]=EEPROM.read(i + tagnb*14);
//delay(1); //small delay to avoid misreading
} }
//Serial.print(F("Tag nb ")); //Serial.print(F("Tag nb "));
//Serial.print(tagnb); //Serial.print(tagnb);
//Serial.print(F(" : ")); //Serial.print(F(" : "));
@ -73,7 +69,7 @@ byte * readeepromtag(short tagnb=0) {
// Serial.print(","); // Serial.print(",");
//} //}
//Serial.println(""); //Serial.println("");
delay(2); //small delay to avoid misreading delay(2); //small delay to avoid misreading
return tag; return tag;
} }
@ -94,39 +90,47 @@ boolean comparetag(byte taga[14], byte tagb[14]) {
} }
int findtag(byte searchtag[14]) { int findtag(byte searchtag[14]) {
//Find a given tag in EEPROM //Find a given tag in EEPROM and return tag rank or -1 if not found
byte *tag; byte *tag;
for (int i=0; i < maxeepromtags(); i++) for (int i=0; i < maxeepromtags(); i++) {
{
tag = readeepromtag(i); tag = readeepromtag(i);
if (comparetag(tag, searchtag)) return i; if (comparetag(tag, searchtag)) {
return i;
}
} }
return -1; return -1;
} }
void tagOK() { void tagOK() {
// Actions when an known tag is read : Open the door or deleted the tag
// if the prog button is pressed
if (digitalRead(PROG_BUTTON)) if (digitalRead(PROG_BUTTON))
// Prog button is not pressed : open the door
{ {
for (int z = 0; z < 14 ; z++) { for (int z = 0; z < 14 ; z++) {
Serial.print(newtag[z]); Serial.print(newtag[z]);
Serial.print("."); Serial.print(".");
} }
Serial.println(F(" : Accepted")); Serial.println(F(" : Accepted"));
if (!door_state) { if (!door_open) {
Serial.println(F("Ouverture...")); Serial.println(F("Opening..."));
door_state = 1; door_open = 1;
door_timer = millis() + DOOR_OPENED_TIME; door_timer = millis() + DOOR_OPENED_TIME;
} }
} }
else else
//if prog button is pressed : delete the tag from the EEPROM
{ {
delTag(newtag); delTag(newtag);
} }
} }
void tagNoOK() { void tagNoOK() {
// Actions when an known tag is read : light the red LED for 1 sec or
// add the tag to the EEPROM if the prog button is pressed
if (digitalRead(PROG_BUTTON)) { if (digitalRead(PROG_BUTTON)) {
for (int z = 0; z < 14 ; z++) { for (int z = 0; z < 14 ; z++) {
Serial.print(newtag[z]); Serial.print(newtag[z]);
@ -142,11 +146,12 @@ void tagNoOK() {
} }
} }
void readRFID() { void readRFID() {
// Read data from RFID & check if the tag is known (i.e. in EEPROM)
tag_ok = 0; tag_ok = 0;
delay(100); // time for the data to come in from the serial buffer. delay(100); // time for the data to come in from the serial buffer.
//Check up to 3 times if rejected to avoid mistake //Check up to 3 times if rejected to avoid mistakes
for (byte i=0; i<3; i++) { for (byte i=0; i<3; i++) {
// read tag numbers // read tag numbers
for (byte z = 0 ; z < 14 ; z++) // read the rest of the tag for (byte z = 0 ; z < 14 ; z++) // read the rest of the tag
@ -156,18 +161,26 @@ void readRFID() {
} }
RFID.flush(); // stops multiple reads RFID.flush(); // stops multiple reads
// do the tag exist in EEPROM // do the tag exist in EEPROM ?
if (findtag(newtag) >= 0) tag_ok=1; if (findtag(newtag) >= 0) {
if (tag_ok>0) break; tag_ok=1;
}
if (tag_ok>0) {
break;
}
} }
//Check if the tag is not an error before doing anything //Check if the tag is not an error before doing anything
if (!comparetag(newtag, unwrittentag)) { if (!comparetag(newtag, unwrittentag)) {
// now do something based on tag type // now do something based on tag type
if (tag_ok > 0) tagOK(); if (tag_ok > 0) {
else tagNoOK(); tagOK();
}
else {
tagNoOK();
}
} }
// empty the data cache // empty the data cache
while (RFID.available()) { while (RFID.available()) {
RFID.read(); RFID.read();
@ -176,46 +189,43 @@ void readRFID() {
void disableServo() { void disableServo() {
//Disable servomotor once they stop moving //Disable servomotor once they stop moving
if (!door_move and servo_timer < millis() and door_servo.attached()) { if (!door_moving and servo_timer < millis() and door_servo.attached()) {
door_servo.detach(); door_servo.detach();
Serial.println(F("Servo disabled")); Serial.println(F("Servo disabled"));
} }
} }
void updateSerial() { void updateSerial() {
//Désactive la connexion au lecteur RFID quand la //Diconnect UART from RFID module when door is not closed to avoid unwanted
//trappe est en mouvement ou ouverte pour éviter les // small movements
// parasite if (RFID and (door_open or door_moving)) {
if (RFID and (door_state or door_move)) {
RFID.end(); RFID.end();
} }
else { else {
//Active la connexion au lecteur RFID quand la
//trappe est fermée et immobile
RFID.begin(9600); RFID.begin(9600);
} }
} }
void updateDoor() { void updateDoor() {
//Check if the need to move and move it //Check if the door need to move and move it
//Apply door status //Apply door status
if (door_state) { if (door_open) {
//Opening or open //Opening or open
if (servo_pos <= OPEN_ANGLE) { if (servo_pos <= OPEN_ANGLE) {
//if door has to be open and already open, disable servomotor //if door has to be open and already open, disable servomotor
if (door_move) { if (door_moving) {
//Opening //Opening
Serial.println(F("Door : opened")); Serial.println(F("Door : open"));
door_timer = millis() + DOOR_OPENED_TIME; door_timer = millis() + DOOR_OPENED_TIME;
servo_timer = millis() + SERVO_PWR_TIME; servo_timer = millis() + SERVO_PWR_TIME;
disableServo(); disableServo();
door_move = false; door_moving = false;
} }
} }
else { else {
//Keep opening the door //Keep opening the door
door_move = true; door_moving = true;
servo_pos -= SERVO_SPEED; servo_pos -= SERVO_SPEED;
door_servo.attach(SERVO_CTRL); door_servo.attach(SERVO_CTRL);
door_servo.write(servo_pos); door_servo.write(servo_pos);
@ -228,25 +238,25 @@ void updateDoor() {
//Serial.print(F("Door timer :")); //Serial.print(F("Door timer :"));
//Serial.println(door_timer - millis()); //Serial.println(door_timer - millis());
} }
else if (!door_move) else if (!door_moving)
{ {
door_state = 0; door_open = 0;
} }
} }
else { else {
if (servo_pos >= CLOSED_ANGLE) { if (servo_pos >= CLOSED_ANGLE) {
//if door has to be open and already open, disable servomotor //if door has to be open and already open, disable servomotor
disableServo(); disableServo();
if (door_move) { if (door_moving) {
Serial.println(F("Door : closed")); Serial.println(F("Door : closed"));
servo_timer = millis() + SERVO_PWR_TIME; servo_timer = millis() + SERVO_PWR_TIME;
disableServo(); disableServo();
door_move = false; door_moving = false;
} }
} }
else { else {
//Keep closing the door //Keep closing the door
door_move = true; door_moving = true;
servo_pos += SERVO_SPEED; servo_pos += SERVO_SPEED;
door_servo.attach(SERVO_CTRL); door_servo.attach(SERVO_CTRL);
door_servo.write(servo_pos); door_servo.write(servo_pos);
@ -258,7 +268,7 @@ void updateDoor() {
void updateLED() { void updateLED() {
//Update the LEDs state //Update the LEDs state
if (door_state) { if (door_open) {
//Opening or open //Opening or open
digitalWrite(GREEN_LED, HIGH); digitalWrite(GREEN_LED, HIGH);
digitalWrite(RED_LED, LOW); digitalWrite(RED_LED, LOW);
@ -267,8 +277,8 @@ void updateLED() {
digitalWrite(GREEN_LED, LOW); digitalWrite(GREEN_LED, LOW);
digitalWrite(RED_LED, LOW); digitalWrite(RED_LED, LOW);
} }
//Check the redLED timer and turn ON or OFF the LED //Check the red LED timer and turn ON or OFF the LED
if (millis() < red_led_timer) { if (millis() < red_led_timer) {
digitalWrite(RED_LED, HIGH); digitalWrite(RED_LED, HIGH);
// Serial.print(F("Red LED timer : ")); // Serial.print(F("Red LED timer : "));
@ -278,6 +288,7 @@ void updateLED() {
} }
void addNewTag(byte tag[14]) { void addNewTag(byte tag[14]) {
//Add a new RFID tag to the known tags in EEPROM
Serial.print(F("Adding new tag to EEPROM : ")); Serial.print(F("Adding new tag to EEPROM : "));
for (int z = 0; z < 14 ; z++) Serial.print(tag[z]); for (int z = 0; z < 14 ; z++) Serial.print(tag[z]);
Serial.println(""); Serial.println("");
@ -289,6 +300,8 @@ void addNewTag(byte tag[14]) {
tagnb = findtag(unwrittentag); tagnb = findtag(unwrittentag);
if (tagnb < 0) { if (tagnb < 0) {
Serial.println(F("No unwritten space found either. Can't save more tag.")); Serial.println(F("No unwritten space found either. Can't save more tag."));
//If there is not enough space in EEPROM to add the tag : blink the red LED
// and stop
for (int i=0; i<4; i++) { for (int i=0; i<4; i++) {
digitalWrite(RED_LED, HIGH); digitalWrite(RED_LED, HIGH);
delay(500); delay(500);
@ -299,15 +312,19 @@ void addNewTag(byte tag[14]) {
} }
} }
//Write the ne teg in EEPROM
Serial.print(F("New tag number : ")); Serial.print(F("New tag number : "));
Serial.println(tagnb); Serial.println(tagnb);
Serial.print(F("Writing...")); Serial.print(F("Writing..."));
for (int z = 0; z < 14 ; z++) { for (int z = 0; z < 14 ; z++) {
EEPROM.write(z+(tagnb*14), tag[z]); EEPROM.write(z+(tagnb*14), tag[z]);
} }
//Check the written tag
Serial.print(F(" Checking...")); Serial.print(F(" Checking..."));
int tmp = findtag(tag); int tmp = findtag(tag);
if (tmp != -1 and tmp == tagnb) { if (tmp != -1 and tmp == tagnb) {
//if OK : blink the green LED
Serial.println("OK"); Serial.println("OK");
for (int i=0; i<3; i++) { for (int i=0; i<3; i++) {
digitalWrite(GREEN_LED, HIGH); digitalWrite(GREEN_LED, HIGH);
@ -317,6 +334,7 @@ void addNewTag(byte tag[14]) {
} }
} }
else { else {
//if something's got wrong blink the RED led
Serial.println("Failed"); Serial.println("Failed");
for (int i=0; i<5; i++) { for (int i=0; i<5; i++) {
digitalWrite(RED_LED, HIGH); digitalWrite(RED_LED, HIGH);
@ -328,6 +346,7 @@ void addNewTag(byte tag[14]) {
} }
void delTag(byte tag[14]) { void delTag(byte tag[14]) {
//Delete a RFID tag from the known tags in EEPROM
Serial.print(F("Deleting tag from EEPROM : ")); Serial.print(F("Deleting tag from EEPROM : "));
for (int z = 0; z < 14 ; z++) { for (int z = 0; z < 14 ; z++) {
Serial.print(tag[z]); Serial.print(tag[z]);
@ -338,10 +357,13 @@ void delTag(byte tag[14]) {
Serial.print(F("Tag number ")); Serial.print(F("Tag number "));
Serial.println(tagnb); Serial.println(tagnb);
//Write zeros in the place of the tag
Serial.print(F("Writing zeros...")); Serial.print(F("Writing zeros..."));
for (int z = 0; z < 14 ; z++) { for (int z = 0; z < 14 ; z++) {
EEPROM.write(z+(tagnb*14), 0); EEPROM.write(z+(tagnb*14), 0);
} }
//Check if the tag is realy deleted
Serial.print(F(" Checking...")); Serial.print(F(" Checking..."));
int tmptag = readeepromtag(tagnb); int tmptag = readeepromtag(tagnb);
if (comparetag(tmptag, emptytag)) { if (comparetag(tmptag, emptytag)) {
@ -362,42 +384,45 @@ void delTag(byte tag[14]) {
} }
} }
boolean catIsHere() { boolean IR_detection() {
int IR_ambient; // variable to store the IR coming from the ambient //Detected if a cat is here with infrared barrier
int IR_ambient; // variable to store the IR coming from the ambient
int value; // variable to store the IR values int value; // variable to store the IR values
// detect 5 times in a row
for (int i=0; i<5; i++) { for (int i=0; i<5; i++) {
digitalWrite(IR_LED, LOW); digitalWrite(IR_LED, LOW);
delay(1); delay(1);
//read ambiant IR reception before activating the IR LED to avoid parasites
IR_ambient = analogRead(IR_RECEIVE); IR_ambient = analogRead(IR_RECEIVE);
digitalWrite(IR_LED, HIGH); digitalWrite(IR_LED, HIGH);
delay(1); delay(1);
value += IR_ambient - analogRead(IR_RECEIVE); value += IR_ambient - analogRead(IR_RECEIVE);
} }
Serial.println(value); Serial.println(value);
digitalWrite(IR_LED, LOW); digitalWrite(IR_LED, LOW);
if (value < IR_DETECTION_THRESHOLD) { if (value < IR_DETECTION_THRESHOLD) {
Serial.println(F("Chat présent")); Serial.println(F("Cat is here..."));
return true; return true;
} }
Serial.println(F("Chat absent")); Serial.println(F("No cat !"));
return false; return false;
} }
void setup() { void setup() {
// start serial to PC // start serial to PC
Serial.begin(115200); Serial.begin(115200);
// start serial to RFID reader // start serial to RFID reader
RFID.begin(9600); RFID.begin(9600);
// empty the data cache // empty the data cache
while (RFID.available()) { while (RFID.available()) {
RFID.read(); RFID.read();
} }
// for status LEDs // Pin mode for the LEDs
pinMode(GREEN_LED, OUTPUT); pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT); pinMode(RED_LED, OUTPUT);
pinMode(IR_LED, OUTPUT); pinMode(IR_LED, OUTPUT);
@ -414,12 +439,12 @@ void setup() {
Serial.println(F(" tags)")); Serial.println(F(" tags)"));
Serial.println(findtag(newtag)); Serial.println(findtag(newtag));
Serial.println(findtag(unwrittentag)); Serial.println(findtag(unwrittentag));
//Positioning door //Initialize door position
while (!door_move) { while (!door_moving) {
updateDoor(); updateDoor();
} }
door_state = 0; door_open = 0;
} }
void loop() { void loop() {
@ -433,7 +458,7 @@ void loop() {
//Ouverture manuelle //Ouverture manuelle
if (!digitalRead(DOOR_BUTTON)) { if (!digitalRead(DOOR_BUTTON)) {
Serial.println("Ouverture par l'utilisateur."); Serial.println("Ouverture par l'utilisateur.");
door_state = 1; door_open = 1;
door_servo.attach(SERVO_CTRL); door_servo.attach(SERVO_CTRL);
door_servo.write(OPEN_ANGLE); door_servo.write(OPEN_ANGLE);
delay(1000); delay(1000);
@ -443,16 +468,19 @@ void loop() {
door_servo.write(CLOSED_ANGLE); door_servo.write(CLOSED_ANGLE);
delay(1000); delay(1000);
Serial.println("Fermeture par l'utilisateur."); Serial.println("Fermeture par l'utilisateur.");
door_state = 0; door_open = 0;
} }
if (door_state and !door_move and catIsHere()) { // Maintien de l'ouverture pour le delais DOOR_OPENED_TIME si présence
// détecté par capteur IR
if (door_open and !door_moving and IR_detection()) {
door_timer = millis() + DOOR_OPENED_TIME; door_timer = millis() + DOOR_OPENED_TIME;
} }
//Diverses mises à jour régulières
updateSerial(); updateSerial();
updateDoor(); updateDoor();
updateLED(); updateLED();
delay(10); delay(10);
} }