Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 1:9d7c34bfe43e, committed 2020-12-15
- Comitter:
 - nwabiam
 - Date:
 - Tue Dec 15 18:15:48 2020 +0000
 - Parent:
 - 0:76a291b4c5db
 - Commit message:
 - Simple Electronic Lock (Solenoid) code
 
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C12832.lib Tue Dec 15 18:15:48 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/askksa12543/code/C12832/#990d5eec2ef6
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Door_1_Docs/RGBled.cpp	Tue Dec 15 18:15:48 2020 +0000
@@ -0,0 +1,10 @@
+#include "RGBled.h"
+
+RGBled::RGBled (PinName ledPin): rgbled (DigitalOut(ledPin)){
+}
+RGBled::~RGBled(){
+}
+bool RGBled::SetRGBledStatus (bool RGBStatus){
+    
+    rgbled = RGBStatus;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Door_1_Docs/RGBled.h	Tue Dec 15 18:15:48 2020 +0000
@@ -0,0 +1,19 @@
+#ifndef RGBled_H
+#define RGBled_H
+
+#include <mbed.h>
+
+class   RGBled {
+
+    public:
+        RGBled (PinName ledPin);
+        ~RGBled ();
+        bool SetRGBledStatus (bool RGBStatus);
+        
+    private:
+        DigitalOut rgbled;
+        
+          
+};
+      
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Door_1_Docs/Speaker.h	Tue Dec 15 18:15:48 2020 +0000
@@ -0,0 +1,19 @@
+#include "mbed.h"
+// new class to play a note on Speaker based on PwmOut class
+class Speaker
+{
+public:
+    Speaker(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the Speaker Constructor
+    }
+// class method to play a note based on PwmOut class
+    void PlayNote(float frequency, float duration, float volume) {
+        _pin.period(1.0/frequency);
+        _pin = volume/2.0;
+        wait(duration);
+        _pin = 0.0;
+    }
+
+private:
+    PwmOut _pin;
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Door_1_Docs/hcsr04.cpp	Tue Dec 15 18:15:48 2020 +0000
@@ -0,0 +1,29 @@
+#include "hcsr04.h"
+#include "mbed.h"
+/*
+*HCSR04.cpp
+*/
+HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {}
+ float HCSR04::echo_duration() {
+        
+    timer.reset();  //reset timer
+    trig=0;   // trigger low 
+    wait_us(2); //  wait 
+    trig=1;   //  trigger high
+    wait_us(10);
+    trig=0;  // trigger low
+         while(!echo); // start pulseIN
+      timer.start();
+     while(echo);
+      timer.stop();
+     return timer.read_us(); 
+ 
+}
+ 
+//return distance in cm 
+float HCSR04::distance(){
+    duration = echo_duration();
+  distance_cm = (duration/2)/29.1  ;
+        return distance_cm;
+ 
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Door_1_Docs/hcsr04.h	Tue Dec 15 18:15:48 2020 +0000
@@ -0,0 +1,20 @@
+#ifndef hcsr04_H
+#define hcsr04_H
+#include "mbed.h"
+ 
+ 
+ 
+class HCSR04 {
+  public:
+    HCSR04(PinName t, PinName e);
+    float echo_duration();
+    float distance();
+ 
+    private:
+        DigitalOut trig;
+        DigitalIn echo;
+        Timer timer;
+        float duration,distance_cm;
+};
+ 
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Solenoid.cpp	Tue Dec 15 18:15:48 2020 +0000
@@ -0,0 +1,11 @@
+#include "Solenoid.h"
+
+Solenoid::Solenoid (PinName targetSolenoidPin): solenoid (DigitalOut(targetSolenoidPin)){
+}
+Solenoid::~Solenoid(){
+}
+bool Solenoid::SetSolenoidStatus (bool Status){
+    
+    solenoid = Status;
+    //mySolenoid_1.SetSolenoidStatus();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Solenoid.h	Tue Dec 15 18:15:48 2020 +0000
@@ -0,0 +1,23 @@
+#ifndef Solenoid_H
+#define Solenoid_H
+
+#include <mbed.h>
+
+class   Solenoid {
+
+    public:
+        Solenoid (PinName targetSolenoidPin);
+        ~Solenoid ();
+        bool SetSolenoidStatus (bool Status);
+        //float distance
+        
+    private:
+        DigitalOut solenoid;
+
+};
+      
+#endif
+    
+    
+
+    
--- a/main.cpp	Thu Oct 29 16:38:37 2015 +0000
+++ b/main.cpp	Tue Dec 15 18:15:48 2020 +0000
@@ -1,15 +1,68 @@
 #include "mbed.h"
- 
-DigitalOut myled(LED1);
-DigitalOut Ctrl(p8); //Solenoid output control bit
- 
-int main() {
+#include "hcsr04.h"
+#include "Solenoid.h"
+#include "RGBled.h"
+#include "Speaker.h"
+#include "C12832.h"
+#include <iostream>
+
+HCSR04 sensor(p9, p10);
+Solenoid mySolenoid (p18);
+Solenoid mySolenoid_2 (p19);
+RGBled  redled(p24);
+RGBled  greenled(p23);
+C12832 lcd(p5, p7, p6, p8, p11);
+InterruptIn MagSense(p16);
+Speaker Interrupt(p26);
+DigitalOut myled1(LED1);
+
+void flip()
+{
     while(1) {
-        Ctrl = 1; //ON
-        myled = 1;
-        wait(0.5); 
-        Ctrl = 0; //OFF
-        myled = 0;
-        wait(2);
+        if (mySolenoid.SetSolenoidStatus (false)){
+            Interrupt.PlayNote(969, 0.1, 1.0);      // When contact is broken, interrupt the main program and sound the alarm and perform flip funtion
+            Interrupt.PlayNote(800, 0.1, 1.0);
+            lcd.locate (0,0);
+            lcd.printf("Door Open               ");
+            redled.SetRGBledStatus (true);
+            greenled.SetRGBledStatus (false);
+            }
+            if (MagSense.read() == 0) { // when magsensor is not broken break the loop
+                break; 
+            }
     }
 }
+int main()
+{
+    redled.SetRGBledStatus (false);
+    greenled.SetRGBledStatus (false);
+    float distance = sensor.distance();  //constantly read floating distance
+    MagSense.rise(&flip);
+    lcd.cls();
+    while(1) {
+        float distance = sensor.distance();
+        if (mySolenoid.SetSolenoidStatus (false)) {
+                    greenled.SetRGBledStatus (false);
+                    redled.SetRGBledStatus (true);
+                    lcd.locate(0,0);
+                    lcd.printf("**UNLOCKED**               ");
+                }
+        if (distance < 7)  {
+            while (1) {
+                mySolenoid.SetSolenoidStatus (true);
+                mySolenoid_2.SetSolenoidStatus (true);
+                if (mySolenoid.SetSolenoidStatus (true)) {
+                    greenled.SetRGBledStatus (true);
+                    redled.SetRGBledStatus (false);
+                    lcd.locate(0,0);
+                    lcd.printf("**LOCKED**             ");
+                }              
+            }
+        } else if (distance > 7) {
+            printf("distance =  %f\n",distance);
+            wait(0.5); // 1 sec
+        }
+    }
+
+}
+