CountDown not counting down

Personally I think you should stick at it because you will learn far more for future projects than by using someone else code in a library. (Well at least for a simple count up/down routine anyway)

But at least you’re getting sorted either way.

Also what about a menu widget for your timer select? Costs the same as a numeric input and looks a little neater?

1 Like

Ok nearly there I have the timers working to a point. They count down!!! the problem is I cant seem to reset the time correctly between the 2 timers. What should happen is this

  1. When Ts = 1 the countdown timer should show 24:00:00

  2. When Ts = 2 the countdown timer should show 12:00:00

What is actually happening it is only showing the 24 version for both periods.
Any ideas


/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


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



unsigned long Sec = 1000UL;
unsigned long Min = Sec*60;
unsigned long Hr = Min*60;
unsigned long Dy = Hr*24;
unsigned long LastDeGas = 0;
unsigned long DeGasOff = Min*3;
unsigned long DeGasOn1 = Dy;
unsigned long DeGasOn2 = Dy/2;




char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

BlynkTimer timer;

//CONNECT TO BLYNK SERVER//
BLYNK_CONNECTED()
{
  Blynk.syncAll();
}

//#define Sec_Min  (60UL)
//#define Sec_Hr (3600UL)
//#define Sec_Dy  (Sec_Hr * 24L)


//#define totalSec(_time_) (_time_ % Sec_Min)  
//#define totalMin(_time_) ((_time_ / Sec_Min) % Sec_Min) 
//#define totalHr(_time_) (( _time_% Sec_Dy) / Sec_Hr)
//#define totalDy(_time_) ( _time_ / Sec_Dy) 
int CountdownRemainReset;
int CountdownRemain;
int CountdownTimer;

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

 CountdownTimer = timer.setInterval(1000, CountdownTimerFunction);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

BLYNK_WRITE(V1) 
{
  Ts = param.asInt();
  if (Ts == 1)
  {
    Ts = 1;
    timer.disable(CountdownTimer);
    CountdownRemain = DeGasOn1;
    CountdownShowFormatted(CountdownRemain);
    timer.enable(CountdownTimer);
  }
  else if (Ts == 2)
  {
    Ts = 2;
    timer.disable(CountdownTimer);
    CountdownRemain = DeGasOn2;
    CountdownShowFormatted(CountdownRemain);
    timer.enable(CountdownTimer);
  }
}

void CountdownTimerFunction()
{
  if ((Ts = 1) && (CountdownRemain))
  {
    CountdownRemain--;
    CountdownShowFormatted(CountdownRemain);
    if (!CountdownRemain) 
  {
    timer.disable(CountdownTimer);
    Blynk.virtualWrite(V0, "Degassing");
  }
  }
  if ((Ts = 2) && (CountdownRemain))
  {
    CountdownRemain--;
    CountdownShowFormatted(CountdownRemain);
    if (!CountdownRemain) 
  {
    timer.disable(CountdownTimer);
    Blynk.virtualWrite(V0, "Degassing");
  }
  }
}
    
    

void CountdownShowFormatted(int seconds) 
{
  long days = 0;
  long hours = 0;
  long mins = 0;
  long secs = 0;
  String secs_o = ":";
  String mins_o = ":";
  String hours_o = "";
  secs = seconds; // set the seconds remaining
  mins = secs / 60; //convert seconds to minutes
  hours = mins / 60; //convert minutes to hours
  days = hours / 24; //convert hours to days
  secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
  mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
  hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
  if (secs < 10) {
    secs_o = ":0";
  }
  if (mins < 10) {
    mins_o = ":0";
  }
  if (hours < 10) {
    hours_o = "0";
  }
  Blynk.virtualWrite(V0, hours_o + hours + mins_o + mins + secs_o + secs);

}

If you want to understand what’s happening with the various variables you’re using then you need to add some meaningful serial print statements that allow you to track program flow and variable values.

Code like this:

puts me off the idea of delving too deeply and trying to understand your logic.

Pete.

3 Likes

A input of 1 must equal 1 in order to be 1

Yep, it is totally logical in an illogical way :crazy_face:

2 Likes

if ts==1 , ts=1 but are you really sure ?:fearful:

you need == instead of =

this is the reason why that don’t work !

and

Ts = param.asInt(); 
if (Ts == 1) 
{
 Ts = 1; <<<<------------------------- It's no use

it does not make sense to me :thinking:

2 Likes

Ok I have an update. Everything seems to be working ok the issue seems to be void CountdownShowFormatted() which seems to take its reading from the 1st timer only (Ts == 1).
Any suggestions?

#define BLYNK_PRINT Serial


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



unsigned long Sec = 1000UL;
unsigned long Min = Sec*60;
unsigned long Hr = Min*60;
unsigned long Dy = Hr*24;
unsigned long LastDeGas = 0;
unsigned long DeGasOff = Min*3;
unsigned long DeGasOn1 = Dy;
unsigned long DeGasOn2 = Dy/2;




char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

BlynkTimer timer;

//CONNECT TO BLYNK SERVER//
BLYNK_CONNECTED()
{
  Blynk.syncAll();
}

//#define Sec_Min  (60UL)
//#define Sec_Hr (3600UL)
//#define Sec_Dy  (Sec_Hr * 24L)


//#define totalSec(_time_) (_time_ % Sec_Min)  
//#define totalMin(_time_) ((_time_ / Sec_Min) % Sec_Min) 
//#define totalHr(_time_) (( _time_% Sec_Dy) / Sec_Hr)
//#define totalDy(_time_) ( _time_ / Sec_Dy) 
int CountdownRemainReset;
int CountdownRemain;
int CountdownTimer;
int Ts;

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

 CountdownTimer = timer.setInterval(1000, CountdownTimerFunction);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

BLYNK_WRITE(V1) 
{
  Ts = param.asInt();
  if (Ts == 1)
  {
    timer.disable(CountdownTimer);
    if (CountdownRemain != DeGasOn1)
    {
      CountdownRemain = DeGasOn1;
      CountdownShowFormatted(CountdownRemain);
      timer.enable(CountdownTimer);
      Serial.print("DeGasOn1  ");   //this prints correct
      Serial.println(DeGasOn1);
      Serial.print("CountdownRemain  ");  //this prints correct
      Serial.println(CountdownRemain);
    }
  }
  else if (Ts == 2)
  {
    timer.disable(CountdownTimer);
    if (CountdownRemain != DeGasOn2)
    {
      CountdownRemain = DeGasOn2;
      CountdownShowFormatted(CountdownRemain);
      timer.enable(CountdownTimer);
      Serial.print("DeGasOn2  ");   //this prints correct
      Serial.println(DeGasOn2);
      Serial.print("CountdownRemain  ");    //this prints correct
      Serial.println(CountdownRemain);
    }
  }
}

void CountdownTimerFunction()
{
  if ((Ts == 1) && (CountdownRemain))
  {
    CountdownRemain--;
    CountdownShowFormatted(CountdownRemain);    //this conversion seems to take Ts == 1 reading only
          Serial.print("CountdownRemain#  ");   //this prints correct
      Serial.println(CountdownRemain);

    if (!CountdownRemain) 
  {
    timer.disable(CountdownTimer);
    Blynk.virtualWrite(V0, "Degassing");
  }
  }
  if ((Ts == 2) && (CountdownRemain))
  {
    CountdownRemain--;
    CountdownShowFormatted(CountdownRemain);    //this conversion seems to take Ts == 1 reading only
      Serial.print("CountdownRemain#  ");   //this prints correct
      Serial.println(CountdownRemain);

    if (!CountdownRemain) 
  {
    timer.disable(CountdownTimer);
    Blynk.virtualWrite(V0, "Degassing");
  }
  }
}
    
void CountdownShowFormatted(int seconds)    //this conversion seems to take Ts == 1 reading only
{
  long days = 0;
  long hours = 0;
  long mins = 0;
  long secs = 0;
  String secs_o = ":";
  String mins_o = ":";
  String hours_o = "";
  secs = seconds; // set the seconds remaining
  mins = secs / 60; //convert seconds to minutes
  hours = mins / 60; //convert minutes to hours
  days = hours / 24; //convert hours to days
  secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
  mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
  hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
  if (secs < 10) {
    secs_o = ":0";
  }
  if (mins < 10) {
    mins_o = ":0";
  }
  if (hours < 10) {
    hours_o = "0";
  }
  Blynk.virtualWrite(V0, hours_o + hours + mins_o + mins + secs_o + secs);
}
DeGasOn1  86400000
CountdownRemain  86400000
CountdownRemain#  86399999
CountdownRemain#  86399998
CountdownRemain#  86399997
CountdownRemain#  86399996
CountdownRemain#  86399995
CountdownRemain#  86399994
CountdownRemain#  86399993
CountdownRemain#  86399992
DeGasOn2  43200000
CountdownRemain  43200000
CountdownRemain#  43199999
CountdownRemain#  43199998
CountdownRemain#  43199997
CountdownRemain#  43199996
CountdownRemain#  43199995
CountdownRemain#  43199994
CountdownRemain#  43199993
CountdownRemain#  43199992
CountdownRemain#  43199991
CountdownRemain#  43199990
CountdownRemain#  43199989
CountdownRemain#  43199988
CountdownRemain#  43199987

That is causing the value of Ts to change from 1 to 2?

Pete.

@PeteKnight I have a numeric input widget. When I toggle up to 2 DeGasOn2 is active, when I toggle down to 1 DeGasOn1 is active. I just wanted to show that that part is working correctly as shown on the serial print. It is the void CountdownShowGormatted that is not working correctly. It seems to take the value of DeGasOn1 (probably because it is the first timer) and ignores when time 2 is called and active. The function does not seem distinguish which time is active

I’m struggling to see the issue.

You’ve included your serial output, which I assume shows what you believe to be an error, but I can’t work out why you think it’s wrong. Maybe you could explain what you think your serial output should look like?

Pete.

Ok…here goes
I have a numeric input widget and a numeric display widget. Numeric input widget has been set to 1 up to 2 to represent the 2 timers. The display is just that a display. What is supposed to happen is when I toggle the numeric input to 1 timer 1 is active and the display should show 24:00:00 and start counting down. When I toggle 2 on the numeric input the display should change to 12:00:00 and start counting down. The serial prints are showing that this is actually happening which is good so far. All of the timings at this point are in millis. What is supposed to happen now is the function show formatted should take the millis and convert to time and display, but what this function is actually doing is taking the millis set in the first timer only. This because it is reading a common variable called countdownremain. What I am struggling with is how to get this function to distinguish which timer has been set and to take the appropriate countdownremain period.

May not be of any help at all but just for completeness sake, you need to change the above to

unsigned long CountdownRemain;

This is just because you have previously assigned DeGasOn1 as unsigned long and you are setting this variable to CountdownRemain so they should be of the same type.

unsigned long DeGasOn1 = Dy;
...
CountdownRemain = DeGasOn1;

And also change your convert function from int seconds to unsigned long seconds.

void CountdownShowFormatted(unsigned long seconds)
{
    unsigned long days = 0;
    unsigned long hours = 0;
    unsigned long mins = 0;
    unsigned long secs = 0;
1 Like

Just looking at this again, are you wanting both countdowns to happen at the same time but independently of each other? (I mean if you set timer 1 going, then do you want this to carry on counting down even if then you select timer 2?
Or do you want the countdown to only use the time set from your timer selection?
(If you understand what I am trying to ask?)

I think he needs to reset timer 1 when he starts timer 2 :thinking:

Only 1 timer at a time will be working. I have included a reset already in the BLYNK_WRITE(V1). What this is going to be is a aquarium calcium reactor (CO2 type). They need to be degassed at least 1 a day for them to function correctly and the more degasses the stronger the effluent. So timer 1, 1 degas/day, timer 2 2 degases/day ect…

BINGO!!! Done it!!! The issue was the CountdownShowFormatted was in seconds and my timers was in millis, so by dividing the DeGasOn parameter by 1000 has loved the issue.

#define BLYNK_PRINT Serial


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



unsigned long Sec = 1000UL;
unsigned long Min = Sec*60;
unsigned long Hr = Min*60;
unsigned long Dy = Hr*24;
unsigned long LastDeGas = 0;
unsigned long DeGasOff = Min*3;
unsigned long DeGasOn1 = Dy;
unsigned long DeGasOn2 = Dy/2;




char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

BlynkTimer timer;

//CONNECT TO BLYNK SERVER//
BLYNK_CONNECTED()
{
  Blynk.syncAll();
}

long CountdownRemainReset;
long CountdownRemain;
int CountdownTimer;
int Ts;

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

 CountdownTimer = timer.setInterval(1000, CountdownTimerFunction);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

BLYNK_WRITE(V1) 
{
  Ts = param.asInt();
  if (Ts == 1)
  {
    timer.disable(CountdownTimer);
    if (CountdownRemain != DeGasOn1)
    {
      CountdownRemain = DeGasOn1/1000;
      CountdownShowFormatted(CountdownRemain);

      timer.enable(CountdownTimer);
      Serial.print("DeGasOn1  ");   //this prints correct
      Serial.println(DeGasOn1);
      Serial.print("CountdownRemain  ");  //this prints correct
      Serial.println(CountdownRemain);
    }
  }
  else if (Ts == 2)
  {
    timer.disable(CountdownTimer);
    if (CountdownRemain != DeGasOn2)
    {
      CountdownRemain = DeGasOn2/1000;
     CountdownShowFormatted(CountdownRemain);

      timer.enable(CountdownTimer);
      Serial.print("DeGasOn2  ");   //this prints correct
      Serial.println(DeGasOn2);
      Serial.print("CountdownRemain  ");    //this prints correct
      Serial.println(CountdownRemain);
    }
  }
}

void CountdownTimerFunction()
{
  if ((Ts == 1) && (CountdownRemain))
  {
    CountdownRemain--;
    CountdownShowFormatted(CountdownRemain);    //this conversion seems to take Ts == 1 reading only
    Serial.print("CountdownRemain#  ");   //this prints correct
    Serial.println(CountdownRemain);
    if (!CountdownRemain) 
  {
    timer.disable(CountdownTimer);
    Blynk.virtualWrite(V0, "Degassing");
  }
  }
  if ((Ts == 2) && (CountdownRemain))
  {
    CountdownRemain--;
    CountdownShowFormatted(CountdownRemain);    //this conversion seems to take Ts == 1 reading only
    Serial.print("CountdownRemain#  ");   //this prints correct
    Serial.println(CountdownRemain);
    if (!CountdownRemain) 
  {
    timer.disable(CountdownTimer);
    Blynk.virtualWrite(V0, "Degassing");
  }
  }
}
    
void CountdownShowFormatted(long seconds)    //this conversion seems to take Ts == 1 reading only
{
  long days = 0;
  long hours = 0;
  long mins = 0;
  long secs = 0;
  String secs_o = ":";
  String mins_o = ":";
  String hours_o = "";
  secs = seconds;
  mins = secs / 60;
  hours = mins / 60;
  days = hours / 24;
  secs = secs - (mins * 60);
  mins = mins - (hours * 60); 

  if (secs < 10)
  {
    secs_o = ":0";
  }
  if (mins < 10) {
    mins_o = ":0";
  }
  if (hours < 10) {
    hours_o = "0";
  }
  Blynk.virtualWrite(V0, hours_o + hours + mins_o + mins + secs_o + secs);
}
3 Likes