Sensos Module Updated

Dependents:   SmartLabProyect

Fork of Middleware by Roberto Herrera

Committer:
Jorge_Beltran
Date:
Thu Dec 10 02:31:36 2015 +0000
Revision:
3:1704050210b5
Parent:
1:fd355dc296b1
Child:
4:c59485faea9f
Updateed middleware library; Added ultrasonic function on sensors module

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RoHe 1:fd355dc296b1 1 #include "Sensors.h"
RoHe 1:fd355dc296b1 2
RoHe 1:fd355dc296b1 3 /*Functions*/
RoHe 1:fd355dc296b1 4
RoHe 1:fd355dc296b1 5 //PRESCENCE DETECTION FUNCTION
RoHe 1:fd355dc296b1 6 bool GetPrescenceStatus(){
RoHe 1:fd355dc296b1 7 static bool RoomStatus = FALSE;
RoHe 1:fd355dc296b1 8
RoHe 1:fd355dc296b1 9 if(PIR1==TRUE || PIR2==TRUE)
RoHe 1:fd355dc296b1 10 {
RoHe 1:fd355dc296b1 11 RoomStatus = TRUE;
RoHe 1:fd355dc296b1 12 }
RoHe 1:fd355dc296b1 13 else
RoHe 1:fd355dc296b1 14 {
RoHe 1:fd355dc296b1 15 RoomStatus = FALSE;
RoHe 1:fd355dc296b1 16 }
RoHe 1:fd355dc296b1 17
RoHe 1:fd355dc296b1 18 return RoomStatus;
RoHe 1:fd355dc296b1 19 }
RoHe 1:fd355dc296b1 20
RoHe 1:fd355dc296b1 21 //DOOR STATUS FUNCION
RoHe 1:fd355dc296b1 22 bool GetDoorStatus(){
RoHe 1:fd355dc296b1 23 static bool DoorStatus = CLOSED;
RoHe 1:fd355dc296b1 24
RoHe 1:fd355dc296b1 25 if(DOORSensor == TRUE)
RoHe 1:fd355dc296b1 26 DoorStatus = OPEN;
RoHe 1:fd355dc296b1 27 else
RoHe 1:fd355dc296b1 28 DoorStatus = CLOSED;
RoHe 1:fd355dc296b1 29
RoHe 1:fd355dc296b1 30 return DoorStatus;
RoHe 1:fd355dc296b1 31 }
RoHe 1:fd355dc296b1 32
RoHe 1:fd355dc296b1 33
RoHe 1:fd355dc296b1 34 //WINDOW STATUS FUNCTION
RoHe 1:fd355dc296b1 35 int GetWindowStatus(int window){
RoHe 1:fd355dc296b1 36 static int WindowStatus = CLOSED;
RoHe 1:fd355dc296b1 37
RoHe 1:fd355dc296b1 38 switch(window)
RoHe 1:fd355dc296b1 39 {
RoHe 1:fd355dc296b1 40 case 1:
RoHe 1:fd355dc296b1 41 if(W1)
RoHe 1:fd355dc296b1 42 WindowStatus = OPEN;
RoHe 1:fd355dc296b1 43 else
RoHe 1:fd355dc296b1 44 WindowStatus = CLOSED;
RoHe 1:fd355dc296b1 45 break;
RoHe 1:fd355dc296b1 46 case 2:
RoHe 1:fd355dc296b1 47 if(W2)
RoHe 1:fd355dc296b1 48 WindowStatus = OPEN;
RoHe 1:fd355dc296b1 49 else
RoHe 1:fd355dc296b1 50 WindowStatus = CLOSED;
RoHe 1:fd355dc296b1 51 break;
RoHe 1:fd355dc296b1 52 case 3:
RoHe 1:fd355dc296b1 53 if(W3)
RoHe 1:fd355dc296b1 54 WindowStatus = OPEN;
RoHe 1:fd355dc296b1 55 else
RoHe 1:fd355dc296b1 56 WindowStatus = CLOSED;
RoHe 1:fd355dc296b1 57 break;
RoHe 1:fd355dc296b1 58 default:
RoHe 1:fd355dc296b1 59 WindowStatus = BAD_PARAMETER;
RoHe 1:fd355dc296b1 60 break;
RoHe 1:fd355dc296b1 61 }
RoHe 1:fd355dc296b1 62
RoHe 1:fd355dc296b1 63 return WindowStatus;
RoHe 1:fd355dc296b1 64 }
RoHe 1:fd355dc296b1 65
RoHe 1:fd355dc296b1 66 //LIGHT CONTROL FUNCTION
RoHe 1:fd355dc296b1 67 int SetLight(bool Light){
RoHe 1:fd355dc296b1 68 static int LightStatus = 0;
RoHe 1:fd355dc296b1 69
RoHe 1:fd355dc296b1 70 switch(Light)
RoHe 1:fd355dc296b1 71 {
RoHe 1:fd355dc296b1 72 case 0:
RoHe 1:fd355dc296b1 73 LightStatus = OFF;
RoHe 1:fd355dc296b1 74 LightControl = OFF;
RoHe 1:fd355dc296b1 75 break;
RoHe 1:fd355dc296b1 76 case 1:
RoHe 1:fd355dc296b1 77 LightStatus = ON;
RoHe 1:fd355dc296b1 78 LightControl = ON;
RoHe 1:fd355dc296b1 79 break;
RoHe 1:fd355dc296b1 80 default:
RoHe 1:fd355dc296b1 81 LightStatus = BAD_PARAMETER;
RoHe 1:fd355dc296b1 82 break;
RoHe 1:fd355dc296b1 83 }
RoHe 1:fd355dc296b1 84
RoHe 1:fd355dc296b1 85 return LightStatus;
RoHe 1:fd355dc296b1 86 }
RoHe 1:fd355dc296b1 87
Jorge_Beltran 3:1704050210b5 88 //PERSON COUNTER FUNCTIONS
Jorge_Beltran 3:1704050210b5 89 static void dist(int distance)
Jorge_Beltran 3:1704050210b5 90 {
Jorge_Beltran 3:1704050210b5 91 /*Void*/
Jorge_Beltran 3:1704050210b5 92 }
Jorge_Beltran 3:1704050210b5 93 static void dist2(int distance2)
Jorge_Beltran 3:1704050210b5 94 {
Jorge_Beltran 3:1704050210b5 95 /*Void*/
Jorge_Beltran 3:1704050210b5 96 }
Jorge_Beltran 3:1704050210b5 97
Jorge_Beltran 3:1704050210b5 98 //START ULTRASONIC INIT
Jorge_Beltran 3:1704050210b5 99 ultrasonic mu(D10, D11, .1, 1, &dist); //Set the trigger pin to D10 and the echo pin to D11
Jorge_Beltran 3:1704050210b5 100 //have updates every .1 seconds and a timeout after 1
Jorge_Beltran 3:1704050210b5 101 //second, and call dist when the distance changes
Jorge_Beltran 3:1704050210b5 102 ultrasonic mu2(D12, D13, .1, 1, &dist2); //Set the trigger pin to D12 and the echo pin to D13
Jorge_Beltran 3:1704050210b5 103 //have updates every .1 seconds and a timeout after 1
Jorge_Beltran 3:1704050210b5 104 //second, and call dist when the distance changes
Jorge_Beltran 3:1704050210b5 105 //END ULTRASONIC INIT
Jorge_Beltran 3:1704050210b5 106
Jorge_Beltran 3:1704050210b5 107 unsigned int GetPersonStatus()
Jorge_Beltran 3:1704050210b5 108 {
Jorge_Beltran 3:1704050210b5 109 static int CDistance = 0;
Jorge_Beltran 3:1704050210b5 110 static int CDistance2 = 0;
Jorge_Beltran 3:1704050210b5 111 static unsigned int PersonNumber = 0;
Jorge_Beltran 3:1704050210b5 112 static int flag=0;
Jorge_Beltran 3:1704050210b5 113 mu.startUpdates();//start mesuring the distance
Jorge_Beltran 3:1704050210b5 114 mu2.startUpdates();//start mesuring the distance
RoHe 1:fd355dc296b1 115
Jorge_Beltran 3:1704050210b5 116 printf("--------------------------------------------- \r\n");
Jorge_Beltran 3:1704050210b5 117 //Do something else here
Jorge_Beltran 3:1704050210b5 118 mu.checkDistance(); //call checkDistance() as much as possible, as this is where
Jorge_Beltran 3:1704050210b5 119 //the class checks if dist needs to be called.
Jorge_Beltran 3:1704050210b5 120 mu2.checkDistance(); //call checkDistance() as much as possible, as this is where
Jorge_Beltran 3:1704050210b5 121 //the class checks if dist needs to be called.
Jorge_Beltran 3:1704050210b5 122 CDistance = mu.getCurrentDistance();
Jorge_Beltran 3:1704050210b5 123 CDistance2 = mu2.getCurrentDistance();
Jorge_Beltran 3:1704050210b5 124
Jorge_Beltran 3:1704050210b5 125 if(CDistance <= RANGE && flag == 0)
Jorge_Beltran 3:1704050210b5 126 {
Jorge_Beltran 3:1704050210b5 127 flag = 1;
Jorge_Beltran 3:1704050210b5 128 printf("Entrando %d\r\n", flag);
Jorge_Beltran 3:1704050210b5 129 }
Jorge_Beltran 3:1704050210b5 130 if(CDistance2 <= RANGE && flag == 0)
Jorge_Beltran 3:1704050210b5 131 {
Jorge_Beltran 3:1704050210b5 132 flag = 2;
Jorge_Beltran 3:1704050210b5 133 printf("Saliendo %d\r\n", flag);
Jorge_Beltran 3:1704050210b5 134 }
Jorge_Beltran 3:1704050210b5 135
Jorge_Beltran 3:1704050210b5 136 switch(flag)
Jorge_Beltran 3:1704050210b5 137 {
Jorge_Beltran 3:1704050210b5 138 case 1:
Jorge_Beltran 3:1704050210b5 139 wait(0.3);
Jorge_Beltran 3:1704050210b5 140 CDistance2 = mu2.getCurrentDistance();
Jorge_Beltran 3:1704050210b5 141 if(CDistance2 <= RANGE)
Jorge_Beltran 3:1704050210b5 142 {
Jorge_Beltran 3:1704050210b5 143 PersonNumber++;
Jorge_Beltran 3:1704050210b5 144 printf("Entro una persona \r\n");
Jorge_Beltran 3:1704050210b5 145 }
Jorge_Beltran 3:1704050210b5 146 else
Jorge_Beltran 3:1704050210b5 147 {
Jorge_Beltran 3:1704050210b5 148 printf("No entro nadie \r\n");
Jorge_Beltran 3:1704050210b5 149 }
Jorge_Beltran 3:1704050210b5 150 break;
Jorge_Beltran 3:1704050210b5 151
Jorge_Beltran 3:1704050210b5 152 case 2:
Jorge_Beltran 3:1704050210b5 153 wait(0.3);
Jorge_Beltran 3:1704050210b5 154 CDistance = mu.getCurrentDistance();
Jorge_Beltran 3:1704050210b5 155 if(CDistance <= RANGE)
Jorge_Beltran 3:1704050210b5 156 {
Jorge_Beltran 3:1704050210b5 157 if(PersonNumber > 0)PersonNumber--;
Jorge_Beltran 3:1704050210b5 158 printf("Salio una persona \r\n");
Jorge_Beltran 3:1704050210b5 159 }
Jorge_Beltran 3:1704050210b5 160 else
Jorge_Beltran 3:1704050210b5 161 {
Jorge_Beltran 3:1704050210b5 162 printf("No salio nadie \r\n");
Jorge_Beltran 3:1704050210b5 163 }
Jorge_Beltran 3:1704050210b5 164 break;
Jorge_Beltran 3:1704050210b5 165
Jorge_Beltran 3:1704050210b5 166 default:
Jorge_Beltran 3:1704050210b5 167
Jorge_Beltran 3:1704050210b5 168 break;
Jorge_Beltran 3:1704050210b5 169 }
Jorge_Beltran 3:1704050210b5 170 printf("Personas adentro %d\r\n", PersonNumber);
Jorge_Beltran 3:1704050210b5 171 printf("+++++++++++++++++++ \r\n");
Jorge_Beltran 3:1704050210b5 172 wait(0.5);
Jorge_Beltran 3:1704050210b5 173 flag = 0;
RoHe 1:fd355dc296b1 174
RoHe 1:fd355dc296b1 175 return PersonNumber;
Jorge_Beltran 3:1704050210b5 176 }