Workshop kit code

None of the above? Put it here! Boop!
gb4192
n00b
Posts: 14
Joined: Fri Nov 09, 2018 2:40 pm

Re: Workshop kit code

Post by gb4192 » Mon Feb 25, 2019 5:33 pm

8_ldr_2.py

Code: Select all

#!/usr/bin/python

import os
import datetime
from time import sleep
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

def RCtime (RCpin):
    reading = 0
    GPIO.setup(RCpin, GPIO.OUT)
    GPIO.output(RCpin, GPIO.LOW)
    sleep(.1)

    GPIO.setup(RCpin, GPIO.IN)
    # This takes about 1 millisecond per loop cycle.
    while (GPIO.input(RCpin) == GPIO.LOW):
        reading += 1
    return reading

while True: 
    GetDateTime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    LDRReading = RCtime(3)

    # Open a file.
    fo = open("/home/pi/gpio/8_light.txt", "ab")
    fo.write (GetDateTime)
    fo.write (",")
    LDRReading = str(LDRReading)
    fo.write (LDRReading)
    print LDRReading
    fo.write ("\n")

    # Close opened file.
    fo.close()
    sleep(1)

gb4192
n00b
Posts: 14
Joined: Fri Nov 09, 2018 2:40 pm

Re: Workshop kit code

Post by gb4192 » Mon Feb 25, 2019 5:34 pm

9_pir.py

Code: Select all

#!/usr/bin/python

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(27,GPIO.OUT)

GPIO_PIR = 7

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

# Set pin as input.
GPIO.setup(GPIO_PIR,GPIO.IN)

Current_State  = 0
Previous_State = 0

try:
    print "Waiting for the PIR to settle ..."
    # Loop until PIR output is 0.
    while GPIO.input(GPIO_PIR)==1:
        Current_State  = 0 
    print "  Ready" 
    # Loop until users quits with CTRL-C.
    while True :
        # Read PIR state.
        Current_State = GPIO.input(GPIO_PIR)
        if Current_State==1 and Previous_State==0:
            # PIR is triggered
            print "  Motion detected!"
            # Record previous state.
            GPIO.output(27,GPIO.HIGH)
            sleep(1)
            GPIO.output(27,GPIO.LOW)
            Previous_State=1
        elif Current_State==0 and Previous_State==1:
            # PIR has returned to ready state.
            print "  Ready"
            Previous_State=0
        # Wait for 10 milliseconds.
        sleep(0.01)

# Clean up gpio pins when we exit the script.
except KeyboardInterrupt:
    print "  Quit" 
    # Reset GPIO settings.
    GPIO.cleanup()

gb4192
n00b
Posts: 14
Joined: Fri Nov 09, 2018 2:40 pm

Re: Workshop kit code

Post by gb4192 » Mon Feb 25, 2019 5:34 pm

9_pir_2.py

Code: Select all

#!/usr/bin/python

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO_RED_LED = 27
GPIO_BLUE_LED = 17
GPIO_BUZZER = 22
GPIO.setup(GPIO_BLUE_LED,GPIO.OUT) # Blue LED.
GPIO.setup(GPIO_RED_LED,GPIO.OUT) # Red LED.
GPIO.setup(GPIO_BUZZER,GPIO.OUT) # Buzzer.

GPIO_PIR = 7

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

# Set pin as input.
GPIO.setup(GPIO_PIR, GPIO.IN)

Current_State  = 0
Previous_State = 0
RED_LED = 0
BLUE_LED = 1
Current_LED = RED_LED

try:
    print "Waiting for the PIR to settle ..."
    # Loop until PIR output is 0.
    while GPIO.input(GPIO_PIR) == 1 :
        Current_State = 0
    print "  Ready" 
    # Loop until users quits with CTRL-C.
    while True :
        # Read PIR state.
        Current_State = GPIO.input(GPIO_PIR)
        if Current_State == 1 and Previous_State == 0 :
            # PIR is triggered
            print "  Motion detected!"
            # Record previous state.
            if Current_LED == RED_LED :
                GPIO.output(GPIO_RED_LED, GPIO.HIGH)
            else:
                GPIO.output(GPIO_BLUE_LED, GPIO.HIGH)
            GPIO.output(GPIO_BUZZER, GPIO.HIGH)
            sleep(.2)
            GPIO.output(GPIO_BUZZER, GPIO.LOW)
            sleep(.8)
            if Current_LED == RED_LED:
                GPIO.output(GPIO_RED_LED, GPIO.LOW)
                Current_LED=BLUE_LED
            else:
                GPIO.output(GPIO_BLUE_LED, GPIO.LOW)
                Current_LED = RED_LED
            Previous_State = 1
        elif Current_State == 0 and Previous_State == 1:
            # PIR has returned to ready state.
            print "  Ready"
            Previous_State = 0
        # Wait for 10 milliseconds.
        sleep(0.01)

# Clean up gpio pins when we exit the script.
except KeyboardInterrupt:
    print "  Quit" 
    # Reset GPIO settings.
    GPIO.cleanup()

gb4192
n00b
Posts: 14
Joined: Fri Nov 09, 2018 2:40 pm

Re: Workshop kit code

Post by gb4192 » Mon Feb 25, 2019 5:35 pm

That's it!

Overall the kit is ok but there is so much scope to do more and have different programs that build upon the last and make more use of the hardware.

Post Reply