From e6c9d36b2c9963a74f14c012b6e2df981bccc7a8 Mon Sep 17 00:00:00 2001 From: CHRIS Date: Sat, 22 Jul 2017 02:21:13 +0800 Subject: [PATCH 1/8] added an extra comment --- 01_blinkingLed_2.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/01_blinkingLed_2.py b/01_blinkingLed_2.py index 9e47d25..2bb77fc 100755 --- a/01_blinkingLed_2.py +++ b/01_blinkingLed_2.py @@ -2,7 +2,7 @@ import RPi.GPIO as GPIO import time -LedPin = 11 # pin11 +LedPin = 11 # pin11 this declares a pin to use def setup(): GPIO.setmode(GPIO.BOARD) # Numbers pins by physical location @@ -28,4 +28,3 @@ def destroy(): loop() except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. destroy() - From ec70aa5f216ab4f682e8151903eea158d578918f Mon Sep 17 00:00:00 2001 From: CHRIS Date: Wed, 26 Jul 2017 23:36:59 +0800 Subject: [PATCH 2/8] testing branch and commit --- 01_blinkingLed_2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01_blinkingLed_2.py b/01_blinkingLed_2.py index 2bb77fc..e04d018 100755 --- a/01_blinkingLed_2.py +++ b/01_blinkingLed_2.py @@ -2,7 +2,7 @@ import RPi.GPIO as GPIO import time -LedPin = 11 # pin11 this declares a pin to use +LedPin = 11 # pin11 -- testing branches here too def setup(): GPIO.setmode(GPIO.BOARD) # Numbers pins by physical location From ff9e9744df8a32efd0d709d40b594a203396e23b Mon Sep 17 00:00:00 2001 From: CHRIS Date: Wed, 26 Jul 2017 23:41:05 +0800 Subject: [PATCH 3/8] test --- 01_blinkingLed_2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01_blinkingLed_2.py b/01_blinkingLed_2.py index e04d018..91f02b2 100755 --- a/01_blinkingLed_2.py +++ b/01_blinkingLed_2.py @@ -2,7 +2,7 @@ import RPi.GPIO as GPIO import time -LedPin = 11 # pin11 -- testing branches here too +LedPin = 11 # pin11 -- testing branches here too and now using ADD def setup(): GPIO.setmode(GPIO.BOARD) # Numbers pins by physical location From 2b117e48e8106174559007303ca17d3fa0246c3c Mon Sep 17 00:00:00 2001 From: CHRIS Date: Wed, 6 Sep 2017 14:15:16 +0800 Subject: [PATCH 4/8] changed timing and comments --- 01_blinkingLed_2.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/01_blinkingLed_2.py b/01_blinkingLed_2.py index 91f02b2..d9d2046 100755 --- a/01_blinkingLed_2.py +++ b/01_blinkingLed_2.py @@ -2,7 +2,7 @@ import RPi.GPIO as GPIO import time -LedPin = 11 # pin11 -- testing branches here too and now using ADD +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 From 7920a46e130ac2dd017d3877506e8031142e80a0 Mon Sep 17 00:00:00 2001 From: CHRIS Date: Wed, 6 Sep 2017 14:27:36 +0800 Subject: [PATCH 5/8] changed values in function setColor to make work and added comments --- 08_breathingLed.py | 7 +++---- 09_rgbLed.py | 13 ++++++------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/08_breathingLed.py b/08_breathingLed.py index 8164d11..aa40990 100755 --- a/08_breathingLed.py +++ b/08_breathingLed.py @@ -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,11 +13,11 @@ 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) @@ -25,4 +25,3 @@ p.stop() GPIO.output(LedPin, GPIO.HIGH) # turn off all leds GPIO.cleanup() - diff --git a/09_rgbLed.py b/09_rgbLed.py index 591c9d9..c34ebde 100755 --- a/09_rgbLed.py +++ b/09_rgbLed.py @@ -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() - From 295d17c345987b4df703c80919113064c0594ffa Mon Sep 17 00:00:00 2001 From: CHRIS Date: Wed, 6 Sep 2017 14:32:24 +0800 Subject: [PATCH 6/8] added comments and corrected message spelling --- 06_relay.py | 3 +-- 07_flowingLed.py | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/06_relay.py b/06_relay.py index 5741184..f9c2eee 100755 --- a/06_relay.py +++ b/06_relay.py @@ -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() - diff --git a/07_flowingLed.py b/07_flowingLed.py index 6e06242..005f34d 100755 --- a/07_flowingLed.py +++ b/07_flowingLed.py @@ -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() - From 917cb56f5621615d2686a95a0f003a9787d61e7f Mon Sep 17 00:00:00 2001 From: CHRIS Date: Wed, 6 Sep 2017 14:41:03 +0800 Subject: [PATCH 7/8] added explanatory commmets to file 11 --- 11_fourBitSegment.py | 222 +++++++++++++++++++++---------------------- 1 file changed, 111 insertions(+), 111 deletions(-) diff --git a/11_fourBitSegment.py b/11_fourBitSegment.py index b1be9b4..aebf809 100755 --- a/11_fourBitSegment.py +++ b/11_fourBitSegment.py @@ -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() From 5ec4593c9680cef9548b41985c1c3299f0e73c0f Mon Sep 17 00:00:00 2001 From: CHRIS Date: Wed, 6 Sep 2017 14:46:11 +0800 Subject: [PATCH 8/8] corrected pins decs to match build diagram --- 01_blinkingLed_1.py | 3 +-- 02_activeBuzzer.py | 3 +-- 03_passiveBuzzer.py | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/01_blinkingLed_1.py b/01_blinkingLed_1.py index 29b3330..b8f3571 100755 --- a/01_blinkingLed_1.py +++ b/01_blinkingLed_1.py @@ -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 - diff --git a/02_activeBuzzer.py b/02_activeBuzzer.py index f4b4fee..8ec2af2 100755 --- a/02_activeBuzzer.py +++ b/02_activeBuzzer.py @@ -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() - diff --git a/03_passiveBuzzer.py b/03_passiveBuzzer.py index 9fd7b3a..084367e 100755 --- a/03_passiveBuzzer.py +++ b/03_passiveBuzzer.py @@ -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