Professional Documents
Culture Documents
# GrovePi + Grove Light Sensor + LED - these are comments for the coder's use
# http://www.seeedstudio.com/wiki/Grove_-_Light_Sensor - this is an info page
online for the light sensor
# http://www.seeedstudio.com/wiki/Grove_-_LED_Socket_Kit - this is an info page online for the LED light
import time - this records the current time
import grovepi - this will import the pre-written software by grovepi
# Connect the Grove Light Sensor to analog port A0 - another comment for the coder
# SIG, NC, VCC, GND
led = 4 - this labels the LED as a value of 4
# Turn on LED once sensor exceeds threshold resistance - this means that if the light sensor detects light, it will
start to record the bright/darkness
threshold = 10 - in this case, the threshold for recording is 10
grovepi.pinMode(light_sensor, "INPUT") - this means that what the light sensor records is the input or
information
grove.pinMode(led, "OUTPUT") - this means that what the LED does is the result or output
while True: - this means that if the following is accurate, do these actions:
try:
# Get sensor value - another comment, symbolised by a hashtag
sensor_value = grovepi.analogRead(light_sensor) - this collects the value
of the sensor
# Calculate resistance of sensor in K 0 comment
resistance = (float) (1023 - sensor_value) * 10 / sensor_value - this
calculates the resistance of the sensor in terms of K, a unit of measurement
if resistance > threshold: - if (~) is greater than (~), then do this:
# Send HIGH to switch on LED - comment
grovepi.digitalWrite(led, 1) - this switches on the LED if the resistance is
higher than the threshold
else: - if the above is incorrect, then do the following:
# Send LOW to switch on LED
grovepi.digitalWrite(led, 0) - comment
print "sensor_value =", sensor_value, " resistance =", resistance - this
creates labels for the information so that it makes sense
time.sleep(.5) - this creates a pause
except 10Error: - if there is an error in the system, this will be printed
print "Error"
# GrovePi + Grove Sound Sensor + Grove LED - comments for what this sensor is