Nico DietzAlexPhilippTorben StrobelGunarDionysios Satikidis
Published © GPL3+

SafeGate

Easy way to count customers.

IntermediateFull instructions provided1 hour492
SafeGate

Things used in this project

Hardware components

Photon
Particle Photon
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×2
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×10
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Losant Platform
Losant Platform

Story

Read more

Schematics

Circuit Diagram

Code

safegate

C/C++
int counter = 0; //counter of people entering/exiting
int currentState = 0;
int previousState = 0;
int currentState2 = 0;
int previousState2 = 0; 
#define trigPin D4
#define echoPin D5
#define trigPin2 D2
#define echoPin2 D1

SYSTEM_THREAD(ENABLED); //photon has permanent connection to the cloud


void setup() {
    pinMode(D7, OUTPUT);
    //1 sensor
    pinMode(D5, INPUT);
    pinMode(D4, OUTPUT);
    //2 sensor
    pinMode(D1, INPUT);
    pinMode(D2, OUTPUT);
    
    digitalWrite(D7, 0);
    Particle.variable("counter", counter); //variable for the counter 

}

void loop() {
    delay(100);
    digitalWrite(D7, 0);
    //get the distance from the first sensor
    long duration, distance;
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10); 
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration/2) / 29.1;
    //get the distance from the second sensor
    long duration2, distance2;
    digitalWrite(trigPin2, HIGH);
    delayMicroseconds(10); 
    digitalWrite(trigPin2, LOW);
    duration2 = pulseIn(echoPin2, HIGH);
    distance2 = (duration2/2) / 29.1;
    //object entering in the system if the distance is lower than 50 cm
    if (distance <= 50){ 
        currentState = 1;
        digitalWrite(D7, 1);
    }
    else {
        currentState = 0;
    }
    if(currentState != previousState){
        while(currentState == 1) {
            long duration2, distance2;
            digitalWrite(trigPin2, LOW); 
            delayMicroseconds(2); 
            digitalWrite(trigPin2, HIGH);
            delayMicroseconds(10); 
            digitalWrite(trigPin2, LOW);
            duration2 = pulseIn(echoPin2, HIGH);
            distance2 = (duration2/2) / 29.1;
            if (distance2 <= 50){
                currentState2 = 1;
            }
            else {
                currentState2 = 0;
            }
            if(currentState2 != previousState2){
                if(currentState2 == 1) {
                    counter = counter + 1;
                    Particle.publish("counter", (String)counter);
                    digitalWrite(D7, 0);
                    delay(400); // pause for 400 miliseconds
                }
                else {
                    //Particle.publish("counter", (String)counter);
                }
                return;
            }
        }
    }
    // object exit from the system if the distance is lower than 50 cm
    if (distance2 <= 50){
        currentState2 = 1;
        digitalWrite(D7, 1);
    }
    else {
        currentState2 = 0;
    }
    if(currentState2 != previousState2){
        while (currentState2 == 1) {
            long duration, distance;
            digitalWrite(trigPin, HIGH);
            delayMicroseconds(10); 
            digitalWrite(trigPin, LOW);
            duration = pulseIn(echoPin, HIGH);
            distance = (duration/2) / 29.1;
            if (distance <= 50){
                currentState = 1;
            }
            else {
                currentState = 0;
            }
            if(currentState != previousState){
                if(currentState == 1) {
                    counter = counter - 1; 
                    Particle.publish("counter", (String)counter);
                    digitalWrite(D7, 0);
                    delay(400); // pause for 400 miliseconds
                }
                else {
                   //Particle.publish("counter", (String)counter);
                }
                return;
            }
        }
    }
}

Credits

Nico Dietz
1 project • 4 followers
Contact
Alex
1 project • 4 followers
Contact
Philipp
1 project • 4 followers
Contact
Torben Strobel
1 project • 4 followers
Contact
Gunar
1 project • 4 followers
Contact
Dionysios Satikidis
17 projects • 32 followers
Dev. Experience Platforms Guy at Mercedes-Benz Lecturer IoT Ecosystems & Applied AI at Brunel Univ. and Univ. of Applied Sciences Esslingen.
Contact

Comments

Please log in or sign up to comment.