แก้ให้แล้ว

Dependencies:   NOKIA_5110 mbed

Fork of Lost-Found_BOX by FRA221:A

Revision:
10:323219f2a084
Parent:
9:5944142f8b40
Child:
11:d773e5c4cc3a
--- a/main.cpp	Mon Dec 07 19:31:30 2015 +0000
+++ b/main.cpp	Tue Dec 08 22:23:05 2015 +0000
@@ -20,16 +20,30 @@
 #include "myRFID.h"
 #include "myLCD4884.h"
 #include <ctype.h>
-DigitalOut LedGreen(LED1);
-DigitalOut spi_address[2] = {SPI_CS,PB_4};
+DigitalOut LedGreen(LED1); //RFID Status
+DigitalOut spi_address[2] = {SPI_CS,PB_4}; //SPI Slave using... (RFID,LCD4884)
+DigitalOut LDRstatus(D4); //Set enable LDR
+AnalogIn LDR(A0);
+double LDRref = 0.1000; //set LDR Threshold
 //Serial connection to PC for output
 Serial esp(D8, D2); //tx rx
-Serial pc(SERIAL_TX, SERIAL_RX);
-Speakers speak(D3);
-DigitalOut lock(PB_13);
-MFRC522    RfChip   (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);
-char buffers[5]={'\0'};
-char IDs[17]={'\0'};
+Serial pc(SERIAL_TX, SERIAL_RX); //for debugging
+Speakers speak(D3); //Buzzer setup
+DigitalOut lock(PB_13); //Solenoid Control
+MFRC522    RfChip   (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET); //RFID Setup
+char buffers[5]={'\0'}; //Buffer
+char IDs[17]={'\0'}; //IDs of RFID
+char backupIDs[17]={'\0'}; //IDs of RFID
+
+int ItemCheck(){
+    pc.printf("Now LDR state : %lf",LDR.read());
+    if(LDR.read() < LDRref){
+        pc.printf("Item available\n");
+        return 1; //Item Available
+    }else
+    pc.printf("Item unavailable\n");
+    return 0; //Item Unavailable
+}
 
 void wifiInitial(){
        pc.baud(115200);
@@ -45,6 +59,7 @@
 }
 
 int main(void) {
+    LDRstatus =1;
     // Init. LCD Pin
     LcdPins myPins;      
     myPins.sce  = PB_4;  //D5 ActiveLow SlaveSelect
@@ -69,8 +84,6 @@
     
     // Init.RFID Variable
     uint8_t RFID[10];
-    int checker = 0;
-    int checks = 0;
     int mode = 0;
     int submode = 0;
     pc.printf("starting...\n");
@@ -87,22 +100,30 @@
 
     while(true){//Loop around
         wait(1);
+        lock = 0; //Locked!
+
+
         getStatus("192.168.1.88","/l-f-box/getdata.php?check=1",stat_uid); //Recheck now status
-        lock = 0;
- 
             
-        if(isalnum((int)stat_uid[0])){
+        if(isalnum((int)stat_uid[0]) && ItemCheck()){ //Check status has an item AND item in the box
             mode = 0; //It have an item
             submode=-1;
-        }else{
+        }else if(!ItemCheck()){
             mode = 1;  //Doesn't have an item
             submode=0; 
+        }else
+        {
+            pc.printf("ERROR! Something went wrong... Maybe Item was stolen\n");    
+            
+            speak.speakWrong();
+            wait(3);
         }
 
 
         
         while(mode == 0){ //Withdraw Process (Item in the box!)
-        pc.printf("Withdraw process...");
+            
+            pc.printf("Withdraw process...");
              if(stat_uid[0] == 'X'){ //Unlocked!
                 speak.speakRight();
                 wait(0.5);
@@ -112,11 +133,40 @@
                 myLcd.SetXY(16,2);
                 myLcd.DrawString("TAP A CARD...");
                 myLcd.stop(); 
-                submode = 0; 
+                submode ++; 
              }else{
                 wait(1);
                 getStatus("192.168.1.88","/l-f-box/getdata.php?check=1",stat_uid); //Recheck now status
+               
+               //Check for now available card..
+               if ( ! RfChip.PICC_IsNewCardPresent()) 
+               {
+                      wait_ms(500);
+                      continue;
+                }
+                // Select one of the cards
+                if( ! RfChip.PICC_ReadCardSerial())
+                {
+                      wait_ms(500);
+                      continue;
+                }
+                GetID(RFID);
+                pc.printf("Comparison old data %s , new %s",backupIDs,IDs);
+                if(backupIDs[0] == IDs[0]){
+                    if(backupIDs[1] == IDs[1]){
+                    if(backupIDs[2] == IDs[2]){ 
+                    pc.printf("Permission allow!\n");
+                    stat_uid[0] = 'X'; //Force to unlock
+                    speak.speak();
+                    submode++;
+                    }
             
+                }
+                }else{
+                    pc.printf("Permission denied.\n");
+                    speak.speakWrong();
+                }
+                
             }
             if(submode == 0){ //Get RFID for update withdrawer
                 if ( ! RfChip.PICC_IsNewCardPresent())
@@ -140,7 +190,20 @@
                     submode ++;    
             }
             if(submode == 1){
-                lock=1;
+                    wait(0.5);
+                    myLcd.InitLcd();  
+                    myLcd.DrawString("PLEASE GET ITEM...");
+                    myLcd.stop();  
+                lock=1; // Unlocked
+                while(ItemCheck()){
+                    pc.printf("Waiting for withdraw item\n");
+                    wait(1);    
+                }
+                    wait(0.5);
+                    myLcd.InitLcd();  
+                    myLcd.DrawString("PLEASE CLOSE THE DOOR");
+                    myLcd.stop();  
+                wait(6);
                 updateWithdrawer("192.168.1.88","/l-f-box/update.php",sid);
                 wait(0.5);
                 myLcd.InitLcd();
@@ -208,15 +271,27 @@
             }
             if(submode == 3){ // Show depositor data on screen
                 newDepositor("192.168.1.88","/l-f-box/update.php",sid);
+                lock =1; //Unlocked the box!
                 wait(0.5);
                 myLcd.InitLcd();  
                 myLcd.SetXY(20,1);
                 myLcd.DrawString("PLEASE");   
                 myLcd.SetXY(4,3);
-                myLcd.DrawString("CHECK ON WEB");   
+                myLcd.DrawString("PUT THE ITEMS");   
                 myLcd.stop();
-    
-                wait(3);
+                
+                while(!ItemCheck()){ //Waiting for Item , Until it has an item...
+                    pc.printf("Waiting for item...");
+                    wait(0.5);
+                }
+               wait(0.5);
+                myLcd.InitLcd();  
+                myLcd.SetXY(20,1);
+                myLcd.DrawString("PLEASE");   
+                myLcd.SetXY(4,3);
+                myLcd.DrawString("CLOSE THE DOOR");   
+                myLcd.stop();
+                wait(8);
                 myLcd.InitLcd();  
                 myLcd.SetXY(10,0);
                 myLcd.DrawString("==CONTACT==");   
@@ -231,11 +306,11 @@
     
                 myLcd.DrawString(tel);   
                 myLcd.stop();
-                
-                
-                submode=0;
-                mode = 0;
-                lock = 0;
+                strcpy(backupIDs,IDs);
+                wait(3);
+                submode=0; //Bring back submode value.
+                mode = 0; //Bring to Item In Mode...
+                lock = 0; //Locked!
             } // End Deposit Process
         }