Making Things Interactive

February 5, 2008

Photosensor

Filed under: 4: Counting Sensor Input,Tiffany Yang — tyang1 @ 8:49 am

Since my project has to do with light in a night table, I thought the first step would be to use a photosensor. When light is detected, my arduino will say good night.


int ledPin = 13;                //sets LEDs to pin 13
int photoPin = 0;               //photoresistor connected to Analog 0
int light;                      //variable for reading photosensor
int photoState;                 //variable to hold the state of the sensor
int lightPresses;               //how many times the light is turned off or on

void setup() {
  pinMode(ledPin, OUTPUT);             //set the ledPin as an Output
  Serial.begin(9600);                  //set up Serial Library at 9600 bps
  pinMode(photoPin, INPUT);            //sets photoresistor as INPUT
  photoState = analogRead(photoPin);   //read the initial state of photosensor
}  

void loop() {
  light = analogRead(photoPin);        // reads resistance from the sensor
  if (light != photoState) {
    if (light <= 500) {                  //if there is no light
      digitalWrite(ledPin, HIGH);          // LEDs turn on
      lightPresses++;
      Serial.print("Goodnight for the ");
      Serial.print(lightPresses);
      Serial.println("th times");
      delay(1000);
    }
    else {                               //if there is light
      digitalWrite(ledPin, LOW);         //LEDs are off
      Serial.println("Goodmorning");
      delay(1000);
    }
  }
  photoState = light;
} 

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: