I have a small project with an RFID module and a servo,
When the card Pass the RFID moves the Servo , if there is no activity for 10 seconds the arduino goes to sleep mode.
the problem is that i can't woke up the arduino with interrupt using the rfid
/**
* Read a card using a mfrc522 reader on your SPI interface
* Pin layout should be as follows (on Arduino Uno):
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK: Pin 13 / ISCP-3
* SS: Pin 10
* RST: Pin 9
*
* Script is based on the script of Miguel Balboa.
* New cardnumber is printed when card has changed. Only a dot is printed
* if card is the same.
*
* @version 0.1
* @author Henri de Jong
* @since 06-01-2013
*/
/*
Pin change interrupts.
Pin Mask / Flag / Enable
D0 PCINT16 (PCMSK2 / PCIF2 / PCIE2)
D1 PCINT17 (PCMSK2 / PCIF2 / PCIE2)
D2 PCINT18 (PCMSK2 / PCIF2 / PCIE2)
D3 PCINT19 (PCMSK2 / PCIF2 / PCIE2)
D4 PCINT20 (PCMSK2 / PCIF2 / PCIE2)
D5 PCINT21 (PCMSK2 / PCIF2 / PCIE2)
D6 PCINT22 (PCMSK2 / PCIF2 / PCIE2)
D7 PCINT23 (PCMSK2 / PCIF2 / PCIE2)
D8 PCINT0 (PCMSK0 / PCIF0 / PCIE0)
D9 PCINT1 (PCMSK0 / PCIF0 / PCIE0)
D10 PCINT2 (PCMSK0 / PCIF0 / PCIE0)
D11 PCINT3 (PCMSK0 / PCIF0 / PCIE0)
D12 PCINT4 (PCMSK0 / PCIF0 / PCIE0)
D13 PCINT5 (PCMSK0 / PCIF0 / PCIE0)
A0 PCINT8 (PCMSK1 / PCIF1 / PCIE1)
A1 PCINT9 (PCMSK1 / PCIF1 / PCIE1)
A2 PCINT10 (PCMSK1 / PCIF1 / PCIE1)
A3 PCINT11 (PCMSK1 / PCIF1 / PCIE1)
A4 PCINT12 (PCMSK1 / PCIF1 / PCIE1)
A5 PCINT13 (PCMSK1 / PCIF1 / PCIE1)
*/
#include <SPI.h>
#include <RFID.h>
#include <avr/sleep.h>
#include <avr/power.h>
int interrupt = 12; // hardware interrupt, Motion Detector
unsigned long awake_time = 0; // capture time when wake up from sleep
unsigned long elapsed_time = 0; // time that have been awake for
#define SS_PIN 10
#define RST_PIN 9
#define TAG_LEN 5
RFID rfid(SS_PIN, RST_PIN);
byte target_tag[TAG_LEN] = {0xC4, 0xB1, 0x2A, 0xEB, 0xB4};
byte target_tag2[TAG_LEN] = {0x44, 0xF5, 0x1B, 0xC9, 0x63};
// Setup variables:
int serNum0;
int serNum1;
int serNum2;
int serNum3;
int serNum4;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0;
void setup()
{
PCICR |= (1<<PCIE0);
//PCMSK0 &= ~(1<<PCINT0);
//PCMSK0 |= (1<<PCINT1);
//PCMSK0 |= (1<<PCINT2);
//PCICR |= (1 << PCIE2);
// PCMSK0 |= _BV (PCINT5); // pin 6
// PCICR |= (1 << PCIE0);
// PCMSK0 |= (1 << PCINT3)| (1<<PCINT2)| (1<<PCINT5)| (1<<PCINT4);
//PCMSK0 |= (1 << PCINT3) ;
//PCMSK0 |= (1<<PCINT2) ;
// PCMSK0 |= (1<<PCINT3) ;
//PCMSK0 |= (1<<PCINT4);
myservo.attach(5);
Serial.begin(9600);
SPI.begin();
rfid.init();
}
void loop()
{
dowork();
elapsed_time = millis() - awake_time; // see if 10 seconds gone by
//Serial.println (elapsed_time);
if (elapsed_time >=5000){
// allOff(); // turn all light off
// delay (1000); // time to walk away
enterSleep(); // call Sleep function to put us out
// THE PROGRAM CONTINUES FROM HERE after waking up in enterSleep()
awake_time = millis(); // capture the time we woke up
}
}
void dowork()
{
if (rfid.isCard()) {
if (rfid.readCardSerial()) {
if (rfid.serNum[0] != serNum0
&& rfid.serNum[1] != serNum1
&& rfid.serNum[2] != serNum2
&& rfid.serNum[3] != serNum3
&& rfid.serNum[4] != serNum4
) {
/* With a new cardnumber, show it. */
Serial.println(" ");
Serial.println("Card found");
serNum0 = rfid.serNum[0];
serNum1 = rfid.serNum[1];
serNum2 = rfid.serNum[2];
serNum3 = rfid.serNum[3];
serNum4 = rfid.serNum[4];
} else {
/* If we have the same ID, just write a dot. */
// Serial.print(".");
}
int i ;
for (i=0; i<5; i++) {
// if (rfid.serNum[i] < 16) { Serial.print("0");}
byte master1 = rfid.serNum[i];
Serial.print(rfid.serNum[i], HEX);
Serial.print(" ");
//Serial.println();
}
if(memcmp(rfid.serNum, target_tag, TAG_LEN) == 0 || memcmp(rfid.serNum, target_tag2, TAG_LEN) == 0 )
{
Serial.println("This Card is a Match!");
myservo.write(150);
awake_time = millis();
delay(2500);
awake_time = millis();
myservo.write(50);
}
}
}
rfid.halt();
}
void Interrupt(void)
{
detachInterrupt(0);
}
void enterSleep(void)
{
myservo.write(50);
/* Setup pin2 as an interrupt and attach handler. */
Serial.println("good night");
attachInterrupt(0, Interrupt, HIGH);
delay(100);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
/* The program will continue from here. */
/* First thing to do is disable sleep. */
sleep_disable();
}