/*
SparkFun Tinker Kit
Example sketch 06
PHOTORESISTOR
Read a photoresistor (light sensor) to detect "darkness" an
d turn on an LED when it is "dark" and turn back off again whe
n it is "bright."
This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK informatio
n.
Visit http://www.arduino.cc to learn more about Arduino.
*/
// As usual, we'll create constants to name the pins we're usi
ng.
// This will make it easier to follow the code below.
const int sensorPin = 0;
const int ledPin = 9;
// We'll also set up some global variables for the light leve
l a calibration value and //and a raw light value
int lightCal;
int lightVal;
void setup()
{
// We'll set up the LED pin to be an output.
pinMode(ledPin, OUTPUT);
lightCal = analogRead(sensorPin);
//we will take a single reading from the light sensor and st
ore it in the lightCal //variable. This will give us a
prelinary value to compare against in the loop
}
void loop()
{
//Take a reading using analogRead() on sensor pin and store
it in lightVal
lightVal = analogRead(sensorPin);
//if lightVal is less than our initial reading (lightCal) mi
nus 50 it is dark and //turn pin 9 HIGH. The (
50) par
t of the statement sets the sensitivity. The smaller //t
he number the more sensitive the circuit will be to variances
in light.
if(lightVal < lightCal
50)
{
digitalWrite(9,HIGH);
}
//else, it is bright, turn pin 9 LOW
else
{
digitalWrite(9,LOW);
Page 37 of 63