1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define trigPin 13
#define echoPin 12
// Find LCD address for I2C in this tutorial it is 0x3f
LiquidCrystal_I2C lcd(0x3f, 16, 2);
int counter = 0;
int currentState1 = 0;
int previousState1 = 0;
int currentState2 = 0;
int previousState2 = 0;
int inside = 0;
int outside = 0;
void setup()
{
// initialize the LCD
lcd.begin();
//Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
lcd.setCursor(0, 0);
lcd.print("IN: ");
lcd.setCursor(8, 0);
lcd.print("OUT: ");
lcd.setCursor(0, 1);
lcd.print("Total Inside: ");
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance <= 9) {
currentState1 = 1;
}
else {
currentState1 = 0;
}
delay(100);
if (currentState1 != previousState1) {
if (currentState1 == 1) {
counter = counter + 1;
}
lcd.setCursor(14, 1);
lcd.print(counter);
inside = inside + 1;
}
lcd.setCursor(4, 0);
lcd.print(inside);
if (distance > 9 && distance <= 18) {
currentState2 = 1;
}
else {
currentState2 = 0;
}
delay(100);
if (currentState2 != previousState2) {
if (currentState2 == 1) {
counter = counter - 1;
}
lcd.setCursor(14, 1);
lcd.print(counter);
outside = outside + 1;
}
lcd.setCursor(13, 0);
lcd.print(outside);
lcd.setCursor(14, 1);
lcd.print(counter);
if (counter > 9 || counter < 0) {
lcd.setCursor(14, 1);
lcd.print(counter);
delay(100);
lcd.clear();
}
} |
Partager