Back to Parent

//---------- RFID ------------
#include <MFRC522.h>
#define SS_PIN SS
#define RST_PIN D2
MFRC522 myRFID(SS_PIN, RST_PIN);	// Create MFRC522 instance.
String id = "";
String data_sent = "";
//------------------------------


//---------- PHOTOCELL ------------
static const int photoPin = A0;
int photoReading;
int photoMap;
int photoCount;
int lastPhotoCount;
//------------------------------


//---------- TIMING ------------
unsigned long lastSampleTime = 0;
unsigned long sampleInterval = 500; // in ms
//------------------------------

//-----OUTGOING-------
String sendState;

void setup() {
	// Initialize serial communications with the computer
	Serial.begin(9600);	
	
	myRFID.setSPIConfig(); // sets up SPI config
	myRFID.PCD_Init();	// Initialize RC522 card
	Serial.println("Start Scanning");
	
	pinMode(photoPin, INPUT);
	
}

void loop() {
    
    rfidSetup();
	socialChip();
	
}

void socialChip(){
    unsigned long now = millis();
  if (lastSampleTime + sampleInterval < now) {
    lastSampleTime = now;
    
    photoReading = analogRead(photoPin); // Social Chip initiation sensor
    photoMap = map(photoReading, 0, 1023, 0, 500);
    
    // Serial.print("Photo Value = ");
    // Serial.println(photoMap);     // the raw analog reading

    if (photoMap <= 1000) { // SENSOR IS DARK, CHIP IS PLACED
        photoCount = 1;
        // Serial.print("Photo Count = ");
        // Serial.println(photoCount);
        // data_sent = "dark_start";
        // Particle.publish("chip_sensor", data_sent, PRIVATE);
    } else if (photoMap > 1000){ // NO CHIP IS PRESENT
        photoCount = 0;
        // Serial.print("Photo Count = ");
        // Serial.println(photoCount);
        //  data_sent = "light_stop";
        // Particle.publish("chip_sensor", data_sent, PRIVATE); 
    }
    
    if (photoCount == 1 && lastPhotoCount == 0){
        sendState = 1;
        Serial.println(sendState);
        // data_sent = "start";
        // Particle.publish("chip_sensor", data_sent, PRIVATE); 
    } else if (photoCount == 0 && lastPhotoCount == 1){
        sendState = 7;
        Serial.println(sendState);
        // data_sent = "stop";
        // Particle.publish("chip_sensor", data_sent, PRIVATE); 
    }
    
    lastPhotoCount = photoCount;
    
  }
}

void rfidSetup(){
    
	// Look for new cards
    if ( ! myRFID.PICC_IsNewCardPresent()) { 
        return; 
    }
// Read tapped card data
    if ( ! myRFID.PICC_ReadCardSerial()) { 
        return; 
    }
    for (byte i = 0; i < myRFID.uid.size; i++) {
        // Create a RFID Hexdecimal String by adding and converting the binary
        id += String(myRFID.uid.uidByte[i], HEX);
    }
    id.toUpperCase(); // Convert to Uppercase
    // Print the RFID identifier to the Serial Port
    // Serial.print(id);
    
    if (id == "7BE73D7E") { // STARTER
        sendState = 2;
        Serial.println(sendState);
    }
    
    if (id == "B0DECE7A") { // MAIN
    sendState = 3;
        Serial.println(sendState);
    }
    
    if (id == "B0A9865B") { // SIDE 01
        sendState = 4;
        Serial.println(sendState);
    }
    
    if (id == "5B5D96BB") { // SIDE 02
        sendState = 5;
        Serial.println(sendState);
    }
    
    if (id == "1B903E7E") { // DESSERT
        sendState = 6;
        Serial.println(sendState);
    }
    
    // Reset Id
    id = "";
    // Serial.println();
    // Stop the reading after one tap
    myRFID.PICC_HaltA();
    /* Used to exit the PCD from its authenticated state. Remember to call this function after
    communicating with an authenticated PICC - otherwise no new communications can
    start .*/
    myRFID.PCD_StopCrypto1();
}
Click to Expand

Content Rating

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

0