Timer code not as expected

I’ve got a 30 min timer to rest a pump for 1/2 hr after 1 hr running. Timer is reset when pump starts and is in the routine labeled “RestPump” about 2/3 down. Every 30min it loops and advances a counter. Counter is zeroed after 3 loops of if pump is off and not flagged as resting.

Except it doesn’t. It’s stuck on 3. (For debugging I send status to app.) I found one error but still something’s wrong. It’s probably something silly.

Any thots?

//V0 soldCnt, V1 ROState (LED), V2 ROOverride (sw), V3 XferState (LED), V4 XferOverride (sw)
//V5 DelayRORestart (LED), V6 LoopCntDelay V7 loopCntRO, V8 PumpResting (LED)

// ADDebug
#define BLYNK_TEMPLATE_ID "TMPL2***3635"
#define BLYNK_TEMPLATE_NAME "Acqu***eDebug"
#define BLYNK_AUTH_TOKEN "1F2v0ebHsMUNq***MJT9uFuCiu21fy"

#define BLYNK_FIRMWARE_VERSION "0.1.3"
#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "MinBlynkOTAESP32.h" // My own Blynk OTA setup. Add these 2 to sketch

int UpdateOnStatusTimerID, RestPumpTimerID;
bool ROState, XferState, CoinState;
bool ReConnected = false, DelayRORestart = false;
const byte ROPin = 16, XferPin = 17, CoinPin=23; 
const byte LEDPin = 2, ROPwrPin=18, XferPwrPin=19;
double SoldCnt, ComPauseTime;

char ssid[] = "Infornet_***staurante";
char pass[] = "***";

BlynkTimer timer; 

void setup() {  
  pinMode(ROPin, INPUT_PULLUP); 
...
  pinMode (XferPwrPin, OUTPUT) ;

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(1000L, PinCk);
  UpdateOnStatusTimerID = timer.setInterval(180000L, UpdateOnStatus); //120k=2m, 300k=5
  RestPumpTimerID = timer.setInterval(1800000L,RestPump); //30 min 
  Blynk.syncVirtual(V0,V2,V4); //sync relay w/ V switches & sold count
}

void loop(){

 Blynk.run();
 timer.run();
 
}

void PinCk(){  //1 sec timer
 static bool OldROState, OldXferState, OldCoinState;

 ROState = !digitalRead(ROPin); //invert for pullup
 if (ROState != OldROState){    //immediate state change, + timer 3m repeats
   Blynk.virtualWrite(V1, ROState);
   OldROState = ROState;
   if (ROState==true) { //if just turned on,  re-start Blynk timer
     timer.restartTimer(UpdateOnStatusTimerID);   //timer to send Blynk info
     timer.restartTimer(RestPumpTimerID);   //timer 60m on 30 off
   }
   if (ROState==false) {   //if just turned off, start re-start prog timer
     DelayRORestart = true; // flag to delay re-start of RO pump to allow raw water tank time to refill and avoid cycling
     Blynk.virtualWrite(V5, DelayRORestart); 
     digitalWrite(ROPwrPin,LOW); //relay on,pwr off
     Blynk.virtualWrite(V2, LOW); //may be redund, try wo dWrite 
   } 

  }
}

void UpdateOnStatus (){  // confirm ON status w app every X min (3)
  static int LoopCntDelay;
  
  if (ROState)  Blynk.virtualWrite(V1, ROState);
  if (XferState) Blynk.virtualWrite(V3, XferState);
  if (DelayRORestart) {  //mostly so if xfer empty gives time to fill some before restart
    LoopCntDelay++;
    Blynk.virtualWrite(V6, LoopCntDelay);
    if (LoopCntDelay >= 3) {
      digitalWrite(ROPwrPin,HIGH); //relay off,pwr on
      Blynk.virtualWrite(V2, HIGH); //may be redund, try wo dWrite 
      LoopCntDelay=0;
      DelayRORestart=false;
      Blynk.virtualWrite(V5, DelayRORestart);
      Blynk.virtualWrite(V6, LoopCntDelay);
    }
  }
}

void RestPump(){    //30 min
  static int LoopCntRO;
  static bool PumpResting;

  if (ROState||PumpResting){     //if pump on OR resting...
    LoopCntRO++;
    Blynk.virtualWrite(V7, LoopCntRO);
    if (LoopCntRO == 2) {     //1st loop do nothing (stays on), 2nd stop pump & set flag
      digitalWrite(ROPwrPin,LOW); //relay on, pwr off
      Blynk.virtualWrite(V2, LOW); //may be redund, try wo dWrite 
      PumpResting=true;
      Blynk.virtualWrite(V8, PumpResting);
    }
    if (LoopCntRO >= 3) {    //3rd loop turn on and reset counter & flag
      digitalWrite(ROPwrPin,HIGH); //relay off, pwr on
      Blynk.virtualWrite(V2, HIGH); //may be redund, try wo dWrite 
      LoopCntRO = 0;
      PumpResting=false;
      Blynk.virtualWrite(V8, PumpResting);
    }
  }
  else LoopCntRO = 0;    // if pump off and not resting, reset counter
}

BLYNK_CONNECTED() {
 // This function will run every time Blynk connection is established
  // Request Blynk server to re-send latest values for pins
  ReConnected = true;
  Blynk.syncAll();

  Blynk.virtualWrite(V1, ROState);
  Blynk.virtualWrite(V3, XferState);
}

BLYNK_WRITE(V0){ //seems redundant, but allows console to change value here
 if(ReConnected == true) {   //only update count if reconnected and APP > device (i.e. no sales while offline)
   double SoldCntTmp = param.asDouble();
   if (SoldCntTmp > SoldCnt) SoldCnt = SoldCntTmp;//if sale while offline
   else if (SoldCnt > SoldCntTmp)  Blynk.virtualWrite(V0, SoldCnt);
   ReConnected = false;
  }
  
  if (!CoinState){ //if CoinState, device initiated write, if !CoinState, app init and must update SoldCnt w/ new value even if less
   SoldCnt = param.asDouble(); // assigning incoming value from pin V0 to a variable
   }
}

/* BLYNK_WRITE(V5){
 int SwState = param.asInt();
 if (SwState) {
   Blynk.syncVirtual(V6);
   Blynk.disconnect();
   timer.setTimeout(ComPauseTime , []() //so this is suppposed to pause and hold place to start @ next line when time up
   {  
    Blynk.connect();
   }); 
  }
}

BLYNK_WRITE(V6){
ComPauseTime = param.asDouble();
ComPauseTime = ComPauseTime * 60000; // chg min to millis
} */

Just looked at your code on my phone, and the only thing that I can see is that you don’t have an if statement for LoopCntRO == 1

I suspect that’s causing LoopCntRO to be reset to 0 each time in your else condition.
Could be wrong though, difficult to follow the logic when viewing the code on a phone.

Pete.

There’s no action for 1. The counter increments, exits and only acts when counter is 2 or 3. It’s stuck on 3. It’s like the timer isn’t running.

The else is opposed to the “if pump on||pump resting” conditon, not the “if ==2”, “if==3” conditions.

Well, it’s supposed to be. I better check that

Ah ha! I think I found it. It IS setting to zero. I just forgot to update the app! There’s no “blynk.virtualWrite…” there.

Reminds me of a time a student of mine couldn’t figure out why a prog wasn’t printing to the screen. He used blue type on a blue background. It was printing, just couldn’t see it.

Thanks!

1 Like