#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Ініціалізація LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Пін світлодіода (GPIO 32)
const int LED_PIN = 25;
// Налаштування симуляції
const int targetSteps = 150;
const int startBpm = 70;
const int maxBpm = 130;
const int finalBpm = 75;
// Змінні стану
int steps = 0;
float currentBpm = startBpm;
unsigned long lastStepTime = 0;
unsigned long lastBpmUpdateTime = 0;
unsigned long lastLedTime = 0;
const unsigned long stepInterval = 350; // Швидкість ходьби
const unsigned long recoveryInterval = 800; // Швидкість відновлення
bool ledState = LOW;
void setup() {
pinMode(LED_PIN, OUTPUT);
Wire.begin(21, 22);
lcd.init();
lcd.backlight();
updateDisplay();
}
void loop() {
unsigned long currentMillis = millis();
// === ФАЗА 1: АКТИВНІСТЬ ===
if (steps < targetSteps) {
if (currentMillis - lastStepTime >= stepInterval) {
lastStepTime = currentMillis;
steps++;
currentBpm = startBpm + ((float)steps / targetSteps) * (maxBpm - startBpm);
updateDisplay();
}
}
// === ФАЗА 2: ВІДПОЧИНОК ===
else {
if (currentBpm > finalBpm) {
if (currentMillis - lastBpmUpdateTime >= recoveryInterval) {
lastBpmUpdateTime = currentMillis;
currentBpm -= 1.0;
if (currentBpm < finalBpm) currentBpm = finalBpm;
updateDisplay();
}
}
}
// === ФАЗА 3: РОБОТА СВІТЛОДІОДА (ОНОВЛЕНО) ===
if (currentBpm > 115) {
// Якщо пульс більше 115 - світиться постійно!
digitalWrite(LED_PIN, HIGH);
} else {
// Якщо 115 або нижче - блимає в такт серцебиттю
float blinkInterval = 60000.0 / currentBpm;
if (currentMillis - lastLedTime >= (blinkInterval / 2)) {
lastLedTime = currentMillis;
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
}
}
}
// Оновлення екрана
void updateDisplay() {
lcd.setCursor(0, 0);
lcd.print("BPM: ");
lcd.print((int)currentBpm);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Steps: ");
lcd.print(steps);
lcd.print(" ");
}