Capacitors for no bounce.
5v relay
 |
OPTOS
PC817
Opto
Isolator Board
 |
Video
Cambridge
Site Button Fire
LED
7
Segment
7-Segment
LED Decoder
Emulator
Emulator
II
Emulator
http://www.falstad.com/circuit/e-7segdecoder.html
https://github.com/arv/explorercanvas
Halt
Start Powerdown
Halt
Start |
Timed Button (high.. raising)
import time
import RPi.GPIO as GPIO
PIN = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
GPIO.wait_for_edge(PIN, GPIO.FALLING)
print "Pressed"
|
|
Be careful you copy the correct indentation, otherwise
Python will throw up some nasty errors!
On my system where I trigger a snapshot, I configure the pin
with a pullup. This saves having to mount the pullup
externally.
(I happen to use a different pin).
Oh. And in my script I have a "time.sleep (0.1)" in both
cases where the switch is stable. Otherwise the CPU will be
polling as fast as it can all the time, wasting power and
crowding out other programs that might need to do something.
>>> Less CPU button.py
import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
prev_input = 0
while True:
input = GPIO.input(17)
if ((not prev_input) and input):
os.system("sudo shutdown -h now")
prev_input = input
time.sleep(0.05)
Code: Select all
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.IN)
while True:
input_value = gpio.input(17)
if input_value == False:
print('The button has been pressed...')
while input_value == False:
input_value = gpio.input(17)
|
Emulate
keyboard |
|
#!/usr/bin/python
# Script for Raspberry Pi Internet Radio
# import
import RPi.GPIO as GPIO
import time
import os
PRESSTIME = 0
PLAYING = False
# Define GPIO to LCD mapping
LCD_RS = 25 # LCD Pin 4
LCD_E = 8 # LCD Pin 6
LCD_D4 = 4 # LCD Pin 11
LCD_D5 = 10 # LCD Pin 12
LCD_D6 = 9 # LCD Pin 13
LCD_D7 = 11 # LCD Pin 14
# Define GPIO for Radio Controls
# Button 1 goes to the Pi-Supply to turn the computer on
PREV = 23 # Button 2 - Previous track. Black wires
PLAYBUTTON = 27 # Button 3 - Play. Green Wires
STOPOFF = 22 # Button 4 - Brown wires. Press for stop. Long press for shutdown.
NEXT = 24 # Button 5 - Grey wires. Next track.
# Define some device constants
LCD_WIDTH = 20 # Maximum characters per line
LCD_CHR = True
LCD_CMD = False
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
# Timing constants
E_PULSE = 0.00005
E_DELAY = 0.00005
# What do you want the display to say when it's loading
startupline1 = "+------------------+"
startupline2 = "| Raspberry Pi |"
startupline3 = "| Internet Radio |"
startupline4 = "+------------------+"
shutdownline1 = "+------------------+"
shutdownline2 = "| Shutting Down |"
shutdownline3 = "| Please Wait |"
shutdownline4 = "+------------------+"
stopline1 = "+------------------+"
stopline2 = "| Music |"
stopline3 = "| Stopped |"
stopline4 = "+------------------+"
def main():
# Main program block
GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
GPIO.setup(LCD_E, GPIO.OUT) # E
GPIO.setup(LCD_RS, GPIO.OUT) # RS
GPIO.setup(LCD_D4, GPIO.OUT) # DB4
GPIO.setup(LCD_D5, GPIO.OUT) # DB5
GPIO.setup(LCD_D6, GPIO.OUT) # DB6
GPIO.setup(LCD_D7, GPIO.OUT) # DB7
GPIO.setup(NEXT, GPIO.IN) # Next Track
GPIO.setup(PREV, GPIO.IN) # Previous Track
GPIO.setup(PLAYBUTTON, GPIO.IN) # Play
GPIO.setup(STOPOFF, GPIO.IN) # Short Press is stop, long press is shutdown
# Initialise display
lcd_init()
# Send some test
lcd_byte(LCD_LINE_1, LCD_CMD)
lcd_string(startupline1,2)
lcd_byte(LCD_LINE_2, LCD_CMD)
lcd_string(startupline2,2)
lcd_byte(LCD_LINE_3, LCD_CMD)
lcd_string(startupline3,2)
lcd_byte(LCD_LINE_4, LCD_CMD)
lcd_string(startupline4, 2)
os.system("mpc play 1")
PLAYING = True
time.sleep(2)
while 1:
|
|