Untitled

 avatar
unknown
plain_text
a year ago
3.1 kB
3
Indexable
#define BLYNK_TEMPLATE_ID "TMPL6QyTN_inE"
#define BLYNK_TEMPLATE_NAME "Voltage Sensor"
#define BLYNK_AUTH_TOKEN "lEjzYUmWHobuwSIiwKSsbrFn7xX9EsgU"

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);

const int voltagePin = A0;
const float voltageThreshold = 7.0;
const float calibrationFactor = 3.2;

int count = 0;
bool counted = false;  // Flag to track whether the voltage has been counted
int statusCycle = 0;   // 0: LOW, 1: NORMAL, 2: HIGH

// Replace with your network credentials
char ssid[] = "aleposyria";
char pass[] = "test123456789";

// Replace with your Blynk authentication token
char auth[] = "lEjzYUmWHobuwSIiwKSsbrFn7xX9EsgU";

void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);

  lcd.init();
  lcd.backlight();

  // Set initial LED states in Blynk
  Blynk.virtualWrite(V1, LOW);  // LED for 'Status: LOW'
  Blynk.virtualWrite(V2, LOW);  // LED for 'Status: NORMAL'
  Blynk.virtualWrite(V3, LOW);  // LED for 'Status: HIGH'
}

void loop() {
  Blynk.run();

  float voltage = getVoltage() * calibrationFactor;

  // Send voltage value to Blynk Gauge widget
  Blynk.virtualWrite(V0, voltage);

  lcd.setCursor(0, 0);
  lcd.print("Amplitude: ");
  lcd.print(voltage);
  lcd.print("V");

  if (voltage > voltageThreshold && !counted) {
    count++;
    counted = true;  // Set the flag to indicate that voltage has been counted
    lcd.setCursor(0, 1);
    lcd.print("R-R Peak: ");
    lcd.print(count);
    
    // Send 'R-R Peak' to Blynk
    Blynk.virtualWrite(V5, count);
  }

  if (voltage <= voltageThreshold) {
    counted = false;  // Reset the flag when voltage goes below the threshold
  }

  // Determine status based on count ranges
  if (count >= 0 && count <= 5) {
    statusCycle = 0;  // LOW
  } else if (count >= 6 && count <= 10) {
    statusCycle = 1;  // NORMAL
  } else if (count >= 11 && count <= 99) {
    statusCycle = 2;  // HIGH
  }

  // Display the corresponding status on the LCD
  lcd.setCursor(0, 3);
  switch (statusCycle) {
    case 0:
      lcd.print("Status   : LOW    ");
      // Turn on LED for 'Status: LOW'
      Blynk.virtualWrite(V1, HIGH);
      Blynk.virtualWrite(V2, LOW);
      Blynk.virtualWrite(V3, LOW);
      break;
    case 1:
      lcd.print("Status   : NORMAL ");
      // Turn on LED for 'Status: NORMAL'
      Blynk.virtualWrite(V1, LOW);
      Blynk.virtualWrite(V2, HIGH);
      Blynk.virtualWrite(V3, LOW);
      break;
    case 2:
      lcd.print("Status   : HIGH   ");
      // Turn on LED for 'Status: HIGH'
      Blynk.virtualWrite(V1, LOW);
      Blynk.virtualWrite(V2, LOW);
      Blynk.virtualWrite(V3, HIGH);
      break;
  }

  // Increment status cycle for the next iteration
  statusCycle = (statusCycle + 1) % 3;  // 3 is the number of status levels
}

float getVoltage() {
  int sensorValue = analogRead(voltagePin);
  float voltage = sensorValue * (5.0 / 1023.0);
  return voltage;
}
Editor is loading...
Leave a Comment