Making it even more irritating

Can you completely rely on the preceding system? If you really have control over your morning emotions of not getting out of the bed, then, yes, sure you can. But for those who just love to be in bed and sleep again after hitting the snooze button, then I am sure you would be able to find a way to switch off the light without properly waking up. So as in the code, the light would switch off whenever the motion was detected three times. But the motion can be anything. You can simply wave your hand while still being in the bed, and the system would detect it as a motion, which would defy the whole purpose. So what should we do now? 

We have a solution for it! We can use a way by which we can be sure that you have to get out of bed. For this very purpose, we will be using our IR proximity sensor, which we have used earlier in our projects, and based on the distance reading of the sensor, we can detect whether you have gone past any specific area. This can be really interesting as you can fit this sensor pointing away from the bed or maybe on the gate of the bathroom, and till the time, you do not cross that specific line. The system would not switch off the alarm. So let's see how we would do it. First, connect the hardware, as shown in the following diagram:

Once you are done with the diagram, go ahead and upload the following code:

import RPi.GPIO as GPIO
import time

import Adafruit_ADS1x15
adc0 = Adafruit_ADS1x15.ADS1115()

GAIN = 1

adc0.start_adc(0, gain=GAIN)


LIGHT = 23
PIR = 24
Irritation_flag = 1
IR = 2

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(LIGHT,GPIO.OUT)
GPIO.setup(PIR, GPIO.IN)
GPIO.setup(IR. GPIO.IN)

import datetime

H = datetime.datetime.now().strftime('%H')
M = datetime.datetime.now().strftime('%M')


while True:

if H = '07' and M <= '15' and Iriitation_Flag > 0 and GPIO.input(PIR) == 0:

GPIO.output(LIGHT,GPIO.HIGH)


if H = '07'and GPIO.input(PIR)==1:

M_snooze = datetime.datetime.now().strftime('%M')
M_snooze = M_snooze + 5

for M <= M_snooze

GPIO.output(LIGHT,GPIO.LOW)

F_value = adc0.get_last_result()
F1 = (1.0 / (F_value / 13.15)) - 0.35

time.sleep(0.1)

F_value = adc0.get_last_result()
F2 = (1.0 / (F_value / 13.15)) - 0.35

F_final = F1-F2

M = datetime.datetime.now().strftime('%M')

if F_final > 25

Irritation_flag = 0




for H = '07'and M > '15' and Irritation_Flag > 0 and GPIO.input(PIR) = 0:

GPIO.output(LIGHT,GPIO.HIGH)
time.sleep(5)
GPIO.output(LIGHT,GPIO.LOW)
time.sleep(5)


if H != '07':

Irritation_flag = 1

Mind blown? This code seems quite complex, having conditions inside conditions and again some more conditions. Say hello to robotics! These conditions make up a lot of robot's programming. A robot has to see continuously what is happening around and make decisions according to it. It's also the way humans work, don't we? 

So, that being said, let's see what we are actually doing here. Most of the code is pretty much the same as the last one. The main difference comes somewhere around the middle of the programming section:

  if H = '07' and M <= '15' and Iriitation_Flag > 0 and GPIO.input(PIR) == 0:

GPIO.output(LIGHT,GPIO.HIGH)

We are switching on the lights as soon as the time is between 07:00 and 07:15:

  if H = '07'and GPIO.input(PIR)==1:

M_snooze = datetime.datetime.now().strftime('%M')
M_snooze = M_snooze + 5

In the hour of 07 whenever the PIR sensor is triggered or in other words, the PIR sensor detects any motion, then it will do a set of activities inside the if condition, which includes noting down the time by the function datetime.datetime.now().strftime('%M') and then storing it down in a variable named M_snooze.

In the next line, we are taking the value of that minute stored in M_snooze and adding another 5 minutes to it. So the value of M_snooze is now incremented by 5:

   for M <= M_snooze 

Now, in the same if condition that we used previously, we have placed a for loop, which looks like this: for M <= M_snooze. But what does this mean? Here, what we are doing is pretty simple. The program inside the for loop will keep on running and will stay in the loop till the time the condition that we have stated is true. Now, the condition here states that till the time M is smaller or equal to M_snooze, the condition will stay true. As you have learned earlier, M is the current minute value and M_snooze is the value of M at the time of starting of this loop, which is incremented by 5. Hence, the loop would be true for 5 minutes from the time of starting: 

     GPIO.output(LIGHT,GPIO.LOW)

F_value = adc0.get_last_result()
F1 = (1.0 / (F_value / 13.15)) - 0.35

time.sleep(0.1)

F_value = adc0.get_last_result()
F2 = (1.0 / (F_value / 13.15)) - 0.35

F_final = F1-F2

Now, this is the most interesting part of the program. Till the time, the for loop for M <= M_snooze is true, the preceding lines of code will run. Let's see what it is doing. In the line, F-value = adc0.get_last_result(), it is taking the value of the IR proximity sensor and storing it in F_value. Thereafter, in the line F1 = (1.0/(F_value/13.15))-0.35, we are simply calculating the distance in centimeters. We have already studied how this is happening, so not much explanation needs to be done here. The value of distance is stored in a variable named F1. Thereafter, using the function time.sleep(0.1), we are pausing the program for 0.1 seconds. Thereafter, we are again repeating the same task again; that is, we are again taking the value of distance. But this time, the distance value calculated is stored in an another variable named F2. Finally, after all of this is done, we are calculating F_final, which is F_final = F1 - F2. So we are simply calculating the difference in distance between the first and the second reading. But, you must be asking why are we doing this. What good does it do?

Well, as you remember, we have placed the IR proximity sensor in front of our bathroom gate. Now, if no one is passing in front of it, the value will remain fairly constant. But whenever a person passes through it, there will be a change in distance. So if there is a change in the overall distance from first to last reading, then we can say that someone has passed through the IR sensor. 

That is pretty cool, but why don't we simply keep a threshold value like we have done previously? The answer to this is simple. That is because if you need to change the position of the sensor, then you again need to recalibrate the sensor according to the position. So this is a simple yet robust solution that can be used anywhere:

     if F_final > 10

Irritation_flag = 1

Now we have got the reading, which can tell us whether a person has passed in front of it. But this data will not be useful until we put it somewhere. 

So, here in the condition if F_final > 10, whenever the distance change is more than 10 cm, then the condition would be true and the line Irritation_flag would be set to 1.

If you go back to the previous lines, then you will be able to make out that the lights will only be on when the time is between 07:00 and 07:15 and the Irritation_flag must be 0. As with this condition, we have set a part of the condition false by making the Irritation_flag = 1; hence, the program to switch on the lights will not work. 

Now, let's look back and see what we have done so far: 

  • Whenever the time is 07:00–07:15, the lights would be switched on
  • If a movement is detected, then the lights would be switched off
  • A condition will be true for another five minutes, which will wait for detection of human motion through the IR proximity sensor
  • If a person crosses that within five minutes, then the alarm would be deactivated or else the alarm will again start to switch on the light

Pretty cool, huh? That being said, let's add another added functionality from the previous program:

  for H = '07'and M > '15' and Irritation_Flag = 0 and GPIO.input(PIR) = 0:

GPIO.output(LIGHT,GPIO.HIGH)
time.sleep(5)
GPIO.output(LIGHT,GPIO.LOW)
time.sleep(5)

You know what this does. If you do not move around in the first 15 minutes, that is from 07:00 to 07:15, then it will start blinking the lights every five seconds, forcing you to wake up:

        if H != '07':

Irritation_flag = 0

Finally, we use the condition if H != '07':. So, whenever the value of H is anything other than 07, then the condition would be true, and this will reset the Irritation_flag to 0. By now, you know what turning  Irritation_flag to 0 does. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.202.167