Forum >[College project] System of locking / unlocking with relay shield
General

[College project] System of locking / unlocking with relay shield

userHead Deviouse 2014-03-20 05:39:58 21149 Views19 Replies

Hello :).

I'm in french college and I have a final project for my baccalaureate. I have a machine : with the relay shield v2.1 : http://www.dfrobot.com/index.php?route= ... ymaAoU3OgY , I have to create a new system for replace the switch of the machine and "create" a new "switch" with a RFID shield and the relay shield. When I pass my good swipe card on the shield RFID the machine starts. But before that I have to program the relay shield with Arduino to create this new switch and remove the old switch.
I found a code for the relay shield but I'm not sure if it will help me. I have to create a new code with the relay shield code and the rfid shield code

Code: Select all/* # This Sample code is for testing the Relay shield V2.1 for Arduino.   # Editor : Phoebe # Date   : 2013.2.28 # Ver    : 0.1 # Product: Relay shield for Arduino # SKU    : DRI0144   # Hardwares: 1. Arduino UNO 2. Relay Shield For Arduino V2.1 3  Power Supply:7~ 12V */ byte relayPin[4] = {  2,7,8,10}; //D2 -> RELAY1 //D7 -> RELAY2 //D8 -> RELAY3 //D10 -> RELAY char input=0; int val; void setup() {  for(int i = 0; i < 4; i++)  pinMode(relayPin[i],OUTPUT);  Serial.begin(57600);  delay(100);  Serial.println("Press 1-4 to control the state of the relay");  Serial.println("waiting for input:");  for(int j = 0; j < 4; j++)  digitalWrite(relayPin[j],LOW); } void loop() {  if (Serial.available())  {      char input= Serial.read();      if(input != -1)    {      switch(input)      {        case '1':                                Serial.println("Relay1");        val=digitalRead(relayPin[0]);        val=!val;        digitalWrite(relayPin[0],val);        break;      case '2':        Serial.println("Relay2");        val=digitalRead(relayPin[1]);        val=!val;        digitalWrite(relayPin[1],val);        break;      case '3':        Serial.println("Relay3");        val=digitalRead(relayPin[2]);        val=!val;        digitalWrite(relayPin[2],val);        break;      case '4':        Serial.println("Relay4");        val=digitalRead(relayPin[3]);        val=!val;        digitalWrite(relayPin[3],val);        break;                  default:        if(input != '\r' && input != '\n')         Serial.println("invalid entry");        break;      }    } //    else unablerelay();  } }

and this is the RFID code for reading the swipe cards :

Code: Select all#include <Wire.h>   #include <Adafruit_NFCShield_I2C.h>   #define IRQ  (2)   #define RESET (3) // Not connected by default on the NFC Shield   Adafruit_NFCShield_I2C nfc(IRQ, RESET);   void setup(void) {    Serial.begin(9600);    Serial.println("Hello!");    nfc.begin();    // configure board to read RFID tags    nfc.SAMConfig();    Serial.println("Waiting for an ISO14443A Card ...");   }   void loop(void) {    uint8_t success;    uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID    uint8_t uidLength;            // Length of the UID (4 or 7 bytes depending on ISO14443A card type)      // Wait for an ISO14443A type cards (Mifare, etc.). When one is found    // 'uid' will be populated with the UID, and uidLength will indicate    // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)    success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);    if (success) {     // Display some basic information about the card     Serial.println("Found an ISO14443A card");     Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");     Serial.print(" UID Value: ");     nfc.PrintHex(uid, uidLength);     Serial.println("");     if (uidLength == 4)     {      // We probably have a Mifare Classic card ...      Serial.println("Seems to be a Mifare Classic card (4 byte UID)");      // Now we need to try to authenticate it for read/write access      // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF      Serial.println("Trying to authenticate block 4 with default KEYA value");      uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };      // Start with block 4 (the first block of sector 1) since sector 0      // contains the manufacturer data and it's probably better just      // to leave it alone unless you know what you're doing      success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);      if (success)      {       Serial.println("Sector 1 (Blocks 4..7) has been authenticated");       uint8_t data[16];       // If you don't want to write something to block 4 to test with, comment       // the following line and this text should be read back in a minute       // WARNING ! Write only on block 4, 5 and 6; 8, 9 and 10;       memcpy(data, (const uint8_t[]){ 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data);       success = nfc.mifareclassic_WriteDataBlock (4, data);       // Try to read the contents of block 4       success = nfc.mifareclassic_ReadDataBlock(4, data);       if (success)       {        // Data seems to have been read ... spit it out        Serial.println("Reading Block 4:");        nfc.PrintHexChar(data, 16);        Serial.println("");        // Wait a bit before reading the card again        delay(1000);       }       else       {        Serial.println("Ooops ... unable to read the requested block. Try another key?");       }      }      else      {       Serial.println("Ooops ... authentication failed: Try another key?");      }     }     if (uidLength == 7)     {      // We probably have a Mifare Ultralight card ...      Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");      // Try to read the first general-purpose user page (#4)      Serial.println("Reading page 4");      uint8_t data[32];      success = nfc.mifareultralight_ReadPage (4, data);      if (success)      {       // Data seems to have been read ... spit it out       nfc.PrintHexChar(data, 4);       Serial.println("");       // Wait a bit before reading the card again       delay(1000);      }      else      {       Serial.println("Ooops ... unable to read the requested page!?");      }     }    }   }  

Thanks for your help , and if you don't understand something you can ask me :).

2014-05-08 22:03:51 Deviouse,

Is quite simple really, I think you might still have time. What are the next steps? Anyway here are some tips:

Use the RFID and simply attach the relay example lines.

Basically there are 3 main points.
  1. Set your Relay pin
  2. Set the pinMode() to avoid strange things happening, this is done on the Setup() loop better
  3. Find the right stop to trigger the Relay when the card is found.
For the 3 point i think you can attach your relay trigger inside the function called:
if (success)
then relay on

and in the else
relay off

more coding will be needed, but it might help you to get some ideas.

Cheers^^




btw, thanks for coping the code again  ;)
userHeadPic Jose
2014-05-08 22:03:51 Deviouse,

Is quite simple really, I think you might still have time. What are the next steps? Anyway here are some tips:

Use the RFID and simply attach the relay example lines.

Basically there are 3 main points.
  1. Set your Relay pin
  2. Set the pinMode() to avoid strange things happening, this is done on the Setup() loop better
  3. Find the right stop to trigger the Relay when the card is found.
For the 3 point i think you can attach your relay trigger inside the function called:
if (success)
then relay on

and in the else
relay off

more coding will be needed, but it might help you to get some ideas.

Cheers^^




btw, thanks for coping the code again  ;)
userHeadPic Jose
2014-05-08 05:43:09 Sorry for double post but finally I receive my relay ! Well I have 2 weeks for finish it but it will be very hard. Well I don't think I will work on my machine but on the LED. I try the test connection diagram of the relay with the LED and it works. Now , when I pass my card if it is the good ID (of the card) the LED must be lighted. If not, the LED must be off. The program for detect my card and the ID is in the first post. But I add it again for you :
The RFID code :
Code: Select all
#include <Wire.h> 
#include <Adafruit_NFCShield_I2C.h> 
#define IRQ  (2) 
#define RESET (3) // Not connected by default on the NFC Shield 
Adafruit_NFCShield_I2C nfc(IRQ, RESET)
void setup(void)
  Serial.begin(9600); 
  Serial.println("Hello!"); 
  nfc.begin(); 
  // configure board to read RFID tags 
  nfc.SAMConfig(); 
  Serial.println("Waiting for an ISO14443A Card ..."); 
void loop(void)
  uint8_t success; 
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID 
  uint8_t uidLength;            // Length of the UID (4 or 7 bytes depending on ISO14443A card type)   
  // Wait for an ISO14443A type cards (Mifare, etc.). When one is found 
  // 'uid' will be populated with the UID, and uidLength will indicate 
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) 
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); 
  if (success) { 
  // Display some basic information about the card 
  Serial.println("Found an ISO14443A card"); 
  Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); 
  Serial.print(" UID Value: "); 
  nfc.PrintHex(uid, uidLength); 
  Serial.println(""); 
  if (uidLength == 4
  { 
    // We probably have a Mifare Classic card ... 
    Serial.println("Seems to be a Mifare Classic card (4 byte UID)"); 
    // Now we need to try to authenticate it for read/write access 
    // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 
    Serial.println("Trying to authenticate block 4 with default KEYA value"); 
    uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 
    // Start with block 4 (the first block of sector 1) since sector 0 
    // contains the manufacturer data and it's probably better just 
    // to leave it alone unless you know what you're doing 
    success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya); 
    if (success) 
    { 
    Serial.println("Sector 1 (Blocks 4..7) has been authenticated"); 
    uint8_t data[16]; 
    // If you don't want to write something to block 4 to test with, comment 
    // the following line and this text should be read back in a minute 
    // WARNING ! Write only on block 4, 5 and 6; 8, 9 and 10; 
    memcpy(data, (const uint8_t[]){ 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data); 
    success = nfc.mifareclassic_WriteDataBlock (4, data); 
    // Try to read the contents of block 4 
    success = nfc.mifareclassic_ReadDataBlock(4, data); 
    if (success) 
    { 
      // Data seems to have been read ... spit it out 
      Serial.println("Reading Block 4:"); 
      nfc.PrintHexChar(data, 16); 
      Serial.println(""); 
      // Wait a bit before reading the card again 
      delay(1000); 
    } 
    else 
    { 
      Serial.println("Ooops ... unable to read the requested block. Try another key?"); 
    } 
    } 
    else 
    { 
    Serial.println("Ooops ... authentication failed: Try another key?"); 
    } 
  } 
  if (uidLength == 7
  { 
    // We probably have a Mifare Ultralight card ... 
    Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)"); 
    // Try to read the first general-purpose user page (#4) 
    Serial.println("Reading page 4"); 
    uint8_t data[32]; 
    success = nfc.mifareultralight_ReadPage (4, data); 
    if (success) 
    { 
    // Data seems to have been read ... spit it out 
    nfc.PrintHexChar(data, 4); 
    Serial.println(""); 
    // Wait a bit before reading the card again 
    delay(1000); 
    } 
    else 
    { 
    Serial.println("Ooops ... unable to read the requested page!?"); 
    } 
  } 
  } 
The test connection of the relay with the Led :
Code: Select all
/*
# Product: 16A Relay Module
# SKU    : DFR0251
# Description:   
#    This sample code is combined  with the figure above to test the relay module whether works normally.
# Link:
#    D    --  2      // Signal
#    GND  --  GND
#    VCC  --  VCC
#    COM  --  13      // INPUT
#    NO    --  LED +  // OUTPUT
#    LED - --  GND
*/
 
int Relay = 3;
 
void setup()
{
  pinMode(13, OUTPUT);        //Set Pin13 as output
  digitalWrite(13, HIGH);    //Set Pin13 High
  pinMode(Relay, OUTPUT);    //Set Pin3 as output
}
void loop()
{
          digitalWrite(Relay, HIGH);  //Turn off relay
          delay(2000);
          digitalWrite(Relay, LOW);    //Turn on relay
          delay(2000);
}
How I can mix these programs ?
userHeadPic Deviouse
2014-05-08 05:43:09 Sorry for double post but finally I receive my relay ! Well I have 2 weeks for finish it but it will be very hard. Well I don't think I will work on my machine but on the LED. I try the test connection diagram of the relay with the LED and it works. Now , when I pass my card if it is the good ID (of the card) the LED must be lighted. If not, the LED must be off. The program for detect my card and the ID is in the first post. But I add it again for you :
The RFID code :
Code: Select all
#include <Wire.h> 
#include <Adafruit_NFCShield_I2C.h> 
#define IRQ  (2) 
#define RESET (3) // Not connected by default on the NFC Shield 
Adafruit_NFCShield_I2C nfc(IRQ, RESET)
void setup(void)
  Serial.begin(9600); 
  Serial.println("Hello!"); 
  nfc.begin(); 
  // configure board to read RFID tags 
  nfc.SAMConfig(); 
  Serial.println("Waiting for an ISO14443A Card ..."); 
void loop(void)
  uint8_t success; 
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID 
  uint8_t uidLength;            // Length of the UID (4 or 7 bytes depending on ISO14443A card type)   
  // Wait for an ISO14443A type cards (Mifare, etc.). When one is found 
  // 'uid' will be populated with the UID, and uidLength will indicate 
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) 
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); 
  if (success) { 
  // Display some basic information about the card 
  Serial.println("Found an ISO14443A card"); 
  Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); 
  Serial.print(" UID Value: "); 
  nfc.PrintHex(uid, uidLength); 
  Serial.println(""); 
  if (uidLength == 4
  { 
    // We probably have a Mifare Classic card ... 
    Serial.println("Seems to be a Mifare Classic card (4 byte UID)"); 
    // Now we need to try to authenticate it for read/write access 
    // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 
    Serial.println("Trying to authenticate block 4 with default KEYA value"); 
    uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 
    // Start with block 4 (the first block of sector 1) since sector 0 
    // contains the manufacturer data and it's probably better just 
    // to leave it alone unless you know what you're doing 
    success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya); 
    if (success) 
    { 
    Serial.println("Sector 1 (Blocks 4..7) has been authenticated"); 
    uint8_t data[16]; 
    // If you don't want to write something to block 4 to test with, comment 
    // the following line and this text should be read back in a minute 
    // WARNING ! Write only on block 4, 5 and 6; 8, 9 and 10; 
    memcpy(data, (const uint8_t[]){ 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data); 
    success = nfc.mifareclassic_WriteDataBlock (4, data); 
    // Try to read the contents of block 4 
    success = nfc.mifareclassic_ReadDataBlock(4, data); 
    if (success) 
    { 
      // Data seems to have been read ... spit it out 
      Serial.println("Reading Block 4:"); 
      nfc.PrintHexChar(data, 16); 
      Serial.println(""); 
      // Wait a bit before reading the card again 
      delay(1000); 
    } 
    else 
    { 
      Serial.println("Ooops ... unable to read the requested block. Try another key?"); 
    } 
    } 
    else 
    { 
    Serial.println("Ooops ... authentication failed: Try another key?"); 
    } 
  } 
  if (uidLength == 7
  { 
    // We probably have a Mifare Ultralight card ... 
    Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)"); 
    // Try to read the first general-purpose user page (#4) 
    Serial.println("Reading page 4"); 
    uint8_t data[32]; 
    success = nfc.mifareultralight_ReadPage (4, data); 
    if (success) 
    { 
    // Data seems to have been read ... spit it out 
    nfc.PrintHexChar(data, 4); 
    Serial.println(""); 
    // Wait a bit before reading the card again 
    delay(1000); 
    } 
    else 
    { 
    Serial.println("Ooops ... unable to read the requested page!?"); 
    } 
  } 
  } 
The test connection of the relay with the Led :
Code: Select all
/*
# Product: 16A Relay Module
# SKU    : DFR0251
# Description:   
#    This sample code is combined  with the figure above to test the relay module whether works normally.
# Link:
#    D    --  2      // Signal
#    GND  --  GND
#    VCC  --  VCC
#    COM  --  13      // INPUT
#    NO    --  LED +  // OUTPUT
#    LED - --  GND
*/
 
int Relay = 3;
 
void setup()
{
  pinMode(13, OUTPUT);        //Set Pin13 as output
  digitalWrite(13, HIGH);    //Set Pin13 High
  pinMode(Relay, OUTPUT);    //Set Pin3 as output
}
void loop()
{
          digitalWrite(Relay, HIGH);  //Turn off relay
          delay(2000);
          digitalWrite(Relay, LOW);    //Turn on relay
          delay(2000);
}
How I can mix these programs ?
userHeadPic Deviouse
2014-05-07 01:20:47 Ok I see. Sorry for answer too late it was holidays. Well thanks for your help but I can' finish my project now because I don't have receive my component and I don't have time to finish in time so it's sad. But nevermind maybe one day I will test alone with your help ;) userHeadPic Deviouse
2014-05-07 01:20:47 Ok I see. Sorry for answer too late it was holidays. Well thanks for your help but I can' finish my project now because I don't have receive my component and I don't have time to finish in time so it's sad. But nevermind maybe one day I will test alone with your help ;) userHeadPic Deviouse
2014-04-10 19:20:39 the relays often work like the LEDs, sending a HIGH to the pin to trigger the relay enable/disable. The LED is just an idea so you can test your set up before putting it up on the machine. It's really up to you, how you want to do it. Like a blink Arduino IDE example.

I'd appreciate a picture of your machine, and your current electronics, not exactly the plan. But you can add the plan as well, more information will let us recommend you better.

userHeadPic Jose
2014-04-10 19:20:39 the relays often work like the LEDs, sending a HIGH to the pin to trigger the relay enable/disable. The LED is just an idea so you can test your set up before putting it up on the machine. It's really up to you, how you want to do it. Like a blink Arduino IDE example.

I'd appreciate a picture of your machine, and your current electronics, not exactly the plan. But you can add the plan as well, more information will let us recommend you better.

userHeadPic Jose
2014-04-10 05:52:54 Yes , I just asked to my teacher to order it so I have to wait. Thanks for your recommendation about this module. Well , I think I have a LED so what I need to do with this Led ? Test the relay ? I think there's some examples on the net so it will be ok. You need a picture of what ? The machine or the electronic plan of the machine ? userHeadPic Deviouse
2014-04-10 05:52:54 Yes , I just asked to my teacher to order it so I have to wait. Thanks for your recommendation about this module. Well , I think I have a LED so what I need to do with this Led ? Test the relay ? I think there's some examples on the net so it will be ok. You need a picture of what ? The machine or the electronic plan of the machine ? userHeadPic Deviouse
2014-04-05 02:14:09 Hello Deviouse,

Have you considered this module:
https://www.dfrobot.com/index.php?route= ... uct_id=992

You don't really need an LED, is just a test approach. You could use the Serial port directly I guess. You don't have any LED around?
Do you have a picture of your current hardware system?
userHeadPic Jose
2014-04-05 02:14:09 Hello Deviouse,

Have you considered this module:
https://www.dfrobot.com/index.php?route= ... uct_id=992

You don't really need an LED, is just a test approach. You could use the Serial port directly I guess. You don't have any LED around?
Do you have a picture of your current hardware system?
userHeadPic Jose
2014-04-02 01:41:21 Yes it's like a segway. The wheels are hidden but it's the same thing.
Well , I need a LED then ? To try the reading of cards ?
I saw technical caracteristics and the shield is not enough powerful so I need another one which is more powerful , do you have another shield to recommend me ? I can't try it now until I have another one. The tension of the motor of the machine is 24VDC and the power is 200 W.

Thanks for help Jose.
userHeadPic Deviouse
2014-04-02 01:41:21 Yes it's like a segway. The wheels are hidden but it's the same thing.
Well , I need a LED then ? To try the reading of cards ?
I saw technical caracteristics and the shield is not enough powerful so I need another one which is more powerful , do you have another shield to recommend me ? I can't try it now until I have another one. The tension of the motor of the machine is 24VDC and the power is 200 W.

Thanks for help Jose.
userHeadPic Deviouse
2014-03-26 22:07:14 Hi deviouse,

This project looks very interesting, This machine is like a segway? I don't see the wheels, so I am quite confused about how it works.

I don't have the adafruit NFC module, but the relay shield is very simple to use! is like light an LED. The only thing you need to take care of are the pins so its not in conflict with the other attached hardware.

I think you can try the NFC example, if the card is read, lit the LED. Just for test, stand alone and no relay. If this works, attach the Relay shield and change the LED pin to the Relay pin and try again.

Keep in mind, this module needs external power supply:7~ 12V    testing from the usb might not work well if the whole hardware setup requires more power.
userHeadPic Jose
2014-03-26 22:07:14 Hi deviouse,

This project looks very interesting, This machine is like a segway? I don't see the wheels, so I am quite confused about how it works.

I don't have the adafruit NFC module, but the relay shield is very simple to use! is like light an LED. The only thing you need to take care of are the pins so its not in conflict with the other attached hardware.

I think you can try the NFC example, if the card is read, lit the LED. Just for test, stand alone and no relay. If this works, attach the Relay shield and change the LED pin to the Relay pin and try again.

Keep in mind, this module needs external power supply:7~ 12V    testing from the usb might not work well if the whole hardware setup requires more power.
userHeadPic Jose
2014-03-26 11:05:19 No help ? :/ userHeadPic Deviouse
2014-03-26 11:05:19 No help ? :/ userHeadPic Deviouse
2014-03-20 05:39:58 Hello :).

I'm in french college and I have a final project for my baccalaureate. I have a machine : http://www.ewee.fr/boutique/ewee-pt.htm ... re=default and with the relay shield v2.1 : https://www.dfrobot.com/index.php?route= ... ymaAoU3OgY , I have to create a new system for replace the switch of the machine and "create" a new "switch" with a RFID shield and the relay shield. When I pass my good swipe card on the shield RFID the machine starts. But before that I have to program the relay shield with Arduino to create this new switch and remove the old switch.
I found a code for the relay shield but I'm not sure if it will help me. I have to create a new code with the relay shield code and the rfid shield code
Code: Select all
/*
# This Sample code is for testing the Relay shield V2.1 for Arduino.
 
# Editor : Phoebe
# Date  : 2013.2.28
# Ver    : 0.1
# Product: Relay shield for Arduino
# SKU    : DRI0144
 
# Hardwares:
1. Arduino UNO
2. Relay Shield For Arduino V2.1
3  Power Supply:7~ 12V
*/
byte relayPin[4] = {
  2,7,8,10};
//D2 -> RELAY1
//D7 -> RELAY2
//D8 -> RELAY3
//D10 -> RELAY
char input=0;
int val;
void setup() {
  for(int i = 0; i < 4; i++)  pinMode(relayPin[i],OUTPUT);
  Serial.begin(57600);
  delay(100);
  Serial.println("Press 1-4 to control the state of the relay");
  Serial.println("waiting for input:");
  for(int j = 0; j < 4; j++)  digitalWrite(relayPin[j],LOW);
}
void loop() {
  if (Serial.available())
  { 
    char input= Serial.read(); 
    if(input != -1)
    {
      switch(input)
      {
 
      case '1':                       
        Serial.println("Relay1");
        val=digitalRead(relayPin[0]);
        val=!val;
        digitalWrite(relayPin[0],val);
        break;
      case '2':
        Serial.println("Relay2");
        val=digitalRead(relayPin[1]);
        val=!val;
        digitalWrite(relayPin[1],val);
        break;
      case '3':
        Serial.println("Relay3");
        val=digitalRead(relayPin[2]);
        val=!val;
        digitalWrite(relayPin[2],val);
        break;
      case '4':
        Serial.println("Relay4");
        val=digitalRead(relayPin[3]);
        val=!val;
        digitalWrite(relayPin[3],val);
        break;   
       
      default:
        if(input != '\r' && input != '\n')
        Serial.println("invalid entry");
        break;
      }
    }
//    else unablerelay();
  }
}
and this is the RFID code for reading the swipe cards :
Code: Select all
#include <Wire.h> 
#include <Adafruit_NFCShield_I2C.h> 
#define IRQ  (2) 
#define RESET (3) // Not connected by default on the NFC Shield 
Adafruit_NFCShield_I2C nfc(IRQ, RESET)
void setup(void)
  Serial.begin(9600); 
  Serial.println("Hello!"); 
  nfc.begin(); 
  // configure board to read RFID tags 
  nfc.SAMConfig(); 
  Serial.println("Waiting for an ISO14443A Card ..."); 
void loop(void)
  uint8_t success; 
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID 
  uint8_t uidLength;            // Length of the UID (4 or 7 bytes depending on ISO14443A card type)   
  // Wait for an ISO14443A type cards (Mifare, etc.). When one is found 
  // 'uid' will be populated with the UID, and uidLength will indicate 
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) 
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); 
  if (success) { 
  // Display some basic information about the card 
  Serial.println("Found an ISO14443A card"); 
  Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); 
  Serial.print(" UID Value: "); 
  nfc.PrintHex(uid, uidLength); 
  Serial.println(""); 
  if (uidLength == 4
  { 
    // We probably have a Mifare Classic card ... 
    Serial.println("Seems to be a Mifare Classic card (4 byte UID)"); 
    // Now we need to try to authenticate it for read/write access 
    // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 
    Serial.println("Trying to authenticate block 4 with default KEYA value"); 
    uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 
    // Start with block 4 (the first block of sector 1) since sector 0 
    // contains the manufacturer data and it's probably better just 
    // to leave it alone unless you know what you're doing 
    success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya); 
    if (success) 
    { 
    Serial.println("Sector 1 (Blocks 4..7) has been authenticated"); 
    uint8_t data[16]; 
    // If you don't want to write something to block 4 to test with, comment 
    // the following line and this text should be read back in a minute 
    // WARNING ! Write only on block 4, 5 and 6; 8, 9 and 10; 
    memcpy(data, (const uint8_t[]){ 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data); 
    success = nfc.mifareclassic_WriteDataBlock (4, data); 
    // Try to read the contents of block 4 
    success = nfc.mifareclassic_ReadDataBlock(4, data); 
    if (success) 
    { 
      // Data seems to have been read ... spit it out 
      Serial.println("Reading Block 4:"); 
      nfc.PrintHexChar(data, 16); 
      Serial.println(""); 
      // Wait a bit before reading the card again 
      delay(1000); 
    } 
    else 
    { 
      Serial.println("Ooops ... unable to read the requested block. Try another key?"); 
    } 
    } 
    else 
    { 
    Serial.println("Ooops ... authentication failed: Try another key?"); 
    } 
  } 
  if (uidLength == 7
  { 
    // We probably have a Mifare Ultralight card ... 
    Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)"); 
    // Try to read the first general-purpose user page (#4) 
    Serial.println("Reading page 4"); 
    uint8_t data[32]; 
    success = nfc.mifareultralight_ReadPage (4, data); 
    if (success) 
    { 
    // Data seems to have been read ... spit it out 
    nfc.PrintHexChar(data, 4); 
    Serial.println(""); 
    // Wait a bit before reading the card again 
    delay(1000); 
    } 
    else 
    { 
    Serial.println("Ooops ... unable to read the requested page!?"); 
    } 
  } 
  } 
Thanks for your help , and if you don't understand something you can ask me :).
userHeadPic Deviouse