You are on page 1of 3

Coding

# 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

# http://www.seeedstudio.com/wiki/Grove_-_Sound_Sensor - this takes you to an


online page about the sound sensor
# http://www.seeedstudio.com/wiki/Grove_-_LED_Socket_Kit - this takes you to
an online page about the LEDs
import time - this records the current time
import grovepi - this imports written software from the grovePi
# Connect the Grove Sound Sensor to analog port A0 - a comment for the reader's
use/knowledge
# SIG, NC, VCC, GND - another comment
sound_ sensor = 0 - this labels the value of the (sound_sensor) as 0
# Connect the Grove LED to digital port D5 - another comment
# SIG, NC, VCC, GND - a comment
led = 5 - this labels the value of the LED as 5
grovepi .pinMode(sound_sensor , "INPUT" ) - this labels the sound sensor as the
input or source of information
grovepi .pinMode(led , "OUTPUT") - this labels the LED as the source of output
# The threshold to turn the led on 400.00 * 5 / 1024 = 1.95v - a comment
threshold_value = 400 - this makes the threshold value 400
while True: - if the following is true, then do this:
try:
# Read the sound level - this is another comment
sensor_value = grovepi .analogRead(sound_sensor) - this will measure
the level of sound the sensor detects
# If loud, illuminate LED, otherwise dim
if sensor_value > threshold_value: - if the sound goes above the
threshold, which means it is noisy, then the LED switches on
grovepi .digitalWrite(led,1)
else:
grovepi.digitalWrite(led,0) - if the room is quiet, however, the LED
will remain off
print "sensor_value =" , sensor_value - this organizes the collected
information on the screen
time.sleep(.5) - this creates a pause
except 10Error: - if there is an error, this will be printe (print "Error")
# GrovePi + Grove Temperature & Humidity Sensor Pro - a comment for the
reader's use

# http://www.seeedstudio.com/wiki/Grove__Temperature_and_Humidity_Sensor_Pro - this is an online page which links to


the sensor
import grovepi - this imports the relevant software to the Pi
# Connect the Grove Temperature & Humidity Sensor Pro to digital port D4 - a
comment for the reader
# SIG, NC, VCC, GND - another comment, signified by a hashtag
sensor = 4 - this labels the sensor's value as 4
while True: - if the following is correct, this will happen:
try:
[temp,humidity] = grovepi .dht(sensor,0) - the Pi will record the
temperature and humidity of the room
print "temp =", temp, " humidity =', humidity - this displays the info
for the reader
except 10Error: - if there is a problem, it will be displayed by 'ERROR'
print "Error"

You might also like