Main Menu

search

You are here

rPiBlink - revised

[last updated: 2016-07-27]
go to: Raspberry Pi page

-----

Added a LED and code for ~ 1/sec heartbeat
Added two pushbuttons, one to turn on the (previously blinked) LED when pressed,
and the other to exit (break out of) the infinite while True loop to end the program.
========================================#
# program name: MyProg04
# lights LED when PB input connected to 3.3v (through 1k resistor)
# break PB to exit inf loop
# heartbeat LED

import time
import RPi.GPIO as GPIO

# This sets the pin number, NOT the GPIO number
LED = 29
LED_hrtbt = 35
PB_LED = 31
PB_brk = 33

GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED, GPIO.OUT)
GPIO.setup(LED_hrtbt, GPIO.OUT)
GPIO.setup(PB_brk, GPIO.IN)
GPIO.setup(PB_LED, GPIO.IN)

# set up heartbeat
tPrev = time.time ()

while (1):

# heartbeat
tNow= time.time()
if tNow > (tPrev + 1):
tPrev = tNow

GPIO.output(LED_hrtbt, GPIO.HIGH)
time.sleep(0.1)
GPIO.output(LED_hrtbt, GPIO.LOW)

PBstate = GPIO.input(PB_LED)
if PBstate:
GPIO.output(LED, GPIO.HIGH)
else:
GPIO.output(LED, GPIO.LOW)

PBstate = GPIO.input(PB_brk)
if PBstate:
break

# --------------- END program ------------

.

.

.

eof