MECH 3032
Arduino – LED Finite State Machine
Part 1: LED Finite State Machine, Random States
Arduino Code
//state variable type
typedef int state;
//Defining LED pins
int LED1 = 9;
int LED2 = 10;
int LED3 = 11;
//State values and state variables
const state S0 = 0;
const state S1 = 1;
const state S2 = 2;
const state S3 = 3;
state current_state;
void setup() {
// put your setup code here, to run once:
// set up LED pins as output
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
//wait for 1 sec
delay(1000);
//pick random value between 0 and 3
current_state = random(0, 4);
//print on the serial monitor random varibale
Serial.println(current_state);
//check the state change variable and change its state due to input variable
switch(current_state)
{
//state S0 and switches OFF all LEDs
case S0:
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
break;
//state S1 and switches OFF 2 LEDs and switch ON 1 LED
case S1:
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
break;
//state S2 and switch ON 2 LEDs and switch OFF 1 LED
case S2:
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
break;
//state S3 and switches ON all LEDs
case S3:
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
break;
}
}
Part 2: LED Finite State Machine, User Controlled
Arduino Code;
//specialized state variable type
typedef int state;
//defining LED pins
int LED1 = 9;
int LED2 = 10;
int LED3 = 11;
//state variable registers and their values
const state S0 = 0;
const state S1 = 1;
const state S2 = 2;
const state S3 = 3;
//starting state is S1 and previous state whill determine whether there is a state change or not
state current_state = S1;
state prev_state = S1;
void setup() {
//Setting up Baud rate
Serial.begin(9600);
// put your setup code here, to run once:
//Setting up LED pins as output
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
}
void loop() {
//check if data is being sent through Serial
if (Serial.available() > 0)
{
//store its value in this variable
current_state = Serial.read();
//check if the value is different from previous value then print on Serial monitor
if (current_state != prev_state){
Serial.println("State is change. New state is:");
Serial.println(current_state);
}
//update previous stae vaalue
prev_state = current_state;
//if it is 0 character state is S0
//and all LEDs are OFF
if ((current_state-'0') == S0)
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
}
//if it is character 1 state is S1
//and only 2 LEDs are OFF and 1 is ON
else if ((current_state - '0') == S1)
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
}
//if it is character 2 then state is S2
//and 2 LEDs are ON and 1 is OFF
else if ((current_state - '0') == S2)
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
}
//if it is character 3 state is S3
//and all LEDs are ON
else if ((current_state - '0') == S3)
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
}
//if the input is any other character the input is invalid and prints Error message
else
{
//otherwise prints error
Serial.print("Error! Enter Value between 0 and 3");
}
}