Making the alarm irritating

But there is a problem. The problem is that the lights will be switched on and, no matter whether you get up, the light would automatically switch itself off within 20 minutes. That is a bit of problem because not every time will you wake up in just 20 minutes. So, in that case, what should we do? The first thing we need to do is to detect whether you have woken up. This is very simple and not much needs to be told here. If you wake up in the morning, it is very certain that you will move out of the bed. Once you do, we can detect the motion that can tell our automated system whether you have really woken up.

Now, what we can do here is something very simple. We can detect your motion, and based on that detection, we can be decisive on whether you have really woken up. This doesn't seem much of a task. All we need to do is to add a motion detection sensor. For this purpose, we can use a PIR sensor, which can tell us whether the motion has been detected. So, let's go ahead, add another layer of sensor on top of our system, and see what happens. 

So, first, connect the circuit as follows. While mounting the PIR sensor, do make sure that it is facing the bed and detecting any motion on and around it. Once the PIR is set up, wire the sensors as shown in the following diagram and see what happens:

Once done, then go ahead and write the following code:

import RPi.GPIO as GPIO
import time

LIGHT = 23
PIR = 24
Irritation_flag = 3

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

GPIO.setup(LIGHT,GPIO.OUT)
GPIO.setup(PIR, 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:

GPIO.output(LIGHT,GPIO.LOW)
time.sleep(10)

Irritation_Flag = Irritation_Flag - 1


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 = 3
GPIOP.output(LIGHT, GPIO.LOW)

OK, let's see what we have done. The code is extremely simple, but we had a small twist in it, that is,  Irritation_Flag:

Irritation_flag = 3

Now this variable works something like a snooze button. As we know, when we wake up sometimes, or in fact, most of the time, we again go back to sleep only to wake up much later to realize that we are late. To prevent this, we have this Irritation_flag, and what this basically would be used for is to detect the number of times you have performed the action to stop the alarm. How it would be used we will see later: 

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

GPIO.output(LIGHT,GPIO.HIGH)

In this line, we are simply comparing time values by hours and minutes. If the hours is 07 and minutes are fewer than or equal to 15, then the lights would be switched off. There is also a condition that says Irritation_Flag > 0 as we have already declared in the beginning that the value of Irritation_flag = 3; hence, initially this condition will always be true. The last condition is GPIO.input(PIR) == 0; which means that the condition will only be satisfied when the PIR has not detected any motion. In very simple words, the alarm will go off every time between 07:00 and 07:15 if the PIR does not detect any motion: 

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

GPIO.output(LIGHT,GPIO.LOW)
time.sleep(10)

Irritation_Flag = Irritation_Flag - 1

In this part of the program, the condition will only be true if the hours or H is equal to 7 and when the PIR is detecting some motion. Hence, every time when the time is between 07:00 and 07:59 and whenever the motion is detected, the condition will be true. Once true, the program will first switch off the light using the line GPIO.output*LIGHT,GPIO.LOW. Once it is turned off, it waits for 10 seconds using time.sleep(10). Once the time is over, it will implement the following operation: Irritation_Flag - Irritation_Flag - 1. Now what it does is that it decrements the value of Irritation_Flag by 1 every time it detects a motion. So the first time a motion happens, the value of Irritation_Flag would be 2; thereafter, it would be 1, and finally, it would be 0

If you look at the previous part of the code, you will be able to make out that the light would be switched on if the value of Irritation_Flag was greater than 0. So if you want to turn off the light, you would have to move at least three times. Why three times? Because then the code Irritation_Flag = Irritation - 1 would be executed three times so as to make the value get down to 0, which obviously makes the condition GPIO.input(PIR) > 0 false:

        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)

Now, let's say even after of all this, you still do not wake up. Then what should happen? We have something special for you here. Now, instead of an if condition, we have a for loop. What this will check for is that the time should be 07 hours, and minutes should be greater than 15, Irritation_Flag > 0, and obviously no motion is being detected. Till the time all of these are true, the light would be switched on thereafter for 5 seconds, it would be kept switched on using the time.sleep(5). The lights would be again switched on. Now this will keep on happening till the time the conditions are true or in other words, till the time is between 07:15 and 07:59. Irritation)_Flag > 0, that is, the motion is not detected for three times and there is no motion detected. Till that time, the for loop would keep on the switch on and off of the light in action. Due to frequent biking of light, there is a very higher chance of you waking up. This may be very effective, but surely not the most convenient. Well, however inconvenient it is, it will still be better than the conventional alarm:

        if H != '07':

Irritation_flag = 3

We have the entire light-based alarm ready for us to wake us up every morning. However, there is a problem. Once it is turned off, the value of Irritation_Flag will be 0. Once it is turned to 0, then no matter what the time is, the light would never start up. Hence, to make sure that the alarm is always operational at the same time every single day, we would need to set the value of the flag to any number more than 0.

Now in the preceding line, if H != '07', then the Irritation_flag would be 3. That is whenever the time is anything other than 07 hours, then the value of Irritation_Flag would be 3

It was simple, wasn't it? But I'm sure that it would do a good job to make sure you wake up on time.