Making Furniture Interactive

October 3, 2007

Kipum Lee’s Final 4 States

Filed under: Exercise 4: Four State Machines,Kipum Lee — Kip @ 11:57 pm

I have 3 sensors and 4 states.

1st State: One yellow LED is lit, w/ the light intensity proportional to the amount of light entering sensor 1 (on board).

2nd State: With the press of the red switch, the yellow LED starts to blink. Covering the other sensor on the yellow straw will bring the LED back to the 1st State.

3rd State: From the 2nd State, covering sensor 1 on the board will make it go to the 3rd State where the 3 red LED start to have a light show. Once again, covering the sensor on the yellow straw will bring the yellow LED back to the 1st State.

4th State: To go to the 4th State, simple press the red switch from the 3rd State. To go back to the 1st State, cover the sensor on the yellow straw.

Also, to go immediately to the 4th State from the 1st State, cover both photosensors (on yellow straw as well as the one on the board).

Code:
[sourecode language=”cpp”]

// Four States by Kip Lee, due 10/4/2007
// Instructor Mark Gross, Making Furniture Interactive

// Pin Assignments
int sensorPin = 2; // photosensor, in pin 2 in ANALOG port
int sensorPin2 = 1; // photosensor2, in pin 1 in ANALOG port
int ledpin = 9; // yellow light connected to digital pin 9
int ledMeterA = 7; // turn on LED’s for state 3
int ledMeterB = 5;
int ledMeterC = 4;
int bluePin = 13;
int photoVal = 0; // variable to store the value coming from the sensor
int photoVal2 = 0;
int state = 0; // start state 0
int button = 2; // switch, in pin 2 on DIGITAL port

void setup()
{
pinMode (sensorPin, INPUT); // activates sensor
pinMode(ledpin, OUTPUT); // turns on LED in pin #9 PWM
pinMode(ledMeterA, OUTPUT); // turns on LED in pin #7
pinMode(ledMeterB, OUTPUT); // turns on LED in pin #5
pinMode(ledMeterC, OUTPUT); // turns on LED in pin #4
pinMode(sensorPin2, INPUT); // activates sensor2
pinMode(bluePin, OUTPUT); // turns on LED in pin #10 PWM
Serial.begin (9600);
}

void loop() {
photoVal = analogRead(sensorPin);
photoVal2 = analogRead(sensorPin2);
//Serial.print (“Light from Potentiometer reads “);
//Serial.println (photoVal);
//Serial.print (“Light from 2nd sensor reads “);
//Serial.println (photoVal2);
if (state == 0) sleep ();
if (state == 1) arouse ();
if (state == 2) attract ();
if (state == 3) reward ();
}

// state: SLEEP = 0
void sleep () {
if (digitalRead (button) == 0) state = 1; // If switch pressed, goes to AROUSE
if (digitalRead (button) == 1) state = 0; // If switch left alone, stays in SLEEP
if (photoVal2 < 60 && photoVal < 60) state = 3; // If both photosensors covered, go to REWARD
analogWrite(ledpin, photoVal); // Digital pin 9 lights according to intensity of photocell light
}

// state: AROUSE = 1
void arouse () {
// if (digitalRead (button) == 0) state = 0; // If Pin 2 switch pressed, goes back to SLEEP
if (photoVal = 60) state = 1; // Stay in AROUSE
if (photoVal2 < 60) state = 0; // Go back to SLEEP
analogWrite(ledpin, 255); // Blinking
delay(500);
analogWrite(ledpin, 0);
delay(200);
}

// state: ATTRACT = 2
void attract () {
if (digitalRead (button) == 0) state = 3; // Go to REWARD
if (photoVal2 < 60) state = 0; // Go back to SLEEP
digitalWrite(ledMeterA, HIGH);
delay(30);
digitalWrite(ledMeterB, HIGH);
delay(30);
digitalWrite(ledMeterC, HIGH);
delay(30);

digitalWrite(ledMeterC, LOW);
delay(30);
digitalWrite(ledMeterB, LOW);
delay(30);
digitalWrite(ledMeterA, LOW);
delay(30);
}
// state: REWARD = 3
void reward () {
if (photoVal2 < 60) state = 0; // Cover photosensor #2 to go back to SLEEP
for(int i=0; i< 2;i++) // Pin 10 blinks 2 times
{
analogWrite(bluePin, 255);
delay(200);
analogWrite(bluePin, 0);
delay(200);
}
}
[/sourcecode]

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

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: