Skip to content

Suggestions #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions 01_blinkingLed_1.py
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
import RPi.GPIO as GPIO
import time

LedPin = 11 # pin11
LedPin = 12 # declare pin as per diagram

GPIO.setmode(GPIO.BOARD) # Numbers pins by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set pin mode as output
@@ -29,4 +29,3 @@
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the flowing code will be executed.
GPIO.output(LedPin, GPIO.HIGH) # led off
GPIO.cleanup() # Release resource

7 changes: 3 additions & 4 deletions 01_blinkingLed_2.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
import RPi.GPIO as GPIO
import time

LedPin = 11 # pin11
LedPin = 12 # pin12 -- new pin declaration

def setup():
GPIO.setmode(GPIO.BOARD) # Numbers pins by physical location
@@ -13,10 +13,10 @@ def loop():
while True:
print '...led on'
GPIO.output(LedPin, GPIO.LOW) # led on
time.sleep(0.5)
time.sleep(1) # increase time to slow down blink
print 'led off...'
GPIO.output(LedPin, GPIO.HIGH) # led off
time.sleep(0.5)
time.sleep(1) # increase timing to slow down blink

def destroy():
GPIO.output(LedPin, GPIO.HIGH) # led off
@@ -28,4 +28,3 @@ def destroy():
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()

3 changes: 1 addition & 2 deletions 02_activeBuzzer.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
import RPi.GPIO as GPIO
import time

BeepPin = 11 # pin11
BeepPin = 12 # pin12 as per diagram

def setup():
GPIO.setmode(GPIO.BOARD) # Numbers pins by physical location
@@ -27,4 +27,3 @@ def destroy():
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()

2 changes: 1 addition & 1 deletion 03_passiveBuzzer.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
import RPi.GPIO as GPIO
import time

BZRPin = 11
BZRPin = 12 #delare pin as per diagram

GPIO.setmode(GPIO.BOARD) # Numbers pins by physical location
GPIO.setup(BZRPin, GPIO.OUT) # Set pin mode as output
3 changes: 1 addition & 2 deletions 06_relay.py
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ def setup():

def loop():
while True:
print '...clsoe'
print '...close' #corrected spelling
GPIO.output(RelayPin, GPIO.LOW)
time.sleep(0.5)
print 'open...'
@@ -28,4 +28,3 @@ def destroy():
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()

5 changes: 2 additions & 3 deletions 07_flowingLed.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
import RPi.GPIO as GPIO
import time

pins = [11, 12, 13, 15, 16, 18, 22, 7]
pins = [11, 12, 13, 15, 16, 18, 22, 7] #this in an array not a dictionary

def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
@@ -13,7 +13,7 @@ def setup():
def loop():
while True:
for pin in pins:
GPIO.output(pin, GPIO.LOW)
GPIO.output(pin, GPIO.LOW)
time.sleep(0.5)
GPIO.output(pin, GPIO.HIGH)

@@ -28,4 +28,3 @@ def destroy():
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()

7 changes: 3 additions & 4 deletions 08_breathingLed.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
import RPi.GPIO as GPIO
import time

LedPin = 12
LedPin = 12 #GPIO pin declaration. same as diagram

GPIO.setmode(GPIO.BOARD) # Numbers pins by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set pin mode as output
@@ -13,16 +13,15 @@

try:
while True:
for dc in range(0, 101, 4): # Increase duty cycle: 0~100
for dc in range(0, 85, 5): # Increase duty cycle: 0~100
p.ChangeDutyCycle(dc) # Change duty cycle
time.sleep(0.05)
time.sleep(1)
for dc in range(100, -1, -4): # Decrease duty cycle: 100~0
for dc in range(85, -1, -5): # Decrease duty cycle: 100~0
p.ChangeDutyCycle(dc)
time.sleep(0.05)
time.sleep(1)
except KeyboardInterrupt:
p.stop()
GPIO.output(LedPin, GPIO.HIGH) # turn off all leds
GPIO.cleanup()

13 changes: 6 additions & 7 deletions 09_rgbLed.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
import time

colors = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF]
pins = {'pin_R':11, 'pin_G':12, 'pin_B':13} # pins is a dict
pins = {'pin_R':11, 'pin_G':12, 'pin_B':13} # pins is a dictionary

GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
for i in pins:
@@ -25,11 +25,11 @@ def setColor(col): # For example : col = 0x112233
R_val = (col & 0x110000) >> 16
G_val = (col & 0x001100) >> 8
B_val = (col & 0x000011) >> 0
R_val = map(R_val, 0, 255, 0, 100)
G_val = map(G_val, 0, 255, 0, 100)
B_val = map(B_val, 0, 255, 0, 100)

R_val = map(R_val, 0, 25, 0, 100) #needed to change these values
G_val = map(G_val, 0, 50, 0, 100) #to see the LED change colour
B_val = map(B_val, 0, 75, 0, 100)

p_R.ChangeDutyCycle(R_val) # Change duty cycle
p_G.ChangeDutyCycle(G_val)
p_B.ChangeDutyCycle(B_val)
@@ -46,4 +46,3 @@ def setColor(col): # For example : col = 0x112233
for i in pins:
GPIO.output(pins[i], GPIO.HIGH) # Turn off all leds
GPIO.cleanup()

222 changes: 111 additions & 111 deletions 11_fourBitSegment.py
Original file line number Diff line number Diff line change
@@ -1,124 +1,124 @@
#!/usr/bin/env python
#!/usr/bin/env python

import RPi.GPIO as GPIO
import time
import RPi.GPIO as GPIO
import time

BIT0 = 3
BIT1 = 5
BIT2 = 24
BIT3 = 26
BIT0 = 3
BIT1 = 5
BIT2 = 24
BIT3 = 26

segCode = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f] #0~9
pins = [11,12,13,15,16,18,22,7,3,5,24,26]
bits = [BIT0, BIT1, BIT2, BIT3]
segCode = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f] #0~9
pins = [11,12,13,15,16,18,22,7,3,5,24,26]
bits = [BIT0, BIT1, BIT2, BIT3]

def print_msg():
print 'Program is running...'
print 'Please press Ctrl+C to end the program...'
def print_msg():
print 'Program is running...'
print 'Please press Ctrl+C to end the program...'

def digitalWriteByte(val):
GPIO.output(11, val & (0x01 << 0))
GPIO.output(12, val & (0x01 << 1))
GPIO.output(13, val & (0x01 << 2))
GPIO.output(15, val & (0x01 << 3))
GPIO.output(16, val & (0x01 << 4))
GPIO.output(18, val & (0x01 << 5))
GPIO.output(22, val & (0x01 << 6))
GPIO.output(7, val & (0x01 << 7))
def digitalWriteByte(val):
GPIO.output(11, val & (0x01 << 0))
GPIO.output(12, val & (0x01 << 1))
GPIO.output(13, val & (0x01 << 2))
GPIO.output(15, val & (0x01 << 3))
GPIO.output(16, val & (0x01 << 4))
GPIO.output(18, val & (0x01 << 5))
GPIO.output(22, val & (0x01 << 6))
GPIO.output(7, val & (0x01 << 7))

def display_1():
GPIO.output(BIT0, GPIO.LOW)
for i in range(10):
digitalWriteByte(segCode[i])
time.sleep(0.5)
def display_1(): #use only first segment
GPIO.output(BIT0, GPIO.LOW)
for i in range(10):
digitalWriteByte(segCode[i])
time.sleep(0.5)

def display_2():
for bit in bits:
GPIO.output(bit, GPIO.LOW)
for i in range(10):
digitalWriteByte(segCode[i])
time.sleep(0.5)
def display_2(): #use all 4 segments
for bit in bits:
GPIO.output(bit, GPIO.LOW)
for i in range(10):
digitalWriteByte(segCode[i])
time.sleep(0.5)

def display_3(num):
b0 = num % 10
b1 = num % 100 / 10
b2 = num % 1000 / 100
b3 = num / 1000
if num < 10:
GPIO.output(BIT0, GPIO.LOW)
GPIO.output(BIT1, GPIO.HIGH)
GPIO.output(BIT2, GPIO.HIGH)
GPIO.output(BIT3, GPIO.HIGH)
digitalWriteByte(segCode[b0])
elif num >= 10 and num < 100:
GPIO.output(BIT0, GPIO.LOW)
digitalWriteByte(segCode[b0])
time.sleep(0.002)
GPIO.output(BIT0, GPIO.HIGH)
GPIO.output(BIT1, GPIO.LOW)
digitalWriteByte(segCode[b1])
time.sleep(0.002)
GPIO.output(BIT1, GPIO.HIGH)
elif num >= 100 and num < 1000:
GPIO.output(BIT0, GPIO.LOW)
digitalWriteByte(segCode[b0])
time.sleep(0.002)
GPIO.output(BIT0, GPIO.HIGH)
GPIO.output(BIT1, GPIO.LOW)
digitalWriteByte(segCode[b1])
time.sleep(0.002)
GPIO.output(BIT1, GPIO.HIGH)
GPIO.output(BIT2, GPIO.LOW)
digitalWriteByte(segCode[b2])
time.sleep(0.002)
GPIO.output(BIT2, GPIO.HIGH)
elif num >= 1000 and num < 10000:
GPIO.output(BIT0, GPIO.LOW)
digitalWriteByte(segCode[b0])
time.sleep(0.002)
GPIO.output(BIT0, GPIO.HIGH)
GPIO.output(BIT1, GPIO.LOW)
digitalWriteByte(segCode[b1])
time.sleep(0.002)
GPIO.output(BIT1, GPIO.HIGH)
GPIO.output(BIT2, GPIO.LOW)
digitalWriteByte(segCode[b2])
time.sleep(0.002)
GPIO.output(BIT2, GPIO.HIGH)
GPIO.output(BIT3, GPIO.LOW)
digitalWriteByte(segCode[b3])
time.sleep(0.002)
GPIO.output(BIT3, GPIO.HIGH)
else:
print 'Out of range, num should be 0~9999 !'
def display_3(num):
b0 = (num % 10)
b1 = (num % 100) / 10
b2 = (num % 1000) / 100 #added brackets as it's good practice when
b3 = (num / 1000) #representing maths functions for readability
if num < 10:
GPIO.output(BIT0, GPIO.LOW)
GPIO.output(BIT1, GPIO.HIGH)
GPIO.output(BIT2, GPIO.HIGH)
GPIO.output(BIT3, GPIO.HIGH)
digitalWriteByte(segCode[b0])
elif num >= 10 and num < 100:
GPIO.output(BIT0, GPIO.LOW)
digitalWriteByte(segCode[b0])
time.sleep(0.002)
GPIO.output(BIT0, GPIO.HIGH)
GPIO.output(BIT1, GPIO.LOW)
digitalWriteByte(segCode[b1])
time.sleep(0.002)
GPIO.output(BIT1, GPIO.HIGH)
elif num >= 100 and num < 1000:
GPIO.output(BIT0, GPIO.LOW)
digitalWriteByte(segCode[b0])
time.sleep(0.002)
GPIO.output(BIT0, GPIO.HIGH)
GPIO.output(BIT1, GPIO.LOW)
digitalWriteByte(segCode[b1])
time.sleep(0.002)
GPIO.output(BIT1, GPIO.HIGH)
GPIO.output(BIT2, GPIO.LOW)
digitalWriteByte(segCode[b2])
time.sleep(0.002)
GPIO.output(BIT2, GPIO.HIGH)
elif num >= 1000 and num < 10000:
GPIO.output(BIT0, GPIO.LOW) #must open gate for that segment
digitalWriteByte(segCode[b0]) #light up correct led in that segment
time.sleep(0.002) #wait 2/1000th of a second
GPIO.output(BIT0, GPIO.HIGH) #close gate
GPIO.output(BIT1, GPIO.LOW) #open next gate for next segment
digitalWriteByte(segCode[b1])
time.sleep(0.002)
GPIO.output(BIT1, GPIO.HIGH)
GPIO.output(BIT2, GPIO.LOW)
digitalWriteByte(segCode[b2])
time.sleep(0.002)
GPIO.output(BIT2, GPIO.HIGH)
GPIO.output(BIT3, GPIO.LOW)
digitalWriteByte(segCode[b3])
time.sleep(0.002)
GPIO.output(BIT3, GPIO.HIGH)
else:
print 'Out of range, num should be 0~9999 !'

def setup():
GPIO.setmode(GPIO.BOARD) #Number GPIOs by its physical location
for pin in pins:
GPIO.setup(pin, GPIO.OUT) #set all pins' mode is output
GPIO.output(pin, GPIO.HIGH) #set all pins are high level(3.3V)
def setup():
GPIO.setmode(GPIO.BOARD) #Number GPIOs by its physical location
for pin in pins: # relates to digiatl write function
GPIO.setup(pin, GPIO.OUT) #set all pins' mode is output
GPIO.output(pin, GPIO.HIGH) #set all pins are high level(3.3V)

def loop():
while True:
print_msg()
display_1()
time.sleep(1)
display_2()
time.sleep(1)
def loop():
while True:
print_msg()
display_1()
time.sleep(1)
display_2()
time.sleep(1)

tmp = int(raw_input('Please input a num(0~9999):'))
for i in range(500):
display_3(tmp)
time.sleep(1)
tmp = int(raw_input('Please input a num(0~9999):'))
for i in range(500):
display_3(tmp) #calling function to ask for user input
time.sleep(1)

def destroy(): #When program ending, the function is executed.
for pin in pins:
GPIO.output(pin, GPIO.LOW) #set all pins are low level(0V)
GPIO.setup(pin, GPIO.IN) #set all pins' mode is input
def destroy(): #When program ending, the function is executed.
for pin in pins:
GPIO.output(pin, GPIO.LOW) #set all pins are low level(0V)
GPIO.setup(pin, GPIO.IN) #set all pins' mode is input

if __name__ == '__main__': #Program starting from here
setup()
try:
loop()
except KeyboardInterrupt:
destroy()
if __name__ == '__main__': #Program starting from here
setup()
try:
loop()
except KeyboardInterrupt:
destroy()