Browse Source

- Début de code arduino avec :

- lecture de RFID,
  - gestion des LEDs,
  - gestion du moteur,
  - lecture de l'EEPROM
master
arofarn 5 years ago
parent
commit
2aefaef6c3
  1. 0
      LICENSE
  2. 0
      Logigramme.dia
  3. 0
      README.md
  4. 0
      code/.gitkeep
  5. 300
      code/cat_on_diet_feeder/cat_on_diet_feeder.ino
  6. 0
      images/.gitkeep
  7. 0
      images/schema_synoptique.png
  8. 0
      plans/boitier-concept.fcstd
  9. 0
      plans/boitier-proto.fcstd

0
LICENSE

0
Logigramme.dia

0
README.md

0
code/.gitkeep

300
code/cat_on_diet_feeder/cat_on_diet_feeder.ino

@ -0,0 +1,300 @@
// Source : http://tronixstuff.com/2013/11/19/arduino-tutorials-chapter-15-rfid/
// with just a mod to print the tag nb before "Accepted"/"Rejected"
#include <SoftwareSerial.h>
#include <Servo.h>
#include <EEPROM.h>
//Pin definitions
#define RED_LED 12
#define GREEN_LED 13
#define RFID_RX 2
#define RFID_TX 3
#define PROG_BUTTON 4
#define SERVO_CTRL 5
// Others parameters
#define SERVO_SPEED 2
#define SERVO_PWR_TIME 500 //time to stop powering servomotor after the end of move
#define DOOR_OPENED_TIME 3000
#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}
//Globals declarations
SoftwareSerial RFID(RFID_RX, RFID_TX); // RX and TX
Servo door_servo;
int data1 = 0;
int tag_ok = -1;
byte servo_pos = 180;
bool door_state = 0; // door opened ?
bool door_move = false; // door moving ?
long door_timer = 0; // timer to close the door
long servo_timer = 0; //timer to stop powering servomotor
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
const byte emptytag[14] = EMPTY_TAG;
const byte unwrittentag[14] = UNWRITTEN_TAG;
byte * readeepromtag(short tagnb=0) {
// Read the n-th RFID tag in EEPROM
//First check if the # of tag is out of the capacity of EEPROM
//Return an null tag if so.
if ((tagnb+1)*14 > EEPROM.length()) {
Serial.print(F("Error: Tag Nb to high : "));
Serial.println(tagnb);
byte tag[14] = EMPTY_TAG;
return tag;
}
byte tag[14] = EMPTY_TAG;
for (int i=0; i<14; i++)
{
tag[i]=EEPROM.read(i + tagnb*14);
delay(2); //small delay to avoid misreading
}
return tag;
}
int maxeepromtags() {
// Return maximum number of tag that can be stored in EEPROM
return EEPROM.length() / 14;
}
boolean comparetag(byte taga[14], byte tagb[14]) {
// Compare two RFID tag
int x = 0;
for (int i = 0 ; i < 14 ; i++)
{
if (taga[i] == tagb[i]) x++;
}
return (x == 14);
}
int findtag(byte searchtag[14]) {
//Find a given tag in EEPROM
byte *tag;
for (int i=0; i < maxeepromtags(); i++)
{
tag = readeepromtag(i);
if (comparetag(tag, searchtag)) return i;
}
return -1;
}
void setup() {
// start serial to PC
Serial.begin(115200);
// start serial to RFID reader
RFID.begin(9600);
// empty the data cache
while (RFID.available()) {
RFID.read();
}
//attaches servo
door_servo.attach(SERVO_CTRL);
// for status LEDs
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
//Mode for button and end-stop
pinMode(PROG_BUTTON, INPUT_PULLUP);
Serial.print(F("EEPROM length : "));
Serial.print(EEPROM.length());
Serial.print(F(" bytes ("));
Serial.print(maxeepromtags());
Serial.println(F(" tags)"));
Serial.println(findtag(newtag));
Serial.println(findtag(unwrittentag));
for(byte n=0; n<maxeepromtags(); n++)
{
Serial.print(F("Tag n°"));
Serial.print(n);
Serial.print(": ");
byte *t;
t=readeepromtag(n);
for (int z = 0; z < 14 ; z++)
{
Serial.print(t[z]);
Serial.print(",");
}
Serial.println("");
}
}
void checkmytags() {
// compares each tag against the tag just read
// if it is 1 or more we have a match, zero is a read but no match,
// -1 is no read attempt made
if (comparetag(newtag, tag1)) tag_ok=1;
}
void tagOK() {
if (digitalRead(PROG_BUTTON))
{
for (int z = 0; z < 14 ; z++) Serial.print(newtag[z]);
Serial.println(F(" : Accepted"));
if (!door_state) {
Serial.println("Ouverture...");
door_state = 1;
//door_timer = millis() + 1000;
}
}
else
{
addNewTag(newtag);
}
}
void tagNoOK() {
if (digitalRead(PROG_BUTTON)) {
for (int z = 0; z < 14 ; z++) Serial.print(newtag[z]);
Serial.println(F(" : Rejected"));
digitalWrite(RED_LED, HIGH);
//Light the red LED for 1s
red_led_timer = millis() + 1000;
}
else {
delTag(newtag);
}
}
void readRFID() {
tag_ok = 0;
delay(100); // time for the data to come in from the serial buffer.
//Check up to 3 times if rejected to avoid mistake
for (byte i=0; i<3; i++) {
// read tag numbers
for (byte z = 0 ; z < 14 ; z++) // read the rest of the tag
{
data1 = RFID.read();
newtag[z] = data1;
}
RFID.flush(); // stops multiple reads
// do the tags match up?
checkmytags();
if (tag_ok>0) break;
}
// now do something based on tag type
if (tag_ok > 0) tagOK();
else tagNoOK();
// empty the data cache
while (RFID.available()) {
RFID.read();
}
}
void disableServo() {
//Disable servomotor once they stop moving
if (!door_move and servo_timer < millis() and door_servo.attached()) {
door_servo.detach();
Serial.println(F("Servo disabled"));
}
}
void updateDoor() {
//Check if the need to move and move it
//Apply door status
if (door_state) {
digitalWrite(GREEN_LED, HIGH);
digitalWrite(RED_LED, LOW);
if (servo_pos >= 180) {
//if door has to be open and already open, disable servomotor
disableServo();
if (door_move) {
Serial.println(F("Door : opened"));
door_timer = millis() + DOOR_OPENED_TIME;
servo_timer = millis() + SERVO_PWR_TIME;
door_move = false;
}
}
else {
//Keep opening the door
door_move = true;
servo_pos += SERVO_SPEED;
door_servo.attach(SERVO_CTRL);
door_servo.write(servo_pos);
Serial.print(F("Door : opening "));
Serial.println(servo_pos);
}
//Check opened-door timer
if (millis() < door_timer)
{
Serial.print(F("Door timer :"));
Serial.println(door_timer - millis());
}
else if (!door_move)
{
door_state = 0;
}
}
else {
digitalWrite(GREEN_LED, LOW);
if (servo_pos <= 0) {
//if door has to be open and already open, disable servomotor
disableServo();
if (door_move) {
Serial.println(F("Door : closed"));
digitalWrite(RED_LED, LOW);
servo_timer = millis() + SERVO_PWR_TIME;
door_move = false;
}
}
else {
//Keep closing the door
door_move = true;
servo_pos -= SERVO_SPEED;
door_servo.attach(SERVO_CTRL);
door_servo.write(servo_pos);
Serial.print(F("Door : closing "));
Serial.println(servo_pos);
}
}
}
void updateRedLED() {
//Check the redLED timer and turn ON or OFF the LED
if (millis() < red_led_timer) {
digitalWrite(RED_LED, HIGH);
Serial.print(F("Red LED timer : "));
Serial.println(red_led_timer - millis());
}
else digitalWrite(RED_LED, LOW);
}
void addNewTag(byte tag[14]) {
Serial.print(F("Adding new tag to EEPROM : "));
for (int z = 0; z < 14 ; z++) Serial.print(tag[z]);
}
void delTag(byte tag[14]) {
Serial.print(F("Deleting tag from EEPROM : "));
for (int z = 0; z < 14 ; z++) Serial.print(tag[z]);
}
void loop() {
tag_ok = -1;
if (RFID.available() > 0 and !door_state ) readRFID();
updateDoor();
updateRedLED();
delay(10);
}

0
images/.gitkeep

0
images/schema_synoptique.png

Before

Width: 1319  |  Height: 669  |  Size: 91 KiB

After

Width: 1319  |  Height: 669  |  Size: 91 KiB

0
plans/boitier-concept.fcstd

0
plans/boitier-proto.fcstd

Loading…
Cancel
Save