I’m having trouble compiling my adapted code and i don’t really get why…
I’m using a particle photon, a rc522 rfid reader and a relay.
Can any one help me figure this out?
Thank you
code:
#include "MFRC522/MFRC522.h"
/*
Function Core Pin MRFC522 Pin
Reset D2 RST
SPI SS D1 SDA
SPI MOSI A5 MOSI
SPI MISO A4 MISO
SPI SCK A3 SCK
*/
#define SS_PIN D1
#define RST_PIN D2
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
mfrc522.setSPIConfig();
mfrc522.PCD_Init(); // Init MFRC522 card
String ok_rfid_1="25bcefb0";
int lock=5; //Which pin the lock will be on if using a relay or solenoid or similar
RGB.control(true); // take control of onboard RGB led
}
void blink() {
RGB.color(0, 0, 255);
delay(150);
RGB.color(0, 0, 0);
delay(100);
RGB.color(0, 0, 255);
delay(150);
RGB.color(0, 0, 0);
}
void open_lock() {
//Use this routine when working with Relays and Solenoids etc.
digitalWrite(lock, HIGH);
delay(2000);
digitalWrite(lock,LOW);
}
void loop() {
// Look for new cards
if ( mfrc522.PICC_IsNewCardPresent()) {
// Serial.println("New card present...");
if ( mfrc522.PICC_ReadCardSerial()) {
// Dump debug info about the card. PICC_HaltA() is automatically called.
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
String UID = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
UID += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
UID += String(mfrc522.uid.uidByte[i], HEX);
}
mfrc522.PICC_HaltA();
Serial.print("UID: ");
Serial.println(UID);
if (UID==ok_rfid_1) {
//ok, open the door.
open_lock();
}
blink(); // visual feedback of read using onboard led
}
}
}
errors:
dumpinfo.cpp: In function 'void setup()':
dumpinfo.cpp:24:7: warning: unused variable 'lock' [-Wunused-variable]
void setup() {
^
dumpinfo.cpp: In function 'void open_lock()':
dumpinfo.cpp:42:16: error: 'lock' was not declared in this scope
RGB.color(0, 0, 255);
^
dumpinfo.cpp:42:16: note: suggested alternative:
In file included from ../wiring/inc/spark_wiring_thread.h:31:0,
from ../wiring/inc/spark_wiring_watchdog.h:22,
from ../wiring/inc/spark_wiring_cloud.h:31,
from ../wiring/inc/spark_wiring.h:48,
from ./inc/application.h:36,
from MFRC522/MFRC522.h:77,
from dumpinfo.cpp:1:
/usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/c++/4.8.4/mutex:708:5: note: 'std::lock'
lock(_L1& __l1, _L2& __l2, _L3&... __l3)
^
dumpinfo.cpp: In function 'void loop()':
dumpinfo.cpp:68:16: error: 'ok_rfid_1' was not declared in this scope
UID += String(mfrc522.uid.uidByte[i], HEX);
^
make[1]: *** [../build/target/user/platform-6dumpinfo.o] Error 1
make: *** [user] Error 2
@bernas_123, you declared lock as in int in setup() making it a local variable. Trying to use it in open_lock() causes the error since lock is not known globally. Move the declaration for lock outside of setup() to make it global.
@peekay123 thank for the reply. So i moved lock and ok_rfid_1 outside of setup and the code compiles but the board goes dark and, no lights and no seiral output, any idea why?
thank you
EDIT: removed the whole blink()function and the board lights up in the healthy blue light but nothing shows in serial…
Probably because neither if ( mfrc522.PICC_IsNewCardPresent()) nor if ( mfrc522.PICC_ReadCardSerial()) evaluate to true.
Add some extra Serial.print() statements fo all other cases too, then you should see something and maybe even guess what’s happening.
Funny thing, changed the wiring and it turns out it does display the UID of the tag in serial but i no longer have that code that is running in the photon, it should be be like this but it does not compile
#include "MFRC522/MFRC522.h"
/*
Function Core Pin MRFC522 Pin
Reset D2 RST
SPI SS D1 SDA
SPI MOSI A5 MOSI
SPI MISO A4 MISO
SPI SCK A3 SCK
*/
#define SS_PIN D1
#define RST_PIN D2
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
string ok_rfid_1="F5 53 43 A8";
int lock=5; //Which pin the lock will be on if using a relay or solenoid or similar
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
mfrc522.setSPIConfig();
mfrc522.PCD_Init(); // Init MFRC522 card
pinMode(lock, OUTPUT);
RGB.control(true); // take control of onboard RGB led
}
void blink() {
RGB.color(0, 0, 255);
delay(150);
RGB.color(0, 0, 0);
delay(100);
RGB.color(0, 0, 255);
delay(150);
RGB.color(0, 0, 0);
}
void open_lock() {
//Use this routine when working with Relays and Solenoids etc.
digitalWrite(lock, HIGH);
delay(2000);
digitalWrite(lock,LOW);
}
void loop() {
// Look for new cards
if ( mfrc522.PICC_IsNewCardPresent()) {
// Serial.println("New card present...");
if ( mfrc522.PICC_ReadCardSerial()) {
// Dump debug info about the card. PICC_HaltA() is automatically called.
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
String UID = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
UID += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
UID += String(mfrc522.uid.uidByte[i], HEX);
}
mfrc522.PICC_HaltA();
Serial.print("UID: ");
Serial.println(UID);
if (UID==ok_rfid_1) {
//ok, open the door.
open_lock();
}
blink(); // visual feedback of read using onboard led
}
}
}
errors:
umpinfo.cpp:16:1: error: 'string' does not name a type
SPI SCK A3 SCK
^
dumpinfo.cpp: In function 'void loop()':
dumpinfo.cpp:67:16: error: 'ok_rfid_1' was not declared in this scope
UID += String(mfrc522.uid.uidByte[i], HEX);
^
make[1]: *** [../build/target/user/platform-6dumpinfo.o] Error 1
make: *** [user] Error 2
thanks for the quick responses, it verifies and, UID is displayed in serial, the board’s led blinks like supposed in the code but no relay movement, the serial displays UID : f55343a8 so should it be String ok_rfid_1="F55343A8";```orString ok_rfid_1=“F5 53 43 A8”;``` ?
So i got it working fine, except for the unlock/locl part so i tried using a simple example to control a servo but i doenst work, any idea why? im using a servo connected to an 5v external power and pin A2 to send the signal but it doesn’t move…
Servo myservo; // create servo object to control a servo <br> // a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(A2); // attaches the servo on pin 2 to the servo object
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}