#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int inputPin = 9; // Пин для подключения PIR-сенсора
const int inputPin1 = 10; // Второй пин для подключения PIR-сенсора
const int length = 10; // Длина поезда в метрах
int pirState = LOW; // Состояние PIR-сенсора (движение отсутствует)
int pirState1 = LOW; // Состояние второго PIR-сенсора (движение отсутствует)
int val = 0;
int val1 = 0;
long SEC = 0; // Общее количество секунд
long detect_time = 0; // Время обнаружения движения
long detect_time1 = 0; // Время обнаружения движения вторым сенсором
unsigned long timer;
long pass_time = 0; // Время прохода между датчиками
float speed = 0; // Рассчитанная скорость движения
void setup() {
lcd.begin(16, 2); // Инициализация дисплея
pinMode(inputPin, INPUT);
pinMode(inputPin1, INPUT);
Serial.begin(9600);
timer = millis();
}
void loop() {
if (millis() - timer > 1) {
timer = millis();
SEC++;
}
val = digitalRead(inputPin);
if (val == HIGH) {
if (pirState == LOW) {
detect_time = SEC;
Serial.println("Обнаружено движение! " + String(detect_time));
pirState = HIGH;
}
} else {
if (pirState == HIGH) {
Serial.println("Движение прекращено!");
pirState = LOW;
}
}
val1 = digitalRead(inputPin1);
if (val1 == HIGH) {
if (pirState1 == LOW) {
if (pass_time == 0) {
pass_time = SEC;
}
detect_time1 = SEC;
Serial.println("Обнаружено движение! " + String(detect_time1));
pirState1 = HIGH;
if (detect_time != 0 && detect_time1 != 0) {
float time = detect_time1 - detect_time;
time /= 1000;
speed = length / time;
Serial.println(String(time) + " " + String(speed));
Serial.println();
displayTrainInfo(time, speed);
}
}
} else {
if (pirState1 == HIGH) {
Serial.println("Движение завершено!");
pirState1 = LOW;
pass_time = SEC - pass_time;
Serial.println("Время прохода " + String(pass_time));
Serial.println("Длина поезда " + String(pass_time * speed / 1000));
displayTrainLength(pass_time * speed / 1000);
}
}
}
void displayTrainInfo(float time, float speed) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time: " + String(time) + " s");
lcd.setCursor(0, 1);
lcd.print("Speed: " + String(speed) + " m/s");
}
void displayTrainLength(float trainLength) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Train length:");
lcd.setCursor(0, 1);
lcd.print(String(trainLength) + " m");
}