Edit: I have tried this state machine in Arduino : It accepts those data and according to it, it should bi color LED light. But not working properly. Not lighting up anything
//int currState;
//int reChk, data, confirm;
const int pingPin = 13;
int inPin = 12;
void setup()
{
Serial.begin(9600);
// initialize digital pin 13 as an output.
pinMode(32, OUTPUT);
pinMode(34, OUTPUT);
digitalWrite(32, LOW);
digitalWrite(34, LOW);
//currState = 0;
//reChk = 0;
//confirm = -1;
}
void loop() // run over and over
{
static int currState = 0, reChk = 0, confirm = -1;
int data;
if(reChk == 0)
{
//ultrasonic();
if(Serial.available())
data = Serial.read();
else
currState = 5;
}
switch(currState)
{
case 0:
reChk = 0;
if(data == 78)
currState = 1;
else
currState = 0;
break;
case 1:
reChk = 0;
if(data == 32)
currState = 2;
else
{
currState = 0;
reChk = 1;
}
break;
case 2:
reChk = 0;
switch(confirm)
{
case -1 :
confirm = data;
currState = 3;
break;
case 0 :
digitalWrite(32, LOW);
digitalWrite(34, LOW);
delay(10);
confirm = -1;
currState = 0;
break;
case 1 :
digitalWrite(34, HIGH);
digitalWrite(32, LOW);
delay(10);
confirm = -1;
currState = 0;
break;
case 2 :
digitalWrite(32, HIGH);
digitalWrite(34, LOW);
delay(10);
confirm = -1;
currState = 0;
break;
default :
confirm = -1;
reChk = 1;
currState = 0;
}
case 3:
reChk = 0;
if(data == 20)
{
reChk = 1;
currState = 2;
}
else
{
confirm = -1;
currState = 0;
}
break;
default:
reChk = 0;
currState = 0;
confirm = -1;
}
delay(10);
}
void ultrasonic () //ultrasonic
{
const int pingPin = 13;
int inPin = 12;
//raw duration in milliseconds, cm is the
//converted amount into a distance
long duration, cm;
//initializing the pin states
pinMode(pingPin, OUTPUT);
//pinMode(greenLed, OUTPUT);
//pinMode(redLed, OUTPUT);
//sending the signal, starting with LOW for a clean signal
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
//setting up the input pin, and receiving the duration in
//microseconds for the sound to bounce off the object infront
pinMode(inPin, INPUT);
duration = pulseIn(inPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
if(cm > 50)
{
//printing the current readings to ther serial display
Serial.print("5");
}
else
{
Serial.print("0");
}
//Serial.print("cm");
//Serial.println();
//checking if anything is within the safezone, if not, keep
//green LED on if safezone violated, activate red LED instead
//if (cm > safeZone)
//{
// digitalWrite(greenLed, HIGH);
// digitalWrite(redLed, LOW);
//}
//else
//{
// digitalWrite(redLed, HIGH);
// digitalWrite(greenLed, LOW);
//}
delay(1000);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}