Skip to Content
Raspberry Pi 3 Cookbook for Python Programmers - Third Edition
book

Raspberry Pi 3 Cookbook for Python Programmers - Third Edition

by Steven Lawrence Fernandes, Tim Cox
April 2018
Beginner content levelBeginner
552 pages
13h 58m
English
Packt Publishing
Content preview from Raspberry Pi 3 Cookbook for Python Programmers - Third Edition

How to do it...

Create the btntest.py script as follows:

#!/usr/bin/python3 
#btntest.py 
import time 
import os 
import RPi.GPIO as GPIO 
#HARDWARE SETUP 
# GPIO 
# 2[==X==1=======]26[=======]40 
# 1[=============]25[=======]39 
#Button Config 
BTN = 12 
 
def gpio_setup(): 
  #Setup the wiring 
  GPIO.setmode(GPIO.BOARD) 
  #Setup Ports 
  GPIO.setup(BTN,GPIO.IN,pull_up_down=GPIO.PUD_UP) 
   
 
def main(): 
  gpio_setup() 
  count=0 
  btn_closed = True 
  while True: 
    btn_val = GPIO.input(BTN) 
    if btn_val and btn_closed: 
       print("OPEN") 
       btn_closed=False 
    elif btn_val==False and btn_closed==False: 
       count+=1 
       print("CLOSE %s" % count) 
       os.system("flite -t '%s'" % count) 
       btn_closed=True 
    time.sleep(0.1) 
 
 
try: 
  main() 
finally: 
  GPIO.cleanup() 
  print("Closed Everything. END") 
#End 
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Raspberry Pi for Python Programmers Cookbook - Second Edition

Raspberry Pi for Python Programmers Cookbook - Second Edition

Tim Cox

Publisher Resources

ISBN: 9781788629874Supplemental Content