Making Things Interactive

March 27, 2008

State Machine with LEDs and buzzers

Filed under: 8: State Machine,Assignments,Lingshui Wang — lingshui @ 12:31 am

So I made a state machine consisting of 4 LEDs and a buzzer. There are 4 active states, sOff (self-explanatory), sAttract(flash lights quickly to attract passerby to it), sFirst(the first script it will run – which in this case consists of pulsing 4 lights), and sSecond(the second script – composed of pulsing 4 lights and buzzing a buzzer, this state is only activated once state one has been triggered 4 times previously).

Board Layout Picture 1
Board Layout Picture 2
Schematic

Here’s the code:

note: the compiler keeps telling me that “attractScript” was not declared, even though I clearly have it defined underneath void loop as “void attractScript”, what am I doing wrong?


const int sOff = 0;                                                    //set the case states...
const int sAttract = 1;
const int sFirst = 2;
const int sSecond = 3;

int currentState = sOff;                                               //set currentState and nextState
int nextState = sAttract;
int powerPin = 4;                                                      //on and off switch
int rangeFinder = 0;                                                   //sensor to change states
int ledA = 11;                                                         //LEDs
int ledB = 10;
int ledC = 9;
int ledD = 6;
int buzzer = 12;                                                       //buzzer
int firstCount = 0;                                                    //setup counting variables
int secondCount = 0;

void setup()
{
  pinMode(powerPin, INPUT);                                            //setting rangefinder, power pin, LEDs and buzzer
  pinMode(rangeFinder, INPUT);
  pinMode(ledA, OUTPUT);
  pinMode(ledB, OUTPUT);
  pinMode(ledC, OUTPUT);
  pinMode(ledD, OUTPUT);
  pinMode(buzzer, OUTPUT);
  Serial.begin(9600);                                                  //begin serial library at 9600 baud
}

void loop()
{
  switch(currentState)                                                 //start state machine
    {
      case sOff:                                                       //state "off"
      Serial.println("off");
      firstCount = 0;                                                  //reset counters for both state 1 and state 2
      secondCount = 0;
      if (digitalRead(powerPin) == HIGH)                               //when power pin is on....
      {
        currentState = nextState;                                      //proceed away from sOff and onto sAttract
      }
      break;
      case sAttract:                                                   //state "attract"
      Serial.println("attract");
      if (digitalRead(powerPin) == LOW)                                //if power pin is off...
      {
        currentState = sOff;                                           //turn off state machine
        nextState = sAttract;
      }
      else if (analogRead(rangeFinder) <= 50)                          //however if someone activates the machine...
      {
        currentState = sFirst;                                         //proceed to first state
      }
      else
      {
        attractScript();                                               //otherwise continue attracting passerby
      }
      break;
      case sFirst:                                                     //state "primary"
      Serial.println("first");
      if (digitalRead(powerPin) == LOW)                                //check power is on
      {
        currentState = sOff;
        nextState = sAttract;
      }
      else if ((firstCount == 4) && (analogRead(rangeFinder) <= 50))   //if state "primary" activated 4 times previously
      {
        currentState = sSecond;                                        //proceed onto state "secondary"
      }
      else if ((firstCount < 4) && (analogRead(rangeFinder) <= 50))    //if state "primary" activated less than 4 times
      {
        PulseLed(ledA);                                                //proceed with state "primary" scripts
        PulseLed(ledB);
        PulseLed(ledC);
        PulseLed(ledD);
        firstCount ++;						                           //count times state "primary" has been triggered
        secondCount = 0;					                           //reset state "secondary" count
      }
      else							                                   //if passerby leaves before state "secondary"
      {
        currentState = sAttract;                                       //return to state sAttract
      }
      break;
      case sSecond:                                                    //state "secondary"
      Serial.println("second");                                        //check if power is on
      if (digitalRead(powerPin) == LOW)
      {
        currentState = sOff;
        nextState = sAttract;
      }
      else if ((secondCount == 1) && (analogRead(rangeFinder) <= 50))  //if state "secondary" has been activated
      {
        currentState = sFirst;                                         //proceed back to state "primary"
      }
      else if ((secondCount < 1) && (analogRead(rangeFinder) <= 50))   //if state "secondary" hasnt' been activated
      {
        PulseLed(ledA);                                                //proceed onto secondary script
        PulseLed(ledB);
        PulseLed(ledC);
        PulseLed(ledD);
        buzzerScript();
        secondCount ++;						                           //start "secondary" count
        firstCount = 0;						                           //reset "primary" count
      }
      else
      {
        currentState = sAttract;                                       //if passerby leaves, go back to sAttract
      }
      break;
      default:                                                         //if default state is activated, then error
      Serial.println("ERROR: default state");
      currentState = sOff;
      nextState = sAttract;
      break;
   }
)
void PulseLed(int pin)                                                 //set pattern for pulsing LEDs
{
  for (int i=0; i<=255; i++)
  {
    analogWrite(pin, i);
    delay(4);
  }
  for (int i=255; i>=0; i--)
  {
    analogWrite(pin, i);
    delay(4);
  }
}

void attractScript()                                                   //set light pattern for state "attract"
{
  digitalWrite(ledA, HIGH);
  delay(500);
  digitalWrite(ledA, LOW);
  digitalWrite(ledB, HIGH);
  delay(500);
  digitalWrite(ledB, LOW);
  digitalWrite(ledC, HIGH);
  delay(500);
  digitalWrite(ledC, LOW);
  digitalWrite(ledD, HIGH);
  delay(500);
  digitalWrite(ledD, LOW);
}

void buzzerScript()                                                    //set script for how buzzer works
{
  digitalWrite(buzzer, HIGH);
  delay(2000);
  digitalWrite(buzzer, LOW);
  delay(500);
}
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

Create a free website or blog at WordPress.com.

%d bloggers like this: