Hi together,
Currently I am trying to build a bidirectional visitor counter using two HC-SR04 ultrasonic sensors. The number of people is displayed on a little 4 digits 7 segment display.
Once finished the setup is planned to be installed in a doorway to count the number of people in a room. I want to use this information for home automation (light control, pause/resume TV etc.).
Since I am not very good at programming (just learned some basics during collage) I hope you can help me with my code (see below).
I believe the problem is located within the function CountPeople().
I have written several versions of this function without getting close to a solution.
With the current version of the code the display starts with a “0” as intended. But it doesn’t count objects. Sometimes (I have a hard time reproducing the results) the value changes to “F”, “C” or “E”.
Maybe one of you can find the error.
//####################################### LIBRARIES EINBINDEN #######################################
#include <TM1637Display.h> // 4-Digits, 7Segment Display Library
//######################################### PINS DEFINIEREN #########################################
#define trigPin1 2 // PIN #1 (Sender) Sensor #1 (Flur Seite)
#define echoPin1 3 // PIN #2 (Empfänger) Sensor #1 (Flur Seite)
#define trigPin2 4 // PIN #1 (Sender) Sensor #2 (Raum Seite)
#define echoPin2 5 // PIN #2 (Empfänger) Sensor #2 (Raum Seite
const int CLK = 6; // PIN #1 Display
const int DIO = 7; // PIN #2 Display
//###################################### GLOBALE VARIABLEN ##########################################
int people = 0; // Anzahl Personen
boolean in; // Hat jemand den Raum betreten (True/False)
unsigned long time1; // Zeit zu der Sensor #1 (Flur Seite) ausgelöst wird
unsigned long time2; // Zeit zu der Sensor #2 (Raum Seite) ausgelöst wird
double doorWidth = 50; // Tür Breite
unsigned long duration1; // Zeit die Signal von Sensor #1 (Flur Seite) bis zum Objekt benötigt
double distance1; // Entfernung zwischen Sensor #1 (Flur Seite) und Objekt
unsigned long duration2; // Zeit die Signal von Sensor #2 (Raum Seite) bis zum Objekt benötigt
double distance2; // Entfernung zwischen Sensor #2 (Raum Seite) und Objekt
//################################## DISPLAY INITIALISIEREN #########################################
TM1637Display display(CLK, DIO);
//###################################################################################################
//######################################## FUNKTIONEN ###############################################
//###################################################################################################
//##################################### INITIALISIERUNG #############################################
void setup()
{
Serial.begin(9600); // Startet die serielle Kommunikation
pinMode(trigPin1, OUTPUT); // Definiere trigPin1 als Output
pinMode(echoPin1, INPUT); // Definiere echoPin1 als Input
pinMode(trigPin2, OUTPUT); // Definiere trigPin2 als Output
pinMode(echoPin2, INPUT); // Definiere echoPin2 als Input
display.setBrightness(0x0a); // Setzt Display Helligkeit auf maximal
}
//#################################### MESSUNG SENSOR 1 ############################################
void Sensor1()
{
digitalWrite(trigPin1, LOW); // Beendet Senden von Sensor #1 (Flur Seite) für 2 micro sec
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH); // Starten Senden von Sensor #1 (Flur Seite) für 10 micro sec
delayMicroseconds(10);
digitalWrite(trigPin1, LOW); // Startet Empfangen von Senor #1 (Flur Seite)
duration1 = pulseIn(echoPin1, HIGH); // Liefert die Zeit, die das Signal unterwegs war [ms]
distance1 = duration1 * 0.034 / 2; // Berechnet die Distanz zum Objekt (Näherung für 20°C)
if (distance1 < doorWidth) // Wenn Entfernung zum Objekt < Entfernung bis zum Türrahmen
{
time1 = millis(); // Setze time1 = aktueller Zeitpunkt
}
}
//#################################### MESSUNG SENSOR 2 ############################################
void Sensor2()
{
digitalWrite(trigPin2, LOW); // Beendet Senden von Sensor #2 (Raum Seite) für 2 micro sec
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH); // Starten Senden von Sensor #2 (Raum Seite) für 10 micro sec
delayMicroseconds(10);
digitalWrite(trigPin2, LOW); // Startet Empfangen von Senor #2 (Raum Seite)
duration2 = pulseIn(echoPin2, HIGH); // Liefert die Zeit, die das Signal unterwegs war [ms]
distance2 = duration2 * 0.034 / 2; // Berechnet die Distanz zum Objekt (Näherung für 20°C)
if (distance2 < doorWidth) // Wenn Entfernung zum Objekt < Entfernung bis zum Türrahmen
{
time2 = millis(); // Setze time1 = aktueller Zeitpunkt
}
}
//######################################## PERSONEN ZÄHLEN ########################################
void CountPeople()
{
if (((time1 - time2) < 0))
{
people ++;
time1 = 0;
time2 = 0;
}
else if (((time1 - time2) > 0))
{
people --;
time1 = 0;
time2 = 0;
}
}
//###################################### AUSGABE AUF DISPLAY ######################################
void DisplayPeople()
{
display.showNumberDec(people);
}
//############################## REGELMÄßIGER AUFRUF ALLER FUNKTIONEN #############################
void loop()
{
Sensor1(); // messen mit Sensor #1
Sensor2(); // messen mit Sensor #2
CountPeople(); // Personenanzahl berechnen
DisplayPeople(); // Ausgabe auf Display
}
Thanks in advance,
Chris