Ajout de la détection de présence par infrarouge

This commit is contained in:
Pierrick C 2017-10-08 14:58:13 +02:00
parent c352580e8d
commit a20cff51d9

View File

@ -5,6 +5,11 @@
#include <Servo.h> #include <Servo.h>
#include <EEPROM.h> #include <EEPROM.h>
//Configuration
const float SERVO_SPEED = 0.8;
const float OPEN_ANGLE = 10.0;
const float CLOSED_ANGLE = 170.0;
//Pin definitions //Pin definitions
#define RED_LED 12 #define RED_LED 12
#define GREEN_LED 13 #define GREEN_LED 13
@ -13,11 +18,13 @@
#define PROG_BUTTON 4 #define PROG_BUTTON 4
#define SERVO_CTRL 5 #define SERVO_CTRL 5
#define DOOR_BUTTON 6 #define DOOR_BUTTON 6
#define IR_LED 7
#define IR_RECEIVE A0
// Others parameters // Others parameters
#define SERVO_SPEED 2
#define SERVO_PWR_TIME 500 //time to stop powering servomotor after the end of move #define SERVO_PWR_TIME 500 //time to stop powering servomotor after the end of move
#define DOOR_OPENED_TIME 3000 #define DOOR_OPENED_TIME 3000
#define IR_DETECTION_THRESHOLD 400
#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}
@ -28,7 +35,7 @@ Servo door_servo;
int data1 = 0; int data1 = 0;
int tag_ok = -1; int tag_ok = -1;
byte servo_pos = 180; float servo_pos = OPEN_ANGLE;
bool door_state = 0; // door opened ? bool door_state = 0; // door opened ?
bool door_move = false; // door moving ? bool door_move = false; // door moving ?
long door_timer = 0; // timer to close the door long door_timer = 0; // timer to close the door
@ -58,9 +65,9 @@ byte * readeepromtag(short tagnb=0) {
tag[i]=EEPROM.read(i + tagnb*14); tag[i]=EEPROM.read(i + tagnb*14);
//delay(1); //small delay to avoid misreading //delay(1); //small delay to avoid misreading
} }
//Serial.print("Tag nb "); //Serial.print(F("Tag nb "));
//Serial.print(tagnb); //Serial.print(tagnb);
//Serial.print(" : "); //Serial.print(F(" : "));
//for (int z = 0; z < 14 ; z++) { //for (int z = 0; z < 14 ; z++) {
// Serial.print(tag[z]); // Serial.print(tag[z]);
// Serial.print(","); // Serial.print(",");
@ -108,9 +115,9 @@ void tagOK() {
} }
Serial.println(F(" : Accepted")); Serial.println(F(" : Accepted"));
if (!door_state) { if (!door_state) {
Serial.println("Ouverture..."); Serial.println(F("Ouverture..."));
door_state = 1; door_state = 1;
//door_timer = millis() + 1000; door_timer = millis() + DOOR_OPENED_TIME;
} }
} }
else else
@ -180,22 +187,22 @@ void updateDoor() {
//Apply door status //Apply door status
if (door_state) { if (door_state) {
digitalWrite(GREEN_LED, HIGH); //Opening or open
digitalWrite(RED_LED, LOW); if (servo_pos <= OPEN_ANGLE) {
if (servo_pos >= 180) {
//if door has to be open and already open, disable servomotor //if door has to be open and already open, disable servomotor
disableServo();
if (door_move) { if (door_move) {
//Opening
Serial.println(F("Door : opened")); Serial.println(F("Door : opened"));
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();
door_move = false; door_move = false;
} }
} }
else { else {
//Keep opening the door //Keep opening the door
door_move = true; door_move = 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);
//Serial.print(F("Door : opening ")); //Serial.print(F("Door : opening "));
@ -213,21 +220,20 @@ void updateDoor() {
} }
} }
else { else {
digitalWrite(GREEN_LED, LOW); if (servo_pos >= CLOSED_ANGLE) {
if (servo_pos <= 0) {
//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_move) {
Serial.println(F("Door : closed")); Serial.println(F("Door : closed"));
digitalWrite(RED_LED, LOW);
servo_timer = millis() + SERVO_PWR_TIME; servo_timer = millis() + SERVO_PWR_TIME;
disableServo();
door_move = false; door_move = false;
} }
} }
else { else {
//Keep closing the door //Keep closing the door
door_move = true; door_move = 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);
//Serial.print(F("Door : closing ")); //Serial.print(F("Door : closing "));
@ -236,7 +242,18 @@ void updateDoor() {
} }
} }
void updateRedLED() { void updateLED() {
//Update the LEDs state
if (door_state) {
//Opening or open
digitalWrite(GREEN_LED, HIGH);
digitalWrite(RED_LED, LOW);
}
else {
digitalWrite(GREEN_LED, LOW);
digitalWrite(RED_LED, LOW);
}
//Check the redLED timer and turn ON or OFF the LED //Check the redLED 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);
@ -304,7 +321,7 @@ void delTag(byte tag[14]) {
Serial.println(""); Serial.println("");
int tagnb = findtag(tag); int tagnb = findtag(tag);
Serial.print("Tag number "); Serial.print(F("Tag number "));
Serial.println(tagnb); Serial.println(tagnb);
Serial.print(F("Writing zeros...")); Serial.print(F("Writing zeros..."));
@ -331,6 +348,30 @@ void delTag(byte tag[14]) {
} }
} }
boolean catIsHere() {
int IR_ambient; // variable to store the IR coming from the ambient
int value; // variable to store the IR values
for (int i=0; i<5; i++) {
digitalWrite(IR_LED, LOW);
delay(1);
IR_ambient = analogRead(IR_RECEIVE);
digitalWrite(IR_LED, HIGH);
delay(1);
value += IR_ambient - analogRead(IR_RECEIVE);
}
Serial.println(value);
digitalWrite(IR_LED, LOW);
if (value < IR_DETECTION_THRESHOLD) {
Serial.println(F("Chat présent"));
return true;
}
Serial.println(F("Chat absent"));
return false;
}
void setup() { void setup() {
// start serial to PC // start serial to PC
Serial.begin(115200); Serial.begin(115200);
@ -345,6 +386,8 @@ void setup() {
// for status LEDs // for status LEDs
pinMode(GREEN_LED, OUTPUT); pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT); pinMode(RED_LED, OUTPUT);
pinMode(IR_LED, OUTPUT);
digitalWrite(IR_LED, LOW);
//Mode for button and end-stop //Mode for button and end-stop
pinMode(PROG_BUTTON, INPUT_PULLUP); pinMode(PROG_BUTTON, INPUT_PULLUP);
@ -357,13 +400,18 @@ 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
while (!door_move) {
updateDoor();
}
door_state = 0;
} }
void loop() { void loop() {
tag_ok = -1; tag_ok = -1;
if (RFID.available() > 0 and !door_state ) { if (!door_state and !door_move and RFID.available() > 0 ) {
readRFID(); readRFID();
} }
@ -371,8 +419,13 @@ void loop() {
Serial.println("Ouverture par l'utilisateur."); Serial.println("Ouverture par l'utilisateur.");
door_state = 1; door_state = 1;
} }
if (door_state and !door_move and catIsHere()) {
door_timer = millis() + DOOR_OPENED_TIME;
}
updateDoor(); updateDoor();
updateRedLED(); updateLED();
delay(10); delay(10);
} }