Skip to content

Instantly share code, notes, and snippets.

@RyanFleck
Created September 12, 2021 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyanFleck/c6480ace0f7e9089aa196172d9002b2d to your computer and use it in GitHub Desktop.
Save RyanFleck/c6480ace0f7e9089aa196172d9002b2d to your computer and use it in GitHub Desktop.
import RPi.GPIO as GPIO
from PCF8574 import PCF8574_GPIO
from Adafruit_LCD1602 import Adafruit_CharLCD
from time import sleep, strftime, time
from datetime import datetime
from threading import Lock
import pigpio # run sudo pigpiod
# Simple pi program to run three buttons, an LCD, and a servo.
# Uses code from Freenove kit scripts.
#####################################################################
# Constants
#####################################################################
green_button_pin = 13
blue_button_pin = 5
red_button_pin = 6
servo_pin = 23
message_time_delay = 2 # Seconds
#####################################################################
# Global Variables
#####################################################################
# Use physical pin numbering
GPIO.setmode(GPIO.BCM)
# Servo
GPIO.setup(servo_pin, GPIO.OUT)
class Servo:
lock = Lock()
pwm = None
def __init__(self):
with self.lock:
self.pwm = pigpio.pi()
self.pwm.set_mode(servo_pin, pigpio.OUTPUT)
self.pwm.set_PWM_frequency(servo_pin, 50)
def goto(self, position):
with self.lock:
self.pwm.set_servo_pulsewidth(servo_pin, position)
# LCD Message
class Message:
text = ""
time_posted = 0
lock = Lock()
def set(self, new_message):
with self.lock:
self.text = new_message.center(16)
self.time_posted = time()
message = Message()
servo = Servo()
# LCD Setup
PCF8574_address = 0x27 # I2C address of the PCF8574 chip.
PCF8574A_address = 0x3F # I2C address of the PCF8574A chip.
mcp = None
# Create PCF8574 GPIO adapter.
try:
mcp = PCF8574_GPIO(PCF8574_address)
except:
try:
mcp = PCF8574_GPIO(PCF8574A_address)
except:
print ('I2C Address Error !')
exit(1)
lcd = Adafruit_CharLCD(pin_rs=0, pin_e=2, pins_db=[4,5,6,7], GPIO=mcp)
#####################################################################
# Functions
#####################################################################
def green_button_callback(channel, message, servo):
print("Green button was pushed!")
message.set("GREEN BUTTON")
servo.goto(500)
def blue_button_callback(channel, message, servo):
print("Blue button was pushed!")
message.set("BLUE BUTTON")
servo.goto(1500)
def red_button_callback(channel, message, servo):
print("Red button was pushed!")
message.set("RED BUTTON")
servo.goto(2500)
def get_cpu_temp(): # get CPU temperature and store it into file "/sys/class/thermal/thermal_zone0/temp"
tmp = open('/sys/class/thermal/thermal_zone0/temp')
cpu = tmp.read()
tmp.close()
return '{:.2f}'.format( float(cpu)/1000 ) + ' C'
def get_time_now(): # get system time
return datetime.now().strftime(' %H:%M:%S')
def loop():
mcp.output(3,1) # turn on LCD backlight
lcd.begin(16,2) # set number of LCD lines and columns
while(True):
#lcd.clear()
lcd.setCursor(0,0) # set cursor position
if(time() - message.time_posted < message_time_delay):
lcd.message(message.text + '\n')
else:
lcd.message( ('CPU: ' + get_cpu_temp()).center(16) + '\n' )
lcd.message( get_time_now() ) # display the time
sleep(0.5)
def destroy():
"""Kill everything"""
pwm.stop()
lcd.clear()
#####################################################################
# Set up GPIO Pins
#####################################################################
# Buttons
# Set pins to be inputs and set initial value to be pulled low (off)
GPIO.setup(green_button_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(blue_button_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(red_button_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Setup event on pin 10 rising edge
GPIO.add_event_detect(green_button_pin, GPIO.RISING, callback=lambda x: green_button_callback(x, message, servo), bouncetime=500)
GPIO.add_event_detect(blue_button_pin, GPIO.RISING, callback=lambda x: blue_button_callback(x, message, servo), bouncetime=500)
GPIO.add_event_detect(red_button_pin, GPIO.RISING, callback=lambda x: red_button_callback(x, message, servo), bouncetime=500)
try:
loop()
except KeyboardInterrupt:
destroy()
GPIO.cleanup() # Clean up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment