BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Convert the second to hour not working!


#1

Hi friends. I want to converting the second to hour. the V20 is slider widget to change the x time from 1 to 10 but its only second and will not converting to minute. I’m adding this code countDown = x * 3600L * 1000 but it cannot reach 128 second. thanks if helping me.

the sketch:

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

const byte pirPin = 14;
const byte relayPin = 15;
const byte yellowledPin = 13;
const byte greenledPin = 12;
const byte calibrationTime = 5;
boolean alarmState = 0;
byte countDown = 0;
int x;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "efae6c01779746258427f7315da3102e";                 //Put your blink token
char ssid[] = "MikroTik Tesla";                            //Put your SSID of your connection
char pass[] = "12345678";                            //Put your Password of your connection
char server[] = "10.5.51.5";

SimpleTimer timer;

void setup()
{
  Blynk.begin(auth, ssid, pass, server, 8080);
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  digitalWrite(relayPin, 1); // high before pinMode stops relay chattering during bootup
  pinMode(relayPin, OUTPUT);
  pinMode(yellowledPin, OUTPUT);
  pinMode(greenledPin, OUTPUT);
  Serial.print(F("calibrating sensor "));
  for (int i = 0; i < calibrationTime; i++)
  {
    Serial.print(F("."));
    delay(1000);
  }
  Serial.println(F(" done"));
  Serial.println(F("SENSOR ACTIVE"));
  Serial.println(F("-------------"));
  timer.setInterval(1000L, sense);
}

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V20);
}

BLYNK_WRITE(V20) {
  x = param.asInt();
}

void sense() {
  if (digitalRead(pirPin) && !alarmState)
  { // PIR active and no alarm
    digitalWrite(relayPin, 0); // relay on
    digitalWrite(greenledPin, HIGH); // LED on
    Serial.println("motion detected");
    Serial.println("Relay ON");
    countDown = x * 3600L * 1000; // seconds
    alarmState = HIGH; // remember it
  }
  if (countDown == 0 && alarmState)
  { // end of countdown and alarm
    digitalWrite(relayPin, 1); // relay off
    digitalWrite(greenledPin, LOW);
    digitalWrite(yellowledPin, LOW);
    Serial.println(F("Lights off"));
    Serial.println(F("Relay OFF"));
    Serial.println(F("---------"));
    alarmState = LOW; // remember it
  }


  if (countDown > 0)
  {
    if (digitalRead(pirPin) == HIGH)
    { countDown = x * 3600L * 1000; // seconds
      digitalWrite(yellowledPin, LOW);
      digitalWrite(greenledPin, HIGH); // LED on
      Serial.println(F("motion detected"));
    }
    if (digitalRead(pirPin) == LOW)
    { digitalWrite(greenledPin, LOW); // LED off
      Serial.println(F("motion ended"));
    }
    // if countdown is still running
    Serial.println(countDown);
    if (countDown < 5)
    { // blink last 4sec
      for (int i = 0; i < 5; i ++)
      {
        digitalWrite(yellowledPin, !digitalRead(yellowledPin)); // flip pinstate
        delay(200);
      }
    }
    else delay(1000); // no blink
    countDown --; // -1
  }
}

void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

#2

This may be the problem, as even if x = 1, countDown would equal 3600000. If I am not mistaken a byte can olnly go up to 255.

I think you may have the same issue with an int. I would probably go with long countDown = 0L


#3

thanks. you saved my day :rose: