1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
   | import time
import grovepi
from grovepi import *
 
# Connect the Grove Light Sensor to analog port A0
# SIG,NC,VCC,GND
light_sensor = 0
 
# Connect the LED to digital port D4
# SIG,NC,VCC,GND
led = 4
 
# Turn on LED once sensor exceeds threshold resistance
threshold = 10
# Connect the Motion Sensor to digital port D3
pir_sensor = 8
motion=0
 
 
grovepi.pinMode(pir_sensor,"INPUT")
grovepi.pinMode(light_sensor,"INPUT")
grovepi.pinMode(led,"OUTPUT")
 
while True:
    try:
        # Get sensor ValueError
        sensor_value = grovepi.analogRead(light_sensor)
        # Calculate resistance of sensor in K
        resistance = (float)(1024 - sensor_value) * 10 / sensor_value
        if resistance > threshold: 
            motion=grovepi.digitalRead(pir_sensor) 
        elif motion==0 or motion==1:
            time.sleep(1)
            if motion==1:
                print("sensor_value = %d resistance = %.2f" %(sensor_value,  resistance))
                time.sleep(.3)
                grovepi.digitalWrite(led,1)
        else:
            if motion==0:
                grovepi.digitalWrite(led,0)
            time.sleep(1)
            print ('-')
 
    except IOError:
        grovepi.digitalWrite(led,0)
        print ("Error") | 
Partager