Making Furniture Interactive

October 3, 2007

Interactive LEDs

Filed under: Exercise 4: Four State Machines,Jamin Hegeman — Jamin Hegeman @ 5:57 pm

This project has two main states.

Sleep: By default, the program is asleep.

Awake: If the photocell gets dark, it awakens. The four LEDs fade in and out in series. After a few series without being interacted with, it goes back to sleep.

There are two other sub-states.

Attract: If it’s asleep and you don’t wake it up for a while, all the LEDs blink to attract your attention.

Reward: If it’s awake and you continue to play with it (cover the photocell) it rewards you with flashing LEDs.

int potPin = 2; // select the input pin for the potentiometer
int led1 = 3; // select the pin for the LED
int led2 = 6;
int led3 = 9;
int led4 = 11;

int val = 0; // variable to keep the actual value
int lightLimit = 40;

int energy = 0; // counter default
int count = 0; // counter default
boolean awake = false; // asleep
boolean fallAsleep = false;
int awakeTimer = 0; //control how long it will stay awake
int awakeLimit = 3;
int attractTimer = 0; //controls how long it waits to attract
int attractLimit = 500;
int blinkDelayOn = 10;
int blinkDelayOff = 30;

int timer = 0;

void setup() {
pinMode(led1, OUTPUT); // declare the ledPin as an OUTPUT
pinMode(led2, OUTPUT); // declare the ledPin as an OUTPUT
pinMode(led3, OUTPUT); // declare the ledPin as an OUTPUT
pinMode(led4, OUTPUT); // declare the ledPin as an OUTPUT
//lightLimit = val/20;
Serial.begin(9600);
attract();
}

void loop() {
val = analogRead(potPin); // read the value from the sensor

if (awake == false && val > lightLimit) {
attractTimer++;
//Serial.print(“attractTimer = “);
//Serial.println(attractTimer);
}

if(attractTimer > attractLimit) {
attract();
attractTimer = 0;
Serial.println(“attract”);
}

// wake up
if(val lightLimit && awake == true) {
awakeTimer++;
//Serial.print(“awakeTimer = “);
//Serial.println(awakeTimer);
awakeState();
}

//reward
if(awake == true && val 0 && val > 5) {
reward();
Serial.println(“reward”);
awakeTimer = 1;
}

// put it to sleep
if(awakeTimer > awakeLimit || val < 5) {
Serial.println(“sleep”);
sleep();
}
Serial.println(val);
}

void awakeState() {
blink(led1);
blink(led2);
blink(led3);
blink(led4);
}

void reward() {
digitalWrite(led1, HIGH); // sets the LED on
digitalWrite(led3, HIGH); // sets the LED on
delay(100);
digitalWrite(led1, LOW); // sets the LED off
digitalWrite(led3, LOW); // sets the LED off
delay(100);
digitalWrite(led2, HIGH); // sets the LED on
digitalWrite(led4, HIGH); // sets the LED on
delay(100);
digitalWrite(led2, LOW); // sets the LED off
digitalWrite(led4, LOW); // sets the LED off
}

void sleep() {
awake = false;
awakeTimer = 0;
Serial.println(“sleep”);
}

void attract() {
digitalWrite(led1, HIGH); // sets the LED on
digitalWrite(led3, HIGH); // sets the LED on
digitalWrite(led2, HIGH); // sets the LED on
digitalWrite(led4, HIGH); // sets the LED on
delay(100);
digitalWrite(led1, LOW); // sets the LED off
digitalWrite(led3, LOW); // sets the LED off
digitalWrite(led2, LOW); // sets the LED off
digitalWrite(led4, LOW); // sets the LED off
}

void blink(int pin) {
fadeIn(pin);
delay(blinkDelayOn);
fadeOut(pin);
delay(blinkDelayOff);
}

void fadeIn(int pin) {
for(int value = 0 ; value =0; value-=5) // fade out (from max to min)
{
analogWrite(pin, value);
delay(10);
}
}

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

Blog at WordPress.com.

%d bloggers like this: