Skip to content

Commit

Permalink
Updated to Python3
Browse files Browse the repository at this point in the history
  • Loading branch information
GeekyTim committed Feb 3, 2016
1 parent f5f4d69 commit 0dc5d5f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 44 deletions.
4 changes: 2 additions & 2 deletions Code/1-helloworld.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Print Hello World!
print "Hello World!"
# Print Hello World!
print("Hello World!")
7 changes: 5 additions & 2 deletions Code/2-LEDBuzz.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# CamJam Edukit 2 - Sensors
# Worksheet 2 - LEDs and Buzzer

# Import Python libraries
import RPi.GPIO as GPIO
import time
Expand All @@ -11,15 +14,15 @@
GPIO.setup(24, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)

print "Lights and sound on"
print("Lights and sound on")
GPIO.output(18, GPIO.HIGH)
GPIO.output(24, GPIO.HIGH)
GPIO.output(22, GPIO.HIGH)

# Pause for one second
time.sleep(1)

print "Lights and sound off"
print("Lights and sound off")
GPIO.output(18, GPIO.LOW)
GPIO.output(24, GPIO.LOW)
GPIO.output(22, GPIO.LOW)
Expand Down
5 changes: 4 additions & 1 deletion Code/3-temperature.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# CamJam EduKit 2 - Sensors
# Worksheet 3 - Temperature

# Import Libraries
import os
import glob
Expand All @@ -15,7 +18,7 @@
# A function that reads the sensors data
def read_temp_raw():
f = open(device_file, 'r') # Opens the temperature device file
lines = f.readlines() # Returns the text
lines = f.readlines() # Returns the text
f.close()
return lines

Expand Down
15 changes: 9 additions & 6 deletions Code/4-LDR.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
# CamJam EduKit 2 - Sensors
# Worksheet 3 - Light

# Import Libraries
import time
import RPi.GPIO as GPIO

# Set the GPIO Mode and set the pin to use for the
# Set the GPIO Mode and set the pin to use for the
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# A variable with the LDR reading pin number
PINLDR = 27

def ReadLDR():
LDRCount = 0 # Sets the count to 0
GPIO.setup(PINLDR, GPIO.OUT)
GPIO.output(PINLDR, GPIO.LOW)
time.sleep(0.1) # Drains all charge from the capacitor

GPIO.setup(PINLDR, GPIO.IN) # Sets the pin to be input
# While the input pin reads off or Low, count
# While the input pin reads 'off' or Low, count
while (GPIO.input(PINLDR) == GPIO.LOW):
LDRCount += 1 # Add one to the counter
return LDRCount
while True:
print ReadLDR()

while True:
print(ReadLDR())
time.sleep(1) # Wait for a second
25 changes: 14 additions & 11 deletions Code/5-PIR.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# CamJam EduKit 2 - Sensors
# Worksheet 2 - Movement

# Import Python header files
import RPi.GPIO as GPIO
import time
Expand All @@ -9,7 +12,7 @@
# Set a variable to hold the GPIO Pin identity
PinPIR = 17

print "PIR Module Test (CTRL-C to exit)"
print("PIR Module Test (CTRL-C to exit)")

# Set pin as input
GPIO.setup(PinPIR, GPIO.IN)
Expand All @@ -19,32 +22,32 @@
Previous_State = 0

try:
print "Waiting for PIR to settle ..."
print("Waiting for PIR to settle ...")
# Loop until PIR output is 0
while GPIO.input(PinPIR)==1:
while GPIO.input(PinPIR) == 1:
Current_State = 0

print " Ready
print(" Ready")
# Loop until users quits with CTRL-C
while True:
# Read PIR state
Current_State = GPIO.input(PinPIR)

# If the PIR is triggered
if Current_State==1 and Previous_State==0:
print " Motion detected!"
if Current_State == 1 and Previous_State == 0:
print(" Motion detected!")
# Record previous state
Previous_State=1
Previous_State = 1
# If the PIR has returned to ready state
elif Current_State==0 and Previous_State==1:
print " Ready"
Previous_State=0
elif Current_State == 0 and Previous_State == 1:
print(" Ready")
Previous_State = 0

# Wait for 10 milliseconds
time.sleep(0.01)

except KeyboardInterrupt:
print " Quit"
print(" Quit")

# Reset GPIO settings
GPIO.cleanup()
47 changes: 25 additions & 22 deletions Code/6-Alarm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# CamJam EduKit 2 - Sensors
# Worksheet 6 - Alarm

# Import Python header files
import RPi.GPIO as GPIO
import time
Expand All @@ -11,7 +14,7 @@
PinBlueLED = 24
PinBuzzer = 22

print "PIR Module Test (CTRL-C to exit)"
print("PIR Module Test (CTRL-C to exit)")

# Set pins as input/output
GPIO.setup(PinPIR, GPIO.IN)
Expand All @@ -24,44 +27,44 @@
Previous_State = 0

try:
print "Waiting for PIR to settle ..."
print("Waiting for PIR to settle ...")
# Loop until PIR output is 0
while GPIO.input(PinPIR)==1:
while GPIO.input(PinPIR) == 1:
Current_State = 0

print "Ready"
print(" Ready")
# Loop until users quits with CTRL-C
while True :
# Read PIR state
Current_State = GPIO.input(PinPIR)

if Current_State==1 and Previous_State==0:
if Current_State == 1 and Previous_State == 0:
# PIR is triggered
print " Motion detected!"
print(" Motion detected!")
# Flash lights and sound buzzer three times
for x in range(0,3):
GPIO.output(PinBuzzer, GPIO.HIGH)
GPIO.output(PinRedLED, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(PinRedLED, GPIO.LOW)
GPIO.output(PinBlueLED, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(PinBlueLED, GPIO.LOW)
GPIO.output(PinBuzzer, GPIO.LOW)
time.sleep(0.2)
for x in range(0, 3):
GPIO.output(PinBuzzer, GPIO.HIGH)
GPIO.output(PinRedLED, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(PinRedLED, GPIO.LOW)
GPIO.output(PinBlueLED, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(PinBlueLED, GPIO.LOW)
GPIO.output(PinBuzzer, GPIO.LOW)
time.sleep(0.2)

# Record previous state
Previous_State=1
Previous_State = 1

elif Current_State==0 and Previous_State==1:
elif Current_State == 0 and Previous_State == 1:
# PIR has returned to ready state
print " Ready"
Previous_State=0
print(" Ready")
Previous_State = 0

# Wait for 10 milliseconds
time.sleep(0.01)

except KeyboardInterrupt:
print "Quit"
print(" Quit")
# Reset GPIO settings
GPIO.cleanup()

0 comments on commit 0dc5d5f

Please sign in to comment.