Gesture recognition-based automation

Now we have interfaced the connections as per the following diagram:

Let's go ahead and upload the following code:

import signal
import flicklib
import time
import RPi.GPIO as GPIO
GIPO.setmode(GPIO.BCM)
GPIO.setup(light, GPIO.OUT)
GPIO.setup(fan,GPIO.OUT)
pwm = GPIO.PWM(fan,100)
def message(value):
print value
@flicklib.move()
def move(x, y, z):
global xyztxt
xyztxt = '{:5.3f} {:5.3f} {:5.3f}'.format(x,y,z)
@flicklib.flick()
def flick(start,finish):
global flicktxt
flicktxt = 'FLICK-' + start[0].upper() + finish[0].upper()
message(flicktxt)
def main():
global xyztxt
global flicktxt
xyztxt = ''
flicktxt = ''
flickcount = 0
dc_inc = 0
dc_dec = 0

while True:
pwm.start(0)
xyztxt = ' '
if len(flicktxt) > 0 and flickcount < 5:
flickcount += 1
else:
flicktxt = ''

flickcount = 0
if flicktxt ==”FLICK-WE”:
GPIO.output(light,GPIO.LOW)
if flicktxt ==”FLICK-EW”:
GPIO.output(light,GPIO.HIGH)
if flicktxt ==”FLICK-SN”:
if dc_inc < 100:
dc_inc = dc_inc + 10
pwm.changeDutyCycle(dc_inc)

else:
Dc_inc = 10
if flicktxt ==”FLICK-NS”:
if dc_inc >0:
dc_dec = dc_dec - 10
pwm.changeDutyCycle(dc_dec)
main()

The program is in addition to the program we have done before, as always we have some added functionality of using the data being received by the flick gesture board and using it to switch on or switch off the lights. 

Like the previous program, we are taking in the gestures over the board in the form of the directions of swipes, and using a simple condition to switch off the lights, or switch them on. So, let's see what are the additions:

       if flicktxt ==”FLICK-WE”:

GPIO.output(light,GPIO.LOW)

The first condition is simple. We are comparing the value of flicktxt to a given variable, which in our case is FLICK-WE, wherein WE stands for west to east. So when we flick from west to east, or in other words, when we flick from left to right, the lights would be switched off:

       if flicktxt ==”FLICK-EW”:
GPIO.output(light,GPIO.HIGH)

As before, we are again taking in a variable named FLICK-EW, which stands for flick from east to west. What it does is whenever we flick our hand from east to west, or from right to left, the lights will be switched on:

       if flicktxt ==”FLICK-SN”:
if dc_inc <= 100:
dc_inc = dc_inc + 20
pwm.changeDutyCycle(dc_inc)

Now we have put a dimmer along with a fan to control the speed of the fan as well; hence, we will have to give it a PWM corresponding to the speed that we want to drive it. Now whenever the user will flick his hand from south to north or from down to up. The condition if dc_inc <100 will check whether the value of the dc_inc is less than or equal to 100 or not. If it is, then it will increment the value of the dc_inc by 20 values. Using the function ChangeDutyCycle(), we are providing the different duty cycle to the dimmer; hence, changing the overall speed of the fan. Every time you swipe up the value of the fan, it will increase by 20%:

        else:
Dc_inc = 10
if flicktxt ==”FLICK-NS”:
if dc_inc >0:
dc_dec = dc_dec - 10
pwm.changeDutyCycle(dc_dec)
..................Content has been hidden....................

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