Making Things Interactive

March 27, 2008

State Machine Assignment (Assignment 9?)

Filed under: 8: State Machine,Assignments,Cat Adams — catadams @ 12:36 am

For my state machine, I wired and coded the attract and reward states for my recycler (everything except the actual recycling process). I used two button presses for the sensors, which I realize may be a bit simplistic. I have ordered a motion sensor but it did not arrive in time to do this assignment.

I made the mistake of assuming that once I wrote the code and fixed the stupid things like adding a space to an “int” within the code when it was declared without a space, everything would work fine. Also that I would get the wiring right the first time. It might be helpful if we could quickly go over in class why it would be good to declare an int globally. I thought it would be alright to declare it within a certain “for” statement, but eventually discovered I needed to declare it in the beginning of my code with the LED’s and the sensors. Anyway, it works now. In addition to the youtube video, here is an image of my state machine in idle state and excited state.

//states
static int Idle = 0;
static int Excited = 1;
static int Angry = 2;
static int Recycling = 3;
static int BigReward = 4;
static int SmallReward = 5;

//regular ints
int redLED = 3;
int blueLED = 5;
int greenLED = 6;
int angryWait = 0;
int switchPin1 = 9;
int switchPin2 = 12;
int currentState = Idle;
int numberCycle = 0;

//program for pulsing just one LED at a time (to save space later on)
void PulseOne (int pin) {
for (int i=0; i=0; i–) {
analogWrite(pin, i);
delay(1);
}
}

void setup(){
pinMode(redLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
Serial.begin(9600);
}

void loop(){

//Idle State: all LED’s off, print “Idle” on computer screen
if (currentState == Idle) {
Serial.println(“Idle”);
//turn all the LEDs off (in case a later state does not turn an led off)
digitalWrite(blueLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
if (digitalRead(switchPin1) == HIGH){ //if the switch is pressed, i.e. if a person is detected (I do not have the proper sensor yet…)
currentState = Excited;
}
else{ //if no one is around, do nothing
currentState = Idle;
}
delay(10);
}

//Excited State: pulse all three LED’s, print “Excited”

if (currentState == Excited){
Serial.println(“Excited”);
for(int value = 0 ; value =0; value-=5)
{
analogWrite(blueLED, value);
analogWrite(greenLED, value);
analogWrite(redLED, value);
delay(30);
}
delay(2000);
if (digitalRead(switchPin2) == HIGH){ //if a recyclable is detected, start recycling it
currentState = Recycling;
}
else{ //if nothing happens, the machine gets angry
currentState = Angry;
}
}

//Angry State: pulse red LED and print “Angry!!”

if (currentState == Angry){
Serial.println(“Angry!!”);
PulseOne(redLED);
}
delay (2000);
if (digitalRead (switchPin2) == HIGH){ //if a recyclable is detected
currentState = Recycling;
}
else{ //if nothing happens, assume the person has walked away with a guilt conscience
currentState = Idle;
}

//Recycling: this is the state during which the entire recycling process happens.
//this will have a lot more code for the final
if(currentState == Recycling){
Serial.println(“Recycling”);
numberCycle++;
if (numberCycle <=5) { //self-explanatory currentState = SmallReward; } else{ currentState = BigReward; } } //Small Reward: print "Small Reward" and pulse the blue LED then the green LED if (currentState == SmallReward){ Serial.println("Small Reward"); PulseOne(blueLED); PulseOne(greenLED); currentState = Idle; } //Big Reward: print "Big Reward" and pulse the blue then green then red LED if (currentState == BigReward){ Serial.println("Big Reward"); PulseOne(blueLED); PulseOne(greenLED); PulseOne(redLED); currentState = Idle; } } [/sourcecode]

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: