Light activated headlights for the Pi-Car.

Code for the head lights.

photocell

#!/usr/bin/env python

# Example for RC timing reading for Raspberry Pi
# Must be used with GPIO 0.3.1a or later - earlier verions
# are not fast enough!

import RPi.GPIO as GPIO, time, os

#referance name and gpio pin number
HLED1  = 22
HLED2  = 24
sensor = 26

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#Remember to setup the GPIO pin as input or output
GPIO.setup(HLED1, GPIO.OUT)
GPIO.setup(HLED2, GPIO.OUT)

DEBUG = 1
GPIO.setmode(GPIO.BOARD)

runningLights = (True)

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

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

        return reading

print ("Light program running!!!")
while (runningLights == True):
        lightSensor = RCtime(sensor)
        #print lightSensor

        if ( lightSensor > 400):
                GPIO.output(HLED1,1)
                GPIO.output(HLED2,1)
        elif ( lightSensor < 400):
                GPIO.output(HLED1,0)
                GPIO.output(HLED2,0)

GPIO.cleanup()

This code was adapted from the tutorial from Adafruit . The only change I have made is setting up the 2 LED`s (HLED1 and HLED2 ) and added an IF statement . The IF statement switch`s on the led`s if the light drops below 400 and of if it`s over 400. I got this figure by hours of experimentation by switching the bedroom light on and off. The print statement is there just so I know the program is running correctly. Hopefully in the future I will ingrate this code with the main code.

  
Tagged , , , . Bookmark the permalink.

Comments are closed.