Making Things Interactive

March 27, 2008

PhotoSensor State Machine

Filed under: 8: State Machine,Brian Kish — bkish @ 1:49 pm


state-diagram.jpgThis a state machine that will be the basis for my final project of automated louvers. This uses 3 LEDs, a photoresistor, a potentiometer, and aservo to communicate. The user is able to set the light levels that they desire by turning a potentiometer. The machine then tells the person one of three things: Light is good, Its too Dark!, or Its too Bright. It glows 3 different LEDs to express which state it is in. There will be pictures and video to follow, once I find my camera :)

const int sGood= 0;
const int sBright = 1;
const int sDark = 2;

int servoPin = 2;     // Control pin for servo motor
int minPulse = 500;   // Minimum servo position
int maxPulse = 2500;  // Maximum servo position
int pulse = 0;        // Amount to pulse the servo
int goodLED= 6;
int darkLED= 7;
int brightLED = 5;

long lastPulse = 0;    // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses

int photoSensor = 0;  // the value returned from the analog sensor
int sensorPin = 1;    // the analog pin that the sensor's on
int potSwitch = 0;
int potMin = 0;
int potMax = 0;
int potPin = 0;

void setup() {
  pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
  pinMode(potSwitch, INPUT);
  pinMode(goodLED, OUTPUT);
  pinMode(darkLED, OUTPUT);
  pinMode(brightLED, OUTPUT);
  pulse = minPulse;           // Set the motor position value to the minimum
  digitalWrite(goodLED, LOW);
  digitalWrite(darkLED, LOW);
  digitalWrite(brightLED, LOW);


void loop() {
  photoSensor = analogRead(sensorPin);      // read the analog input
  potSwitch = analogRead(potPin);
  pulse = (photoSensor *19)/10 + minPulse;    // convert the analog value
  // to a range between minPulse
  // and maxPulse. 

  potMin = potSwitch-25;
  potMax = potSwitch+50;

  Serial.println((int)potSwitch);  // pulse the servo again if rhe refresh time (20 ms) have passed:

  if (millis() - lastPulse >= refreshTime) {
    digitalWrite(servoPin, HIGH);   // Turn the motor on
    delayMicroseconds(pulse);       // Length of the pulse sets the motor position
    digitalWrite(servoPin, LOW);    // Turn the motor off
    lastPulse = millis();           // save the time of the last pulse

  //good State
  if(photoSensor <= potMax && photoSensor >= potMin){
    digitalWrite(goodLED, HIGH);
    digitalWrite(darkLED, LOW);
    Serial.println("Light is good");
  // bright State
  if(photoSensor < potMin){
    digitalWrite(goodLED, LOW);
    digitalWrite(darkLED, LOW);
    Serial.println("Too Bright");

  // dark State
  if(photoSensor > potMax){
    digitalWrite(goodLED, LOW);
    digitalWrite(darkLED, HIGH);
    Serial.println("Too Dark");


1 Comment »

  1. Hey, check this out, solar-powered louvers:

    Comment by jet — March 28, 2008 @ 11:59 am | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

Blog at

%d bloggers like this: