CamJam-EduKit / EduKit2

CamJam EduKit 2 - Sensors
MIT License
42 stars 27 forks source link

Worksheet 4 #3

Closed peternaz closed 6 years ago

peternaz commented 7 years ago

Can anyone give me some hints for wk sh 4 'Challenge'

Pyroseza commented 7 years ago

here's some pseudo code:

loop forever
    read the brightness from the Light Dependent Resistor
    if brightness is zero
        turn OFF the BLUE LED
        turn OFF the RED LED
        turn OFF the BUZZER
    else if brightness is dull
        turn ON the BLUE LED
        turn OFF the RED  LED
        turn OFF the BUZZER
    else if brightness is bright
        turn OFF the BLUE LED
        turn ON the RED LED
        turn OFF the BUZZER
    else if brightness is really bright
        turn OFF the RED LED
        turn OFF the BLUE LED
        turn ON the BUZZER
    sleep for a very small amount of time
peternaz commented 7 years ago

Thanks for your assistance. I've got the blue light on and the values printing, but blue light won't go out and buzzer won't come on

Pyroseza commented 7 years ago

Can you paste your script here so I can see what it is trying to do

peternaz commented 7 years ago

Hi Sorry for delay. THX for yor time and help

CamJam EduKit 2 - Sensors

Worksheet 4 - Light

Import Libraries

import time import RPi.GPIO as GPIO

Set the GPIO Mode

GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False)

A variable with the LDR reading pin number

PinLDR = 27

GPIO.setup(18, GPIO.OUT) GPIO.setup(24, GPIO.OUT) GPIO.setup(22, GPIO.OUT)

GPIO.output(18, GPIO.HIGH) GPIO.output(24, GPIO.HIGH) GPIO.output(22, GPIO.HIGH)

GPIO.output(18, GPIO.LOW) GPIO.output(24, GPIO.LOW) GPIO.output(22, GPIO.LOW)

GPIO.setup(PinLDR, GPIO.IN)

def ReadLDR(): LDRCount = 0 GPIO.setup(PinLDR, GPIO.OUT) GPIO.output(PinLDR, GPIO.LOW) time.sleep(0.1) GPIO.setup(PinLDR,GPIO.IN) while (GPIO.input(PinLDR) == GPIO.LOW): LDRCount += 1 return LDRCount

while True:

if (GPIO.input(PinLDR) == GPIO.LOW):
    GPIO.output(18, GPIO.LOW)
    GPIO.output(24, GPIO.LOW)
    GPIO.output(22, GPIO.LOW)
    time.sleep(0.5)

elif (GPIO.input(PinLDR) != GPIO.LOW):
    GPIO.output(18, GPIO.LOW)
    GPIO.output(24, GPIO.HIGH)
    GPIO.output(22, GPIO.LOW)

elif (GPIO.input(PinLDR) != GPIO.HIGH):
    GPIO.output(24, GPIO.LOW)
    GPIO.output(18, GPIO.HIGH)
    GPIO.output(22, GPIO.LOW)

elif (GPIO.input(PinLDR) == HIGH):
    GPIO.output(18, GPIO.LOW)
    GPIO.output(22, GPIO.HIGH)
    GPIO.output(24, GPIO.LOW)

print(ReadLDR())
time.sleep(1)

GPIO.cleanup()

Pyroseza commented 7 years ago

Okay a few things can change here.

First re-evaulate how you are checking the LDR and storing the value.

Then use what you have read and compare it to actual values instead of the built-in values of GPIO.LOW and GPIO.HIGH.

You can create a small script that just constantly reads and prints the value of the LDR so you can find out what values to use by moving a light source close to away from the LDR.

Then for the if statements use somelike this:

If LDRvalue >= very_bright Then turn then speaker on Turn both LEDs OFF Else if the LDRvalue >= bright Turn the speaker OFF Turn the RED LED ON Turn the BLUE LED OFF Else if the LDRvalue >= dull Turn the speaker OFF Turn the RED LED OFF Turn the BLUE LED ON Else Turn everything OFF

Let me know how you get on with this I would have to get in front of my PC to give you the actual code to do this.

peternaz commented 7 years ago

HI I'm having trouble working out how to store the LDRvalue

Pyroseza commented 6 years ago

Hi @peternaz, sorry for the delays in responding to you. Have you managed to figure this out on your own or would you still like some assistance?

peternaz commented 6 years ago

That's ok.

Yes still having trouble, need help

Pyroseza commented 6 years ago

Okay I've put a script together, I have not tested it, just coded what I think should work

take note that you have to modify some values to get it working correctly

#Import Libraries
import time
import RPi.GPIO as GPIO

#Set the GPIO Mode
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

#Set up the pins
PINLDR = 17
PINLEDRED = 22
PINLEDBLUE = 23
PINBUZZER = 27

#percentages for brightness control
#YOU MUST CHANGE THESE, REMEMBER THAT THE HIGHER THE NUMBER THE DARKER IT IS, CLOSER TO 0 THE BRIGHTER IT IS
REALLYBRIGHT=50
BRIGHT=500
DULL=1000

#LED pinds
GPIO.setup(PINLDR, GPIO.IN)
GPIO.setup(PINLEDRED, GPIO.OUT)
GPIO.setup(PINLEDBLUE, GPIO.OUT)
GPIO.setup(PINBUZZER, GPIO.OUT)

#read the brightness from the LDR
#higher numbers indicate darker conditions
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 (GPIO.input(PINLDR) == GPIO.LOW):
        LDRCount += 1 # Add one to the counter
    return LDRCount

while True:
    try:
        brightness = ReadLDR()
        GPIO.output(PINLEDRED, GPIO.LOW)  
        GPIO.output(PINLEDBLUE, GPIO.HIGH)
        GPIO.output(PINLEDBLUE, GPIO.LOW)
        print(brightness)
        if (brightness >= DULL):
            #this is dark, everything off
            GPIO.output(PINLEDRED, GPIO.LOW)
            GPIO.output(PINLEDBLUE, GPIO.LOW)
            GPIO.output(PINBUZZER, GPIO.LOW)
        elif (brightness < DULL and brightness >= BRIGHT):
            #this is dull, blue on
            GPIO.output(PINLEDRED, GPIO.LOW)
            GPIO.output(PINLEDBLUE, GPIO.HIGH)
            GPIO.output(PINBUZZER, GPIO.LOW)
        elif (brightness < BRIGHT and brightness >= REALLYBRIGHT):
            #this is bright, red on
            GPIO.output(PINLEDRED, GPIO.HIGH)
            GPIO.output(PINLEDBLUE, GPIO.LOW)
            GPIO.output(PINBUZZER, GPIO.LOW)
        elif (brightness <= REALLYBRIGHT):
            GPIO.output(PINLEDRED, GPIO.LOW)
            GPIO.output(PINLEDBLUE, GPIO.LOW)   
            GPIO.output(PINBUZZER, GPIO.HIGH)
        time.sleep(1)
    except:
      GPIO.cleanup()
Pyroseza commented 6 years ago

Here it is if you want to download it:

script

GeekyTim commented 6 years ago

Thanks for answering, Pyroseza.

Pyroseza commented 6 years ago

No problem @Geeky_Tim :)

Pyroseza commented 6 years ago

Fail... can’t mention on mobile