Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
sergioluz
Published © GPL3+

Motion Direction Detector

FSM that allows to identify the direction of the object.

BeginnerShowcase (no instructions)9,279
Motion Direction Detector

Things used in this project

Hardware components

LED (generic)
LED (generic)
×3
Arduino Nano R3
Arduino Nano R3
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×2
Breadboard (generic)
Breadboard (generic)
×1
Resistor 1k ohm
Resistor 1k ohm
×1

Story

Read more

Schematics

Schematic

Code

Object Direction Detector

C/C++
FSM that detect the direction of the object with the ability to generate alarms or orders. In this particular case of application by way of example, we have chosen a detector of direction of cars in a Highway, and we increased the possibility of counting the cars in the right direction and generating alarms for the cars in the wrong direction
#define trigPin1 2
#define echoPin1 3
#define trigPin2 4
#define echoPin2 5


int Led3=10;
int Led1=11;
int Led2=12;
int sensor1=0;
int sensor2=0;
float duration1,distance1, distance2, duration2;
volatile int FSM1_estado=0;
int contagem=0;
unsigned long tempo =0;
unsigned long r,s,x;



void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(Led1, OUTPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(Led2, OUTPUT);
pinMode(Led3, OUTPUT);
}


void loop() {

  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin1, LOW);

  duration1=pulseIn (echoPin1, HIGH); //tini=micros();
  distance1= (duration1/2)*0.0345;

   digitalWrite(Led1, LOW);
   digitalWrite(Led2, LOW);
   digitalWrite(Led3, LOW);
   if(distance1 >=5 && distance1 <=15){
    sensor1=1;
  
     if (sensor1==1){
        
      FSM1();
      }
   }
   else{sensor1=0;}
  
}




void FSM1(){
int contador;
int s=1000;


switch(FSM1_estado){

 case 0:
   digitalWrite(Led1, LOW);
   digitalWrite(Led2, LOW);
   digitalWrite(Led3, LOW);
  
   FSM1_estado=1;
   break;
 
  
  
  
  case 1:
   r=millis();
   digitalWrite(Led1, HIGH);
   digitalWrite(Led2, LOW);
   digitalWrite(Led3, LOW);
   FSM1_estado=2;
  break;


 case 2:
      
       delay(60);
       digitalWrite(trigPin2, HIGH);
       delayMicroseconds(500);
       digitalWrite(trigPin2, LOW);
       duration2=pulseIn (echoPin2, HIGH);
       distance2= (duration2/2)*0.0344;
        x=millis ()-r;
        
         if ((distance2>=5 && distance2 <15)&&(x<s)){      
                   
            FSM1_estado=3; 
       }       
            else{FSM1_estado=4;}     
   break;


 case 3:
   digitalWrite(Led1, HIGH);
   digitalWrite(Led2, HIGH);
   digitalWrite(Led3, LOW);    
  Serial.println();
  Serial.print("-------------------------------------------");
  Serial.println();
  Serial.print("-----------SEQUENCIA CORRETA-------------- ");
  Serial.println();
  Serial.print("-------------------------------------------");
  Serial.println();
  contagem++;
  Serial.println(contagem);
   FSM1_estado=0;
  break;


 case 4:
  digitalWrite(Led1, LOW);
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, HIGH);    
  Serial.println();
  Serial.print("-------------------------------------------");
  Serial.println();
  Serial.print("-----------INCORRETA ALRME -------------- ");
  Serial.println();
  Serial.print("-------------------------------------------");
  Serial.println();
  delay (200);
   FSM1_estado=0;
  break;
 


}

}




  

Credits

sergioluz

sergioluz

0 projects • 0 followers

Comments