Making Things Interactive

March 27, 2008

State Machine: Light Motor

Filed under: 8: State Machine,Assignments,Thomas Hendrickson — tphendrickson @ 1:06 pm

I created a circuit with a motor that will turn on if the amount of light surrounding it increases. I calibrated a photosensor to accomplish this, which also will help in my final project.

State Diagram:

Circuit Photo:

Code:

 


int motorPin = 4;
int ledPin = 13;
int photoPin = 0;
int readIn = 0;
int initialRead = 0;
int activateVal = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(motorPin, OUTPUT);
  pinMode(photoPin, INPUT);

  blink();     //blinks the status LED
  initialRead = analogRead(photoPin); //gets initial reading for photosensor
  activateVal = initialRead * 2;      //the activation value will be double the initial
  blink();   

}

void loop()
{
    readIn = analogRead(photoPin);    //gets the current sensor reading

    while (readIn >= activateVal)     //while loop turns on motor and status LED if current reading
    {                                 //is greater than the activation value
      digitalWrite(ledPin, HIGH);
      digitalWrite(motorPin, HIGH);

      readIn = analogRead(photoPin);  //checks the current value
    }

    digitalWrite(ledPin, LOW);        //turns off motor and LED if current value is not great enough
    digitalWrite(motorPin, LOW);

}

void blink()
{
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);
}
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

Blog at WordPress.com.

%d bloggers like this: