Making Furniture Interactive

October 4, 2007

spika.4 moods

Filed under: Beste Nazilli,Exercise 4: Four State Machines — botto @ 11:15 pm

So as you can see from the video that Spika my dog 🙂 have 4 states.

There is a photocell to sense the shadow as you come closer, all the modes are using the same sensor , as output I used a servo motor and LEDs. I want to remind that I burned a servo motor during the preparation. But I think it was b/c of the code.As it is fixed, I guess it works fine. It is not still in the way that I want to ( as physical apperacne) but it works well.

1st mode :Asleep //does nothing( waits for intearction let’s say:))
2nd mode:Awake –>just came a liitle bit close// makes small shakes by his head and LEDs on the body starts to blink
3rd mode:Friendly –> as enough as a friend closeness// start to shake his head remarkably ( 90 degrees)
4rd mode:Angry –> you are too closeee // starts to shake his head more randomly ( aggresively) compare to the friendly mode and starts to make a buzz alarm noise.

So in every mode , it is possible for Spika to go to any of the modes, as long as the photosensor have the values that set up for the values.

here is the code:

int timer = 255;                   // The higher the number, the slower the timing.
int pins[] = {
13, 12, 8, 7, 4, 2 };       // an array of pin numbers
int num_pins = 6;                  // the number of pins (i.e. the length of the array)
int value = 0;
int angrycounter = 0;
int happycounter = 0;
int sleepcounter = 0;
int lightReading;

int servoPin = 9;            // R/C  Servo connected to digital pin
int switchPin = 10;            //  digital input pin for a switch
int buzzerPin = 11;
int myAngle;                 // angle of the servo (roughly in degrees) 0-180
int pulseWidth;              // function variable
boolean switchState = false;    // the state of the switch

int closed = 0;
int opening = 1;
int open = 2;
int closing = 3;

int mouthState = closed;

int asleep = 0;
int awake = 1;
int friendly = 2;
int angry = 3;

int spikaState = asleep;

//ldr stuff
int LDRpin = 0;
int startLDRreading;
//piezo stuff
int speakerOut = 11;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
//int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 300};
byte melody[] = "4C4C4C4C4C8p8p4C4C4C4C4C";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//                                10                  20                  30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 12;
int statePin = LOW;

void servoPulse(int servoPin, int myAngle) {
pulseWidth = (myAngle * 11) + 500;  // converts angle to microseconds
digitalWrite(servoPin, HIGH);       // set servo high
delayMicroseconds(pulseWidth);      // wait a very small amount
digitalWrite(servoPin, LOW);        // set servo low
delay(20);                          // refresh cycle of typical servos (20 ms)
}

void buzzer() {
analogWrite(speakerOut, 0);
for (count = 0; count < MAX_COUNT; count++) {
statePin = !statePin;
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
analogWrite(speakerOut,200);
delayMicroseconds(tones[count2]);
analogWrite(speakerOut, 0);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
analogWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
}
}

void LEDs()
{
int i;

for (i = 0; i < num_pins; i++) {

for(value = 0 ; value = num_pins; i++) {
for(value = 255; value >=0; value-=5)   // fade out (from max to min)
{
analogWrite(pins[i], value);
delay(10);
}
}
}

void setup()
{
int i;

for (i = 0; i < num_pins; i++)   // the array elements are numbered from 0 to num_pins - 1
pinMode(pins[i], OUTPUT);      // set each pin as an output
pinMode(servoPin, OUTPUT);          // set servoPin pin as output
pinMode(switchPin, INPUT);         // set the switch pin to be an input
Serial.begin(9600);

//take the average of 10 readings and store as our LDR start val used for calibration
for(int i =0 ; i 50){
spikaState = awake;
angrycounter=0;
sleepcounter=0;
happycounter=0;
}
}

//awake

if (spikaState == awake){
Serial.println("spika awake");
servoPulse(servoPin, random(10,30));
LEDs();
lightReading = analogRead(LDRpin) - startLDRreading ;
if(lightReading>150){
angrycounter++;
//sleepcounter--;
//happycounter--;
} else if(lightReading>60){
happycounter++;
// angrycounter--;
//sleepcounter--;
} else if(lightReading>50){
spikaState = friendly;
} else if(lightReading60 && lightReading 200){
angrycounter++;
}
*/

if (happycounter >5){
spikaState = friendly;
angrycounter=0;
sleepcounter=0;
happycounter=0;
}

if (angrycounter >5){
spikaState = angry;
angrycounter=0;
sleepcounter=0;
happycounter=0;
}

if (sleepcounter >5){
spikaState = asleep;
angrycounter=0;
sleepcounter=0;
happycounter=0;
}
}

// friendly

if (spikaState == friendly){
Serial.println("spika friendly");

for (myAngle=0; myAngle150){
angrycounter++;
//sleepcounter--;
//happycounter--;
} else if(lightReading>60){
happycounter++;
//angrycounter--;
//sleepcounter--;
} else if(lightReading200){
angrycounter++;
} else if(lightReading>60){
happycounter++;
}

if (lightReading>100){
angrycounter++;
}
*/

if (angrycounter >10){
spikaState = angry;
angrycounter=0;
sleepcounter=0;
happycounter=0;
}
if (sleepcounter >10){
spikaState = awake;
angrycounter=0;
sleepcounter=0;
happycounter=0;
}

}

//angry

if (spikaState == angry){
lightReading = analogRead(LDRpin) - startLDRreading ;
Serial.println("spika angry");
if(lightReading<150){
sleepcounter++;
//happycounter--;
//angrycounter--;
}
for (myAngle=0; myAngle5){
spikaState = friendly;
angrycounter=0;
sleepcounter=0;
happycounter=0;
}
}
}
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: