Random Device Offline (Clean Loop)

Also, I’d suggest that you change the way that you format your code when you use nested if statements. This is almost impossible to read…

void invertertime()
{
 if(CEBstate == LOW){
  if(inverterstate == HIGH){
led15.on(); 
Blynk.setProperty(V10, "color", "#23C48E");
Blynk.setProperty(V18, "color", "#23C48E"); //Inverter Status (ON) GREEN
digitalWrite(13,HIGH); //INV RELAY ON D7
delay(100);
Blynk.setProperty(V2, "offLabel", "OFF Inverter Before Starting");
Blynk.setProperty(V2, "onLabel", "OFF Inverter!");
   } else {
Blynk.setProperty(V18, "color", "#ED9D00"); //Inverter Status (OFF) Yellow
Blynk.setProperty(V10, "color", "#ED9D00");
digitalWrite(13,LOW); //INV RELAY OFF D7
delay(100);
Blynk.setProperty(V2, "offLabel", "Hold to Start");
Blynk.setProperty(V2, "onLabel", "Release");
   }
  } else {
  }
  
 if(inverterstate == HIGH && CEBsensor ==LOW){ 
   led16.on(); //Inverter Shutoff Indicator ON
   } else {
   led16.off(); //Inverter Shutoff Indicator OFF
  }
}

void runtime() 
{
  if(CEBstate == HIGH && Timerstate == HIGH){
times = ((millis() - msec));
Blynk.setProperty(V16, "label", "Current Generator Run Time");
Blynk.setProperty(V17, "label", "Current Fuel Consumption");
{
String readableTime;
getReadableTime(readableTime);
Blynk.virtualWrite(V16, readableTime);
Blynk.virtualWrite(V17, 1.7 * ((millis() - msec)/1000)/3600);
delay(100);
}
   } else {
   }

if(InvTimerstate == HIGH && CEBstate == LOW && Timerstate == LOW){
times = ((millis() - msec2));
Blynk.setProperty(V16, "label", "Inverter Run Time");
{
String readableTime;
getReadableTime(readableTime);
Blynk.virtualWrite(V16, readableTime);
}
delay(100);
   } else {
   }
}

Using indents sensibly (the Arduino IDE Tools > Auto Format will do most of the work for you) and adding some narrative will make the code much easier to follow. As you can (now) see there is an inconsistency about how you’ve written the last part of the code in this function…

void invertertime()
{
  if (CEBstate == LOW) 
  {
    if (inverterstate == HIGH) 
    {
      // we get here if CEBstate == LOW and inverterstate == HIGH
      led15.on();
      Blynk.setProperty(V10, "color", "#23C48E");
      Blynk.setProperty(V18, "color", "#23C48E"); //Inverter Status (ON) GREEN
      digitalWrite(13, HIGH); //INV RELAY ON D7
      delay(100);
      Blynk.setProperty(V2, "offLabel", "OFF Inverter Before Starting");
      Blynk.setProperty(V2, "onLabel", "OFF Inverter!");
    } 
    else
    {
      // we get here if CEBstate == LOW and inverterstate == LOW
      Blynk.setProperty(V18, "color", "#ED9D00"); //Inverter Status (OFF) Yellow
      Blynk.setProperty(V10, "color", "#ED9D00");
      digitalWrite(13, LOW); //INV RELAY OFF D7
      delay(100);
      Blynk.setProperty(V2, "offLabel", "Hold to Start");
      Blynk.setProperty(V2, "onLabel", "Release");
    }
  } 
  else
  {
    // we get here if CEBstate == HIGH
    // but we do nothing! maybe the code below should be incorportated here?
  }

  if (inverterstate == HIGH && CEBsensor == LOW)
  {
    // we get here if CEBstate == HIGH and CEBsensor == LOW
    led16.on(); //Inverter Shutoff Indicator ON
  }
  else
  {
    // we get here with any other combination of CEBstate and CEBsensor, eg -
    // CEBstate == HIGH and CEBsensor == LOW
    // CEBstate == LOW and CEBsensor == LOW
    // CEBstate == LOW and CEBsensor == HIGH       
    led16.off(); //Inverter Shutoff Indicator OFF
  }
}

A few other things…

Don’t assign a value to the variable that will hold your timer ID…

These are assigned by the BlynkTimer library and are zero based.
There used to be a bug regarding timer ID’s, which was solved using a sacrificial timer, but I’m not sure if the bug still exists. More info here…

The way that you’re using aliases for your virtual pins makes no sense…

You’d be far better doing this…

//Blynk LED Widgets 
WidgetLED COIL_LED(V3); 
WidgetLED FUEL_LED(V7); 
WidgetLED STARTER_LED(V4);
WidgetLED GENERATOR_SHUTOFF_LED (V13);
WidgetLED CEB_STATUS_LED(V14); 
WidgetLED lNVERTER_STATUS_LED(V18); 
WidgetLED INVERTER_SHUTOFF_LED(V20); 
WidgetLED CHANGEOVER_RELEASE_INDICATOR(V24); 

That way, you’ll be using meaningful names within your code, rather than simply mapping one meaningless number to another.

You also seem to have one widget that’s not mapped, and which you address directly…

Blynk.virtualWrite(V12, CEBsensor);

Pete.

1 Like