comms not ready

Dependencies:   NRF2401P mbed-rtos mbed

Revision:
0:05989ea2d008
Child:
1:51a5cfbfd25c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Jun 12 08:57:26 2015 +0000
@@ -0,0 +1,111 @@
+#include "mbed.h"
+#include "rtos.h"
+
+/*Program to open locker when a signal level (lock) is sent in from an external controller. 
+System is split into threads so main loop can run even when waiting in door unlock function.
+
+The circuit uses a metal contact to detect if the door is open/closed(latch engaged or not) and sends out an external signal.
+Main loop checks if door needs to be unlocked(from external signal) and if door is open.
+Door unlock solenoid is controlled by function unlock*/
+
+/*function definitions*/
+void unlock (void const *args);           //function statement to unlock door,
+
+/*set pins*/
+DigitalOut latch(D0);       //signal out to open door
+DigitalIn lock(D1);       //signal in from interface to unlock door
+DigitalIn doorclosed(D2);   //detect if door is closed(logic 1 for door closed)
+DigitalOut flagdoor(D3);    //flag out if door is open
+PwmOut alarm(PTE20);        //alarm via pulse width modulated signal on pin PTE20
+
+//testing parameters
+DigitalOut led(LED1);           //light to denote unlock thread
+DigitalOut led2(LED2);          //light to denote cycle
+
+//global flags used in both treads, hence volatile
+volatile int unlockflag = 0;             //internal flag to denote need to unlock door
+volatile int alarmflag = 1;              //alarm activated flag
+
+volatile int justarmed = 0;             //flag to remember if alarm has just been armed to not run unlocking process
+int main()
+{
+    //initital set up for system
+    led=1;
+    alarmflag = 1;      //initially alarm armed
+    unlockflag = 0;     //initially no call to unlock
+    alarm = 0;          //initialise alarm as not sounding
+    latch = 1;          //solenoid initially locked
+    
+    Thread Tunlock(unlock);     //set up unlock function as a thread, thread should not run while unlockflag ==0
+
+
+    //main loop that runs forever, checking for external flags and if door open/closed and to fire off alarm(status thread)
+    while(true) 
+    {
+        led2 = !led2;
+        //check if need to unlock door(external flag, lock), setting internal flag(unlockflag) so unlock function will run accordingly
+        if (lock == 0 && alarmflag ==1 && justarmed == 0) {            
+            unlockflag = 1;
+            Thread :: wait(100);
+        }
+            //after one cycle from arming alarm, turn off justarmed flag and wait for more than a second before testing for next unlock signal
+            else if(justarmed ==1 && alarmflag==1)
+            {
+                justarmed = 0;
+                Thread :: wait(1100);
+            }    
+
+        //check if door is open
+        if (doorclosed == 1) {      //if door is open(electric contact lost), send out flag(logic 1)
+            flagdoor = 0;
+        } else {
+            flagdoor = 1;
+        }
+        Thread :: wait(200);        //only check door status every 0.2s
+        
+        //check if door broken into(alarm active, no unlock signal and door open)
+        if(alarmflag == 1 && lock == 1 && doorclosed == 0)
+        {
+            alarm = 0.5;      //alarm goes off, doesnt stop until investigated by operator
+        }
+    }
+}
+
+/*function to unlock door to be called in thread. Note time limit on solenoid open to prevent overheating
+(should put spring to force door open once unlocked) unlock protocol thread*/
+void unlock (void const *args)
+{
+    (void)args;     //to note thread function has no input parameter
+    
+   
+    while (true) {
+    //unlock process should only run code below if unlockflag == 1, checks every cycle, does nothing if internal flag(unlockflag)==0
+        if (unlockflag == 1) 
+        {      
+            alarmflag = 0;  //deactivate alarm
+            latch = 0;      //open latch via relay activating solenoid
+            led = 0;        //test light on to tell if solenoid should be on
+            Thread :: wait(5000);   //latch stays open(solenoid activated) for 5 seconds
+            latch = 1;      //turn off solenoid, let latch closed
+            led = 1;        //turn off test light
+            unlockflag = 0;     //reset internal flag for door lock
+            
+            //wait for door to be closed, do other loop checks
+            while (doorclosed == 0)
+            {
+                Thread :: wait(100);    //wait 0.5s, allowing other threads to run
+            }
+            //if door is closed and alarm not activated, activate alarm and remember that alarm is just armed
+                if (doorclosed == 1 && alarmflag==0 && justarmed ==0)
+                {
+                    alarmflag = 1;
+                    justarmed = 1;
+                }    
+                    else
+                    {
+                        Thread :: wait(200);            //do main thread checks while waiting(superfluous code from previous iteration?)
+                    }
+           
+        }    
+    }
+}