#include <TM1637.h>
int sensorPin = 13;
int peopleCount = 0;
const int CLK = 2;
const int DIO = 3;
#define PIN_TRIG 5
#define PIN_ECHO 7
TM1637 tm(CLK, DIO);
void setup() {
pinMode(sensorPin, INPUT);
Serial.begin(9600);
tm.init();
tm.set(BRIGHT_TYPICAL);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
}
int prev_state = 0;
void readPIR() {
int sensorValue = digitalRead(sensorPin);
if (sensorValue == HIGH && prev_state == LOW) {
Serial.println(peopleCount);
peopleCount++;
}
prev_state = sensorValue;
}
void showNumber(int data) {
tm.display(0, (data / 1000) % 10);
tm.display(1, (data / 100) % 10);
tm.display(2, (data / 10) % 10);
tm.display(3, data % 10);
}
float readDistanceCM() {
digitalWrite(PIN_TRIG, LOW);
delayMicroseconds(2);
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_ECHO, LOW);
int duration = pulseIn(PIN_ECHO, HIGH);
return duration * 0.034 / 2;
}
float preValue;
void ReadUltrasonic() {
float dist = readDistanceCM();
if(dist < 50 && preValue > 50) {
Serial.println(peopleCount);
peopleCount++;
}
preValue = dist;
}
// void ultraSonic() {
// // Start a new measurement:
// digitalWrite(PIN_TRIG, HIGH);
// delayMicroseconds(10);
// digitalWrite(PIN_TRIG, LOW);
// // Read the result:
// int duration = pulseIn(PIN_ECHO, HIGH);
// showNumber(duration);
// }
void loop() {
readPIR();
ReadUltrasonic();
delay(100);
}