Arduino state machine

When you have an application that needs to move (transition) from one state to another state then it is most likely you will need to implement a state machine. A state machine makes each state responsible for its own actions and also how it transitions to the next state. A state machine also removes the complication of using a lot of if statements.

The code below was written to detect two infra red sensors and use an implementation of a state machine.

package
{
int state = 0;
boolean stateChanged = true;
int prevState = 0;
int ledPins[] = {4,2,8,7};
int sensorPin0 = 0;   //analog input pin
int sensorPin1 = 1;
int sensorValue0 = 0;  //value read from the sensor 1
int sensorValue1 = 1;  //value read from the sensor 2

void setup() {
pinMode (4, OUTPUT);
pinMode (2, OUTPUT);
pinMode (8, OUTPUT);
pinMode (7, OUTPUT);
//initialize serial communications at 9600 bps:
Serial.begin(9600);

}

void loop(){

checkUser();

if (state == 0)
{
    allOff();
} else if (state == 1){
    personSensed();
} else if (state == 2){
    personAligned();
} else if (state == 3){
    hand2LowBrightness();
} else if (state == 4){
    hand2HighBrightness();
}

//Serial.println(analogRead(0));

}

void hand2HighBrightness()
{
if(stateChanged)
{
    stateChanged = false;
    Serial.println("High brightness");
    allOff();
    digitalWrite (7, LOW);

/*sensorValue1 = analogRead (sensorPin1);
if(sensorValue1 > 186){
    state = 1;
stateChanged = true;
} else {
    state = 0;
}*/
}

}

void hand2LowBrightness()
{
    if(stateChanged)
    {
        Serial.println("Low brightness");
        stateChanged = false;
        //allOff();
        digitalWrite (2, LOW);
    }

}

void personAligned()
{
    if(stateChanged)
    {
        Serial.println("Person aligned");
        stateChanged = false;
        allOff();
        digitalWrite (8, LOW);
        delay(5000);
        state = 3;
        tateChanged = true;
    }
}

void personSensed()
{
    if(stateChanged)
    {
        Serial.println("Person sensed");
        stateChanged = false;
        allOff();
        digitalWrite (4, LOW);
    }
}

void checkUser()
{
    if (state == 0){
        state0Checks();
    } else if(state == 1){
        state1Checks();
    } else if(state == 3){
        state3Checks();
    } else if(state == 4){
        state4Checks();
    }
}

void state0Checks()
{
    sensorValue0 = analogRead (sensorPin0);
    if (sensorValue0 > 70)
    {
        if (state != 1 )
        {
            state = 1;
            stateChanged = true;
        }
    }
}

void state1Checks()
{
    sensorValue0 = analogRead (sensorPin0);
    if((sensorValue0 > 186) && (sensorValue0 < 265))
    {
        if (state != 2 )
        {
            state = 2;
            stateChanged = true;
        }
    }
}

void state3Checks()
{
    sensorValue1 = analogRead (sensorPin1);
    /*if((sensorValue1 > 186) && (sensorValue1 < 265)){
    if (state != 4 )
    {
        state = 4;
        stateChanged = true;
    }
    }*/
    Serial.println("state 3 checks");
    if (Serial.available() > 0)
    {
        byte incomingByte = Serial.read();
        Serial.print("Received a ");
        Serial.println(incomingByte);
        if (incomingByte == '1') 
        {
            state = 4;
        stateChanged = true;
        }
    }
}

void state4Checks()
{
    delay(5000);
    allOff();
    state = 0;
    stateChanged = true;
    /* sensorValue0 = analogRead (sensorPin0);
    Serial.println(sensorValue0);
    if(sensorValue1 > 186)
    {
        state = 4;
        stateChanged = true;
    }*/

    /* if ((sensorValue0 > 70) && (sensorValue0 < 186))
        {
            state = 1;
            stateChanged = true;
        } else {
            state = 0;
            stateChanged = true;
            Serial.println("Nobody..........................................................");
        }*/

}

void allOff()
{
    digitalWrite (4, HIGH);
    digitalWrite (8, HIGH);
    digitalWrite (2, HIGH);
    digitalWrite (7, HIGH);
}

}