Back to Parent

// This #include statement was automatically added by the Particle IDE.
#include "MFRC522.h"
long lastPublishedAt = 0;
int publishAfter = 1000;
int redPin = D4;
int greenPin = D3;
int bluePin = D2;
int ledRed = D5;
int ledGreen = D6;
int ledBlue = D7;
#define SS_PIN SS
#define RST_PIN D8
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
String id = "";
long lastDetection = -1;
String lastDetected = "";
int redetectAfter = 10000;
void setup() {
    pinMode(redPin, INPUT_PULLUP);
   // pinMode(yellowPin, INPUT_PULLUP);
    pinMode(greenPin, INPUT_PULLUP);
    pinMode(bluePin, INPUT_PULLUP);
    pinMode(ledRed,OUTPUT);
    pinMode(ledGreen,OUTPUT);
    pinMode(ledBlue,OUTPUT);
    
    Serial.begin(9600); // Initialize serial communications with the PC
    mfrc522.setSPIConfig(); // sets up SPI config
    mfrc522.PCD_Init(); // Initialize RC522 card
    Serial.println("Start Scanning");
    Particle.publish( "started" );
}
void loop() {
    
    int redVal = digitalRead(redPin);
    int greenVal = digitalRead(greenPin);
    int blueVal = digitalRead(bluePin);
    
    if (redVal == LOW){ 
        
        publishRedEvent();
        digitalWrite(ledRed,HIGH);
        delay(1000);
        digitalWrite(ledRed,LOW);
    }
 
    if (greenVal == LOW){ 
        publishGreenEvent();
        digitalWrite(ledGreen,HIGH);
        delay(1000);
        digitalWrite(ledGreen,LOW);
      }
      
    if (blueVal == LOW){ 
        publishBlueEvent();
        digitalWrite(ledBlue,HIGH);
        delay(1000);
        digitalWrite(ledBlue,LOW);
    }
    // Look for new cards
    if ( ! mfrc522.PICC_IsNewCardPresent()) {
        return;
    }
    // Serial.println("New card Detected");
    // Read tapped card data
    if ( ! mfrc522.PICC_ReadCardSerial()) {
        return;
    }
    // discover the id of the last item;
    String rfid_card_id = "";
    for (byte i = 0; i < mfrc522.uid.size; i++) {
        // Create a RFID Hexdecimal String
        rfid_card_id += String(mfrc522.uid.uidByte[i], HEX);
        // Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    }
    // Convert to Uppercase
    rfid_card_id.toUpperCase();
    // if a card hasn't been detected in x seconds/
    // update
    if( lastDetection + redetectAfter < millis() && rfid_card_id == "3CD8FA37" ){
        lastDetection = millis();
        Particle.publish("rme2019/memoryspaces/activity/location1", rfid_card_id  );
    }
    delay(100);
}
void publishRedEvent() {
    if(lastPublishedAt + publishAfter < millis() ){
        String eventName = "red";
        Particle.publish(eventName, "data");
        lastPublishedAt = millis();
    }
}
void publishGreenEvent() {
    if(lastPublishedAt + publishAfter < millis() ){
        String eventName = "green";
        Particle.publish(eventName, "data");
        lastPublishedAt = millis();
    }
}
void publishBlueEvent() {
    if(lastPublishedAt + publishAfter < millis() ){
        String eventName = "blue";
        Particle.publish(eventName, "data");
        lastPublishedAt = millis();
    }
}
String detectID(){
    String rfid_card_id = "";
    for (byte i = 0; i < mfrc522.uid.size; i++) {
        // Create a RFID Hexdecimal String
        rfid_card_id += String(mfrc522.uid.uidByte[i], HEX);
        // Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    }
    // Convert to Uppercase
    rfid_card_id.toUpperCase();
    
    return rfid_card_id;
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0