Making Furniture Interactive

October 5, 2007

Imran’s Exercise 4 – milo

Filed under: Exercise 4: Four State Machines,Imran Sobh — imranixd @ 1:11 am

The set-up
I made a mini-robot named milo. He has 2 eye LEDs, 4 stomach LEDs, a piezo sensor in the back of his head, a photosensor in his mouth, and a DC motor in the top of his head. Feed him light to give him energy, then let him release it through is propeller.

Sleep: In this state milo’s eyes glow faintly while he sleeps, if you open the back of his head and touch the piezo sensor he wakes up and goes to the arousal state.

Arousal: In this state milo is alert and blinking his eyes. If you leave him for too long he will go back to sleep, otherwise feed him light to get to the next state

Attract: In this state milo is filling up with the light you feed him as shown by the lights on his body. If you stop filling before the maximum amount, he drains the light and returns to the arousal state. Otherwise filling him to the maximum amount allows you to move on to the reward state.

Reward: You can only get to this state when milo is fully charged with light. Once he is full, if you press on the piezo in the back of his head, he will discharge the energy by spinning his head-propeller and return to the arousal state.

Documentation photos


int value = 0;                            // variable to keep the actual value
int ledpinEYE = 9;                           // light connected to digital pin 9
int ledpinEYE2 = 10;
//int buttonPin = ;

boolean fillingUp = true;
int motorPin = 8;
int transistorPin = 11;
int piezoSensorPin = 0;
int piezoVal = 0;

int state = 1; // 1=sleep, 2=attract, 3=arousal, 4=award;

int ledMeter1 = 4;
int ledMeter2 = 5;
int ledMeter3 = 6;
int ledMeter4 = 7;

int outputLED = 3;

int photoPin = 2;

int photoVal = 0;
int gasoline = 0;
int rewardCount = 0;
int fillUpCount = 0;
int attractTime = 0;
int arrouseTime = 0;

void setup()
{
Serial.begin(9600);
pinMode(ledpinEYE, OUTPUT);
pinMode(ledpinEYE2, OUTPUT);

pinMode(motorPin, OUTPUT);
pinMode(ledMeter1, OUTPUT);
pinMode(ledMeter2, OUTPUT);
pinMode(ledMeter3, OUTPUT);
pinMode(ledMeter4, OUTPUT);

pinMode(photoPin, INPUT);
pinMode(outputLED, OUTPUT);
pinMode(transistorPin, OUTPUT);
}

void loop()
{
analogWrite(motorPin, 255);
photoVal = analogRead(photoPin);

//  Serial.println(photoVal, DEC);
//Serial.println(gasoline);

if(state == 1)
{
sleep();
}
else if (state == 2)
{
attract();
}
else if (state == 3)
{
arouse();
}
else if (state == 4)
{
reward();
}

}

void sleep()
{

piezoVal = analogRead(piezoSensorPin);
Serial.println(piezoVal);
if(piezoVal > 30){
state=2;
attract();
}

for(value = 0 ; value <= 30; value+=1) // fade in (from min to max)
{

if(piezoVal > 30){
state=2;
break;
}
analogWrite(ledpinEYE, value);           // sets the value (range from 0 to 255)
analogWrite(ledpinEYE2, value);
delay(30);                            // waits for 30 milli seconds to see the dimming effect
}

for(value = 30; value >=0; value-=1)   // fade out (from max to min)
{
if(piezoVal > 30){
state=2;
break;
}
analogWrite(ledpinEYE, value);           // sets the value (range from 0 to 255)
analogWrite(ledpinEYE2, value);
delay(30);
}
}

void attract()
{
attractTime++;
//  fillUpCount++;

// Default flashing for attract state
analogWrite(ledpinEYE, 255);
analogWrite(ledpinEYE2, 255);
delay(300);
analogWrite(ledpinEYE, 0);
analogWrite(ledpinEYE2, 0);
delay(300);

// If you start feeding him, reset the clock and go to state 3
if(analogRead(photoPin) > 500 && gasoline < 4)
{
attractTime = 0;
state=3;
}

// If this state has been going on for 20 cycles, reset clock and go back to sleep
if (attractTime > 20)
{
state = 1;
attractTime = 0;
}
}

void arouse()
{
arrouseTime++; // Clock for this state

// If I'm really feeding him, then flash his eyes, increase fuel, and allow for lights to light up on his belly
if(analogRead(photoPin) > 500 && gasoline < 4)
{
// fillingUp = true;
analogWrite(ledpinEYE, 255);
analogWrite(ledpinEYE2, 255);
delay(300);
analogWrite(ledpinEYE, 0);
analogWrite(ledpinEYE2, 0);
delay(300);

delay(1500-analogRead(photoPin));
gasoline++;
}
if(gasoline >= 0 && arrouseTime%20==0 && gasoline!=4)
{
analogWrite(ledpinEYE, 255);
analogWrite(ledpinEYE2, 255);
delay(300);
gasoline--;
// fillingUp=false;
}

if(gasoline == 0)
{
digitalWrite(ledMeter1, LOW);
digitalWrite(ledMeter2, LOW);
digitalWrite(ledMeter3, LOW);
digitalWrite(ledMeter4, LOW);
state = 2;
}
else if(gasoline == 1)
{
digitalWrite(ledMeter1, HIGH);
digitalWrite(ledMeter2, LOW);
digitalWrite(ledMeter3, LOW);
digitalWrite(ledMeter4, LOW);
}
else if(gasoline == 2)
{
digitalWrite(ledMeter1, HIGH);
digitalWrite(ledMeter2, HIGH);
digitalWrite(ledMeter3, LOW);
digitalWrite(ledMeter4, LOW);
}
else if(gasoline == 3)
{
digitalWrite(ledMeter1, HIGH);
digitalWrite(ledMeter2, HIGH);
digitalWrite(ledMeter3, HIGH);
digitalWrite(ledMeter4, LOW);
}
else if(gasoline == 4)
{
analogWrite(ledpinEYE, 255);
analogWrite(ledpinEYE2, 255);
digitalWrite(ledMeter1, HIGH);
digitalWrite(ledMeter2, HIGH);
digitalWrite(ledMeter3, HIGH);
digitalWrite(ledMeter4, HIGH);
delay(100);
digitalWrite(ledMeter1, LOW);
digitalWrite(ledMeter2, LOW);
digitalWrite(ledMeter3, LOW);
digitalWrite(ledMeter4, LOW);
delay(50);

Serial.println(analogRead(piezoSensorPin));
if(  analogRead(piezoSensorPin) > 30){
state=4;
}
}

/*
if(fillingUp==true)
{
if(gasoline >= 1) {digitalWrite(ledMeter1, HIGH);}
if(gasoline >= 2) {digitalWrite(ledMeter2, HIGH);}
if(gasoline >= 3) {digitalWrite(ledMeter3, HIGH);}
if(gasoline >= 4) {digitalWrite(ledMeter4, HIGH);

analogWrite(ledpinEYE, 255);
analogWrite(ledpinEYE2, 255);
digitalWrite(ledMeter1, HIGH);
digitalWrite(ledMeter2, HIGH);
digitalWrite(ledMeter3, HIGH);
digitalWrite(ledMeter4, HIGH);
delay(100);
//  analogWrite(ledpinEYE, 0);
//  analogWrite(ledpinEYE2, 0);
digitalWrite(ledMeter1, LOW);
digitalWrite(ledMeter2, LOW);
digitalWrite(ledMeter3, LOW);
digitalWrite(ledMeter4, LOW);
delay(50);

if(piezoVal > 50){
state=4;
}

// state=4; //GO TO STATE 4 IF YOU HIT I OR KNOCK IT?

}
}

if(fillingUp==false)
{
if(gasoline <= 1) {digitalWrite(ledMeter1, LOW);
state=2;
}
if(gasoline <= 2) {digitalWrite(ledMeter2, LOW);}
if(gasoline <= 3) {digitalWrite(ledMeter3, LOW);}
if(gasoline <= 4) {digitalWrite(ledMeter4, LOW);

}
}

/* delay(300);
digitalWrite(ledMeter2, HIGH);
delay(300);
digitalWrite(ledMeter3, HIGH);
delay(300);
digitalWrite(ledMeter4, HIGH);
/*
delay(600);

digitalWrite(ledMeter4, LOW);
delay(300);
digitalWrite(ledMeter3, LOW);
delay(300);
digitalWrite(ledMeter2, LOW);
delay(300);
digitalWrite(ledMeter1, LOW);
delay(600);*/

}

void reward()
{
rewardCount++;

// Start spinning motor
analogWrite(transistorPin, 200);

// Flashing eyes
digitalWrite(outputLED, HIGH);
digitalWrite(ledpinEYE, 0);
digitalWrite(ledpinEYE2, 0);
delay(50);
digitalWrite(outputLED, LOW);
digitalWrite(ledpinEYE, 255);
digitalWrite(ledpinEYE2, 255);
delay(50);

if(gasoline >= 0 && rewardCount%20==0)
{
gasoline--;
}
/*if(gasoline == 1)
{
gasoline = 0;
analogWrite(transistorPin, 0);
}*/

if(gasoline == 0)
{
digitalWrite(ledMeter1, LOW);
digitalWrite(ledMeter2, LOW);
digitalWrite(ledMeter3, LOW);
digitalWrite(ledMeter4, LOW);
analogWrite(transistorPin, 0);
state = 2;
}
else if(gasoline == 1)
{
digitalWrite(ledMeter1, HIGH);
digitalWrite(ledMeter2, LOW);
digitalWrite(ledMeter3, LOW);
digitalWrite(ledMeter4, LOW);
}
else if(gasoline == 2)
{
digitalWrite(ledMeter1, HIGH);
digitalWrite(ledMeter2, HIGH);
digitalWrite(ledMeter3, LOW);
digitalWrite(ledMeter4, LOW);
}
else if(gasoline == 3)
{
digitalWrite(ledMeter1, HIGH);
digitalWrite(ledMeter2, HIGH);
digitalWrite(ledMeter3, HIGH);
digitalWrite(ledMeter4, LOW);
}
else if(gasoline == 4)
{
digitalWrite(ledMeter1, HIGH);
digitalWrite(ledMeter2, HIGH);
digitalWrite(ledMeter3, HIGH);
digitalWrite(ledMeter4, HIGH);
}

//  fillingUp = false;
// arouse();
}

Advertisements

3 Comments »

  1. Milo is the bomb.

    Comment by Jamin Hegeman — October 7, 2007 @ 8:15 pm | Reply

  2. i ::heart:: milo

    Comment by jilore — October 7, 2007 @ 11:34 pm | Reply

  3. He is mega-cute. I want one.
    In the reward state though, I can’t help imagining his eyes spinning in circles along with his propeller…

    Comment by tiagororke — October 8, 2007 @ 2:29 am | Reply


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: