Script keeps halting (on MEGA via USB link)

I havde the following sketch for a reef tank controller.
V2 to V8 are sliders controlling the brightness over PWM of LEDS.
V9 is a save button
V10 is a reset button to reset the sliders to 0 at the end of programming new light settings.

There is a problem that happens at different times. I will adjust the sliders and it works, I will press save and it works, I press reset and it works, but EVERYTIME it eventually freezes up. Currently V9 is stuck on so it clearly isn’t running the function that V9 is supposed to trigger. Can you see why?

// -- EEPROM KEY --
// No - DESCRIPTION
// 0  - TEST CHANNEL
// 1  - lunar.ch1
// 2  - lunar.ch2
// 3  - lunar.ch3
// 4  - lunar.ch4
// 5  - lunar.ch5
// 6  - lunar.ch6
// 7  - lunar.ch7
// 8  - lowsun.ch1
// 9  - lowsun.ch2
// 10 - lowsun.ch3
// 11 - lowsun.ch4
// 12 - lowsun.ch5
// 13 - lowsun.ch6
// 14 - lowsun.ch7
// 15 - midsun.ch1
// 16 - midsun.ch2
// 17 - midsun.ch3
// 18 - midsun.ch4
// 19 - midsun.ch5
// 20 - midsun.ch6
// 21 - midsun.ch7
// 22 - highsun.ch1
// 23 - highsun.ch2
// 24 - highsun.ch3
// 25 - highsun.ch4
// 26 - highsun.ch5
// 27 - highsun.ch6
// 28 - highsun.ch7
// 29 - ****  SPARE  ****
// 30 - lunartolowsunStartH
// 31 - lowsuntomidsunStartH
// 32 - midsuntohighsunStartH
// 33 - highsuntomidsunStartH
// 34 - midsuntolowsunStartH
// 35 - lowsuntolunarStartH
// 36 - lunartolowsunStartM
// 37 - lowsuntomidsunStartM
// 38 - midsuntohighsunStartM
// 39 - highsuntomidsunStartM
// 40 - midsuntolowsunStartM
// 41 - lowsuntolunarStartM
// 42 - globalfadedurationM
// 43 - globalfadedurationS
// 44 - globalfadedurationMS
// 45 - ****  SPARE  ****
// 46 - currentlightmode    (0=lunar, 1=low sun, 2=mid sun, 3=high sun, 4=transition, 5=unknown)
// 47 - transitionMode (0- none, 1- l-ls, 2- ls-ms, 3- ms-hs, 4- hs-ms, 5- ms-ls, 6- ls-l)
// 48 - lightedit (0-NOT EDITING  1-EDIT LUNAR  2-EDIT LOWSUN  3-EDIT MIDSUN  4-EDIT HIGHSUN)
// 49 - fadeInProgress (0=false / 1=yes)
// 50 - ****  SPARE  ****
// 51 - currentColor.ch1
// 52 - currentColor.ch2
// 53 - currentColor.ch3
// 54 - currentColor.ch4
// 55 - currentColor.ch5
// 56 - currentColor.ch6
// 57 - currentColor.ch7

// 60 - Power socket 1  - UV
// 61 - Power socket 2  - ATO
// 62 - Power socket 3  - REACTOR
// 63 - Power socket 4  - DOSER
// 64 - Power socket 5  - SUMP LIGHT
// 65 - Power socket 6  - COOLING
// 66 - Power socket 7  - HEATER
// 67 - Power socket 8  - SKIMMER
// 68 - Power socket 9  - POWER HEADS
// 69 - Power socket 10 - RETURN PUMP







// -- VIRTUAL PIN KEY --
// No - DESCRIPTION
// 0  - LCD widget on lighting LEDs page
// 1  - MENU widget on lighting LEDs page
// 2  - SLIDER widget on lighting LEDs page (ch1)
// 3  - SLIDER widget on lighting LEDs page (ch2)
// 4  - SLIDER widget on lighting LEDs page (ch3)
// 5  - SLIDER widget on lighting LEDs page (ch4)
// 6  - SLIDER widget on lighting LEDs page (ch5)
// 7  - SLIDER widget on lighting LEDs page (ch6)
// 8  - SLIDER widget on lighting LEDs page (ch7)
// 9  - BUTTON widget save changes on lighting LEDs page 
// 10 - BUTTON widget to reset lights
// 11 - TIME INPUT widget for lighting TIMES (lunar to lowsun)
// 12 - TIME INPUT widget for lighting TIMES (lowsun to midsun)
// 13 - TIME INPUT widget for lighting TIMES (midsun to highsun)
// 14 - TIME INPUT widget for lighting TIMES (highsun to midsun)
// 15 - TIME INPUT widget for lighting TIMES (midsun to lowsun)
// 16 - TIME INPUT widget for lighting TIMES (lowsun to lunar)
// 17 - +/- BUTTONS widget for 'global' fade duration
// 18 - LABEL DISPLAY widget for 'global' fade duration
// 19 - DISPLAY Float switch 1  - not required
// 20 - DISPLAY Float switch 2  - not required
// 21 - DISPLAY Temp 1
// 22 - DISPLAY Temp 2
// 23 - DISPLAY Temp 3
// 24 - BUTTON sump light timer active
// 25 - TIME INPUT widget for sump lighting ON
// 26 - TIME INPUT widget for sump lighting OFF
// 27 - BUTTON for Arduino resetresetFunc(); //call reset
// 28 - 
// 29 - 

// POWER SOCKET BUTTONS
// 30 - UV
// 31 - ATO
// 32 - REACTOR PUMP
// 33 - DOSER PUMP POWER
// 34 - SUMP LIGHT
// 35 - COOLING FANS
// 36 - HEATER
// 37 - SKIMMER
// 38 - POWER HEADS
// 39 - RETURN PUMP
// POWER SOCKET LEDS
// 40 - UV
// 41 - ATO
// 42 - REACTOR PUMP
// 43 - DOSER PUMP POWER
// 44 - SUMP LIGHT
// 45 - COOLING FANS
// 46 - HEATER
// 47 - SKIMMER
// 48 - POWER HEADS
// 49 - RETURN PUMP

// FORCE LIGHT MODE
// 60 - BUTTON push widget LUNAR
// 61 - BUTTON push widget LOWSUN
// 62 - BUTTON push widget MIDSUN
// 63 - BUTTON push widget HIGHSUN
// 64 - BUTTON widget STORM

// DOSING
// 70 - BUTTON Dose 1 on/off
// 71 - BUTTON Dose 2 on/off
// 72 - BUTTON Dose 3 on/off
// 73 - BUTTON Dose 4 on/off
// 74 - BUTTON Dose 1 Prime
// 75 - BUTTON Dose 2 Prime
// 76 - BUTTON Dose 3 Prime
// 77 - BUTTON Dose 4 Prime
// 78 - BUTTON Dose 1 Reset
// 79 - BUTTON Dose 2 Reset
// 80 - BUTTON Dose 3 Reset
// 81 - BUTTON Dose 4 Reset
// 82 - SLIDER Dose 1 Reset
// 83 - SLIDER Dose 2 Reset
// 84 - SLIDER Dose 3 Reset
// 85 - SLIDER Dose 4 Reset
// 86 - METER Dose 1 Reset
// 87 - METER Dose 2 Reset
// 88 - METER Dose 3 Reset
// 89 - METER Dose 4 Reset
// 90 - VALUE Dose 1 Left
// 91 - VALUE Dose 2 Left
// 92 - VALUE Dose 3 Left
// 93 - VALUE Dose 4 Left
// 94 - TIME INPUT Dose 1 Start time
// 95 - TIME INPUT Dose 2 Start time
// 96 - TIME INPUT Dose 3 Start time
// 97 - TIME INPUT Dose 4 Start time

#define BLYNK_PRINT Serial1
#define ONE_WIRE_BUS_PIN 44   // TEMP SENSORS
#define BETWEEN 2579          // STORM
#define DURATION 100          // STORM
#define TIMES 7               // STORM

#include <BlynkSimpleStream.h>
#include <Wire.h>
#include <Time.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DS1307RTC.h>
#include <EEPROM.h>

OneWire oneWire(ONE_WIRE_BUS_PIN);
DallasTemperature sensors(&oneWire);

DeviceAddress Probe01 = { 0x28, 0x5E, 0x72, 0xC5, 0x06, 0x00, 0x00, 0xA4 }; 
DeviceAddress Probe02 = { 0x28, 0x9F, 0x49, 0x2F, 0x06, 0x00, 0x00, 0xAA };
DeviceAddress Probe03 = { 0x28, 0x7A, 0xFB, 0xC4, 0x06, 0x00, 0x00, 0x79 };

char auth[] = "65b59**********************************166";

//-----Temp Sensors
float tempC01, tempC02, tempC03;
unsigned int tempMillis = millis();

//----- Power Plugs
int pwr1, pwr2, pwr3, pwr4, pwr5, pwr6, pwr7, pwr8, pwr9, pwr10;
int pwrbut1, pwrbut2, pwrbut3, pwrbut4, pwrbut5, pwrbut6, pwrbut7, pwrbut8, pwrbut9, pwrbut10;
int sensorVal1, sensorVal2;
int sump1ontime, sump2ontime, sump1onH, sump1onM, sump2onH, sump2onM, sump1offH, sump1offM, sump2offH, sump2offM;
int sumptimer1active, sumptimer2active;

//----- Storm
unsigned long lastTime = 0;
int waitTime = 0;
int mixer, srumble, brumble, stormstatus=0, stormbutton;
unsigned long stormMillis=0;
unsigned int storm1, storm2, storm3, storm4, storm5, storm6, storm7;

//----- Lighting
int savechangesbut;    // button used to save new light levels
int resetchangesbut;    // button used to reset lighting after editing
int resetarduinobut;   // button used to reset the whooe Arduino

int lightedit=0;       // editing lights? // 0-NOT EDITING  1-EDIT LUNAR  2-EDIT LOWSUN  3-EDIT MIDSUN  4-EDIT HIGHSUN

int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int led6 = 7;
int led7 = 8;

long lunartolowsunSec, lowsuntomidsunSec, midsuntohighsunSec, highsuntomidsunSec, midsuntolowsunSec, lowsuntolunarSec;
int globalfadedurationM;
unsigned long globalfadedurationS, globalfadedurationMS;

int lunartolowsunStartH, lunartolowsunStartM;            // Start fade times
int lowsuntomidsunStartH, lowsuntomidsunStartM;
int midsuntohighsunStartH, midsuntohighsunStartM;
int highsuntomidsunStartH, highsuntomidsunStartM;
int midsuntolowsunStartH, midsuntolowsunStartM;
int lowsuntolunarStartH, lowsuntolunarStartM;

int fadeInProgress;   // 0=false / 1=yes
int currentLightMode, transitionMode;

unsigned long ch1LightMillis, ch2LightMillis, ch3LightMillis, ch4LightMillis, ch5LightMillis, ch6LightMillis, ch7LightMillis;
int ch1timeincr, ch2timeincr, ch3timeincr, ch4timeincr, ch5timeincr, ch6timeincr, ch7timeincr;
int colorDif1, colorDif2, colorDif3, colorDif4, colorDif5, colorDif6, colorDif7;
unsigned long savelightsMillis = 0;
int butLunar, butLowsun, butMidsun, butHighsun;
int zero=0;


struct LEDS // for storing light intensity values
{
  int ch1;
  int ch2;
  int ch3;
  int ch4;
  int ch5;
  int ch6;
  int ch7;
};

typedef struct LEDS LightColor;

LightColor currentColor = {
  0,0,0,0,0,0,0};               // The current color of the light (used for fading)
LightColor lastColor = {
  0,0,0,0,0,0,0};               // The previous color of the light (used for fading)
LightColor targetColor = {
  0,0,0,0,0,0,0};               // The target color of the light (used for fading)
  
LightColor lunar = {            // Lunar colours
  0,0,0,0,0,0,0};             
LightColor lowsun = {           // Lowsun colours
  0,0,0,0,0,0,0};              
LightColor midsun = {           // Midsun colours
  0,0,0,0,0,0,0};               
LightColor highsun = {          // Highsun colours
  0,0,0,0,0,0,0};       
LightColor edit = {             // Used to edit colours
  0,0,0,0,0,0,0};

//----- Feeding
int feeding;

//----- RTC
unsigned long time;
tmElements_t tm; 

//----- Dosing
long dose1startTime, dose2startTime, dose3startTime, dose4startTime;

void(* resetFunc) (void) = 0; //declare reset function @ address 0

/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////                                             ///////////////////////
/////////////////////      B  L  Y  N  K    I  N  P  U  T  S      ///////////////////////
/////////////////////                                             ///////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////


BLYNK_WRITE(V1) 
{
  switch (param.asInt())
  {
    case 1: 
    
      lightedit=0;    // 0-NOT EDITING  1-EDIT LUNAR  2-EDIT LOWSUN  3-EDIT MIDSUN  4-EDIT HIGHSUN
  edit.ch1=highsun.ch1;
  edit.ch2=highsun.ch2;
  edit.ch3=highsun.ch3;
  edit.ch4=highsun.ch4;
  edit.ch5=highsun.ch5;
  edit.ch6=highsun.ch6;
  edit.ch7=highsun.ch7;
  Blynk.virtualWrite(V2, edit.ch1);
  Blynk.virtualWrite(V3, edit.ch2);
  Blynk.virtualWrite(V4, edit.ch3);
  Blynk.virtualWrite(V5, edit.ch4);
  Blynk.virtualWrite(V6, edit.ch5);
  Blynk.virtualWrite(V7, edit.ch6);
  Blynk.virtualWrite(V8, edit.ch7);
  showeditleds();
  break;
  
case 2: // E  D  I  T    L  U  N  A  R    L  I  G  H  T  I  N  G

  lightedit=1;    // 0-NOT EDITING  1-EDIT LUNAR  2-EDIT LOWSUN  3-EDIT MIDSUN  4-EDIT HIGHSUN
  edit.ch1=lunar.ch1;
  edit.ch2=lunar.ch2;
  edit.ch3=lunar.ch3;
  edit.ch4=lunar.ch4;
  edit.ch5=lunar.ch5;
  edit.ch6=lunar.ch6;
  edit.ch7=lunar.ch7;
  Blynk.virtualWrite(V2, edit.ch1);
  Blynk.virtualWrite(V3, edit.ch2);
  Blynk.virtualWrite(V4, edit.ch3);
  Blynk.virtualWrite(V5, edit.ch4);
  Blynk.virtualWrite(V6, edit.ch5);
  Blynk.virtualWrite(V7, edit.ch6);
  Blynk.virtualWrite(V8, edit.ch7);
  showeditleds();
  break;
  
case 3: // E  D  I  T    L  O  W    S  U  N    L  I  G  H  T  I  N  G

  lightedit=2;    // 0-NOT EDITING  1-EDIT LUNAR  2-EDIT LOWSUN  3-EDIT MIDSUN  4-EDIT HIGHSUN
  edit.ch1=lowsun.ch1;
  edit.ch2=lowsun.ch2;
  edit.ch3=lowsun.ch3;
  edit.ch4=lowsun.ch4;
  edit.ch5=lowsun.ch5;
  edit.ch6=lowsun.ch6;
  edit.ch7=lowsun.ch7;
  Blynk.virtualWrite(V2, edit.ch1);
  Blynk.virtualWrite(V3, edit.ch2);
  Blynk.virtualWrite(V4, edit.ch3);
  Blynk.virtualWrite(V5, edit.ch4);
  Blynk.virtualWrite(V6, edit.ch5);
  Blynk.virtualWrite(V7, edit.ch6);
  Blynk.virtualWrite(V8, edit.ch7);
  showeditleds();
  break;
  
case 4: // E  D  I  T    M  I  D    S  U  N    L  I  G  H  T  I  N  G

  lightedit=3;    // 0-NOT EDITING  1-EDIT LUNAR  2-EDIT LOWSUN  3-EDIT MIDSUN  4-EDIT HIGHSUN
  edit.ch1=midsun.ch1;
  edit.ch2=midsun.ch2;
  edit.ch3=midsun.ch3;
  edit.ch4=midsun.ch4;
  edit.ch5=midsun.ch5;
  edit.ch6=midsun.ch6;
  edit.ch7=midsun.ch7;
  Blynk.virtualWrite(V2, edit.ch1);
  Blynk.virtualWrite(V3, edit.ch2);
  Blynk.virtualWrite(V4, edit.ch3);
  Blynk.virtualWrite(V5, edit.ch4);
  Blynk.virtualWrite(V6, edit.ch5);
  Blynk.virtualWrite(V7, edit.ch6);
  Blynk.virtualWrite(V8, edit.ch7);
  showeditleds();
  break;
  
case 5: // E  D  I  T    H  I  G  H    S  U  N    L  I  G  H  T  I  N  G

  lightedit=4;    // 0-NOT EDITING  1-EDIT LUNAR  2-EDIT LOWSUN  3-EDIT MIDSUN  4-EDIT HIGHSUN
  edit.ch1=highsun.ch1;
  edit.ch2=highsun.ch2;
  edit.ch3=highsun.ch3;
  edit.ch4=highsun.ch4;
  edit.ch5=highsun.ch5;
  edit.ch6=highsun.ch6;
  edit.ch7=highsun.ch7;
  Blynk.virtualWrite(V2, edit.ch1);
  Blynk.virtualWrite(V3, edit.ch2);
  Blynk.virtualWrite(V4, edit.ch3);
  Blynk.virtualWrite(V5, edit.ch4);
  Blynk.virtualWrite(V6, edit.ch5);
  Blynk.virtualWrite(V7, edit.ch6);
  Blynk.virtualWrite(V8, edit.ch7);
  showeditleds();
  break;
  }
}


BLYNK_WRITE(V2)
{
  edit.ch1 = param.asInt();
  analogWrite(led1, edit.ch1);
}

BLYNK_WRITE(V3)
{
  edit.ch2 = param.asInt();
  analogWrite(led2, edit.ch2);
}

BLYNK_WRITE(V4)
{
  edit.ch3 = param.asInt(); 
  analogWrite(led3, edit.ch3);
}

BLYNK_WRITE(V5)
{
  edit.ch4 = param.asInt(); 
  analogWrite(led4, edit.ch4);
}

BLYNK_WRITE(V6)
{
  edit.ch5 = param.asInt(); 
  analogWrite(led5, edit.ch5);
}

BLYNK_WRITE(V7)
{
  edit.ch6 = param.asInt(); 
  analogWrite(led6, edit.ch6);
}

BLYNK_WRITE(V8)
{
  edit.ch7 = param.asInt(); 
  analogWrite(led7, edit.ch7);
}



BLYNK_WRITE(V9)    // Save Lighting Changes
{
  savechangesbut = param.asInt(); 
  
  if (savechangesbut == 1)
{
  savevalues();
}
}

BLYNK_WRITE(V10)    // Save Lighting Changes
{
  resetchangesbut = param.asInt(); 
  
  if (resetchangesbut == 1)void(* resetFunc) (void) = 0;
{
  resetlights();
}
}



//======================================================================================
//================================================  LIGHTINGS (TIMES)  V10 - V16  ====== 
//======================================================================================

//=============================================== FADE START TIMES

BLYNK_WRITE(V11)    // LUNAR TO LOWSUN (TIME)
{
  TimeInputParam t(param);
  if (t.hasStartTime())
  {
lunartolowsunStartH=t.getStartHour();
lunartolowsunStartM=t.getStartMinute();
  }
}

BLYNK_WRITE(V12)    // LOWSUN TO MIDSUN (TIME)
{
  TimeInputParam t(param);
  if (t.hasStartTime())
  {
lowsuntomidsunStartH=t.getStartHour();
lowsuntomidsunStartM=t.getStartMinute();
  }
}

BLYNK_WRITE(V13)    // MIDSUN TO HIGHSUN (TIME)
{
  TimeInputParam t(param);
  if (t.hasStartTime())
  {
midsuntohighsunStartH=t.getStartHour();
midsuntohighsunStartM=t.getStartMinute();
  }
}

BLYNK_WRITE(V14)    // HIGHSUN TO MIDSUN (TIME)
{
  TimeInputParam t(param);
  if (t.hasStartTime())
  {
highsuntomidsunStartH=t.getStartHour();
highsuntomidsunStartM=t.getStartMinute();
  }
}

BLYNK_WRITE(V15)    // MIDSUN TO LOWSUN (TIME)
{
  TimeInputParam t(param);
  if (t.hasStartTime())
  {
midsuntolowsunStartH=t.getStartHour();
midsuntolowsunStartM=t.getStartMinute();
  }
}

BLYNK_WRITE(V16)    // LOWSUN TO LUNAR (TIME)
{
  TimeInputParam t(param);
  if (t.hasStartTime())
  {
lowsuntolunarStartH=t.getStartHour();
lowsuntolunarStartM=t.getStartMinute();
  }
}

//=============================================== 'GLOBAL' FADE DURATION

BLYNK_WRITE(V17)    // +/- BUTTONS
{
  globalfadedurationM = param[0].asLong();            // Obtain fade minutes
  
 // IF THIS IS PRINTED TO THE SCREEN LOTS AND LOTS OF TIMES THEN THIS IS BEING CALLED ALL OF THE TIME AND NOT JUST WHEN THE BUTTON IS PRESSED. YOU NEED TO RETHINK THE CALCUATIONS BELOW AND THE WRITE TO EEPROM.");
  globalfadedurationS = globalfadedurationM * 60;     // Calculate fade seconds
  globalfadedurationMS = globalfadedurationS * 1000;  // Calculate fade miliseconds
  
  Blynk.virtualWrite(V12, globalfadedurationM);         // fade time in minutes to V12 labled display
  
  EEPROM.write(42, globalfadedurationM);    // Save new values to EEPROM
  //EEPROM.write(43, globalfadedurationS);
  //EEPROM.write(44, globalfadedurationMS);
}


BLYNK_WRITE(V27)    // Call reset
{
  resetarduinobut = param.asInt();
  
  if (resetarduinobut == 1) 
  {
resetFunc();
  }
}

//======================================================================================
//====================================================  POWER SUPPLIES  V30 - V39  ===== 
//======================================================================================

BLYNK_WRITE(V30)    // UV
{
  pwrbut1 = param.asInt();
  if (pwrbut1 == 1)  // 1 - UV
  {
pwr1on();
  } 
  else 
  {
pwr1off();
  }
}

BLYNK_WRITE(V31)    // ATO  ----------> IN VOID LOOP
{
  pwrbut2 = param.asInt();
}

BLYNK_WRITE(V32)    // REACTOR PUMP
{
  pwrbut3 = param.asInt();
  if (pwrbut3 == 1)  // 3 - REACTOR PUMP
  {
pwr3on();
  } 
  else 
  {
pwr3off();
  }
}

BLYNK_WRITE(V33)    // DOSER
{
  pwrbut4 = param.asInt();
  if (pwrbut4 == 1)  // 4 - DOSER PUMP POWER
  {
pwr4on();
  } 
  else 
  {
pwr4off();
  }
}

BLYNK_WRITE(V34)    // SUMP LIGHT 1
{
  pwrbut5 = param.asInt();
  if (pwrbut5 == 1)  // 5 - SUMP LIGHT
  {
pwr5on();
  } 
  else 
  {
pwr5off();
  }
}

BLYNK_WRITE(V35)    // COOLING FANS
{
  pwrbut6 = param.asInt();
  if ((pwrbut6 == 1) || (sump2ontime == 1))  // 6 - COOLING FANS
  {
pwr6on();
  } 
  else 
  {
pwr6off();
  }
}

BLYNK_WRITE(V36)    // HEATER
{
  pwrbut7 = param.asInt();
  if (pwrbut7 == 1)  // 7 - HEATER
  {
pwr7on();
  } 
  else 
  {
pwr7off();
  }
}

BLYNK_WRITE(V37)    // SKIMMER  ----------> IN VOID LOOP
{
  pwrbut8 = param.asInt();
}

BLYNK_WRITE(V38)    // POWER HEADS
{
  pwrbut9 = param.asInt();
  if ((pwrbut9 == 1) && (feeding == 0))  // 9 - POWER HEADS
  {
pwr9on();
  } 
  else 
  {
pwr9off();
  }
}

BLYNK_WRITE(V39)    // RETURN PUMP
{
  pwrbut10 = param.asInt();
  if (pwrbut10 == 1)  // 10 - RETURN PUMP
  {
pwr10on();
  } 
  else 
  {
pwr10off();
  }
}

BLYNK_WRITE(V60)      // Call Full Lunar
{
  butLunar = param[0].asLong();
  if (butLunar == 1)
  {
lunarFull();
  }
}

BLYNK_WRITE(V61)       // Call Full Lowsun
{
  butLowsun = param[0].asLong();
  if (butLowsun == 1)
  {
lowsunFull();
  }
}

BLYNK_WRITE(V62)       // Call Full Midsun
{
   butMidsun = param[0].asLong();
   if (butMidsun == 1)
  {
midsunFull();
  }
}

BLYNK_WRITE(V63)       // Call Full Highsun
{
  butHighsun = param[0].asLong();
  if (butHighsun == 1)
  {
highsunFull();
  }
}

BLYNK_WRITE(V64)    // STORM BUTTON
{
  stormbutton = param.asInt();
  if (stormbutton == 1)  
  {
//stormstatus=1;  // 0- Off  1- fade down  2- storm on  3- fade up 
stormbegin();
  } 
}

BLYNK_WRITE(V94) 
{
  dose1startTime = param[0].asLong();
}

BLYNK_WRITE(V95) 
{
  dose2startTime = param[0].asLong();
}

BLYNK_WRITE(V96) 
{
  dose3startTime = param[0].asLong();
}

BLYNK_WRITE(V97) 
{
  dose4startTime = param[0].asLong();
}

BLYNK_READ(V90)
{
  Blynk.virtualWrite(V90, dose1startTime);
}
void synclights()
{
// LOAD FULL MODE
  
  if (currentLightMode == 0)    // 0=lunar, 1=low sun, 2=mid sun, 3=high sun, 4=transition, 5=unknown
  {
lunarFull();
  }

  if (currentLightMode == 1)
  {
lowsunFull();
  }
  
  if (currentLightMode == 2)
  {
midsunFull();
  }
  
  if (currentLightMode == 3)
  {
highsunFull();
  }
  
  // LOAD RAMP MODE
  
  if (currentLightMode == 4)    // 0=lunar, 1=low sun, 2=mid sun, 3=high sun, 4=transition, 5=unknown
  { 
if (transitionMode == 1)    // 0- none, 1- l-ls, 2- ls-ms, 3- ms-hs, 4- hs-ms, 5- ms-ls, 6- ls-l
{
  lunar2lowsun();
}

if (transitionMode == 2)
{
  lowsun2midsun();
}

if (transitionMode == 3)
{
  midsun2highsun();
}

if (transitionMode == 4)
{
  highsun2midsun();
}

if (transitionMode == 5)
{
  midsun2lowsun();
}

if (transitionMode == 6)
{
  lowsun2lunar();
}
  }
  
  analogWrite(led1, currentColor.ch1);      
  analogWrite(led2, currentColor.ch2);       
  analogWrite(led3, currentColor.ch3);      
  analogWrite(led4, currentColor.ch4); 
  analogWrite(led5, currentColor.ch5);       
  analogWrite(led6, currentColor.ch6);      
  analogWrite(led7, currentColor.ch7); 
}



void syncPower()
{
  if (pwr1 == 1)
  {
pwr1on();
  }
  else
  {
pwr1off();
  }
  
  if (pwr2 == 1)
  {
pwr2on();
  }
  else
  {
pwr2off();
  }
  
  if (pwr3 == 1)
  {
pwr3on();
  }
  else
  {
pwr3off();
  }
  
  if (pwr4 == 1)
  {
pwr4on();
  }
  else
  {
pwr4off();
  }
  
  if (pwr5 == 1)
  {
pwr5on();
  }
  else
  {
pwr5off();
  }
  
  if (pwr6 == 1)
  {
pwr6on();
  }
  else
  {
pwr6off();
  }
  
  if (pwr7 == 1)
  {
pwr7on();
  }
  else
  {
pwr7off();
  }
  
  if (pwr8 == 1)
  {
pwr8on();
  }
  else
  {
pwr8off();
  }
  
  if (pwr9 == 1)
  {
pwr9on();
  }
  else
  {
pwr9off();
  }
  
  if (pwr10 == 1)
  {
pwr10on();
  }
  else
  {
pwr10off();
  }
}



/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////                                             ///////////////////////
/////////////////////         V  O  I  D    S  E  T  U  P         ///////////////////////
/////////////////////                                             ///////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

void setup()
{
  Serial1.begin(9600);
  Serial.begin(9600);
 
  sensors.begin();
  sensors.setResolution(Probe01, 10);
  sensors.setResolution(Probe02, 10);
  sensors.setResolution(Probe03, 10);
  
  pinMode(led1, OUTPUT);  // LED CH1
  pinMode(led2, OUTPUT);  // LED CH2
  pinMode(led3, OUTPUT);  // LED CH3
  pinMode(led4, OUTPUT);  // LED CH4
  pinMode(led5, OUTPUT);  // LED CH5
  pinMode(led6, OUTPUT);  // LED CH6
  pinMode(led7, OUTPUT);  // LED CH7
  
  pinMode(22, OUTPUT); // POWER SECTION
  pinMode(23, OUTPUT); // ----"---"----
  pinMode(24, OUTPUT); // ----"---"----
  pinMode(25, OUTPUT); // ----"---"----
  pinMode(26, OUTPUT); // ----"---"----
  pinMode(27, OUTPUT); // ----"---"----
  pinMode(28, OUTPUT); // ----"---"----
  pinMode(29, OUTPUT); // ----"---"----
  pinMode(30, OUTPUT); // ----"---"----
  pinMode(31, OUTPUT); // ----"---"----
  
  pinMode(51, INPUT_PULLUP);    // Float switch 1
  pinMode(52, INPUT_PULLUP);    // Float switch 2
  
  uploadToBlynk();
  //startup();
  
  Blynk.begin(Serial, auth);
  
  resetarduinobut=0;
  Blynk.virtualWrite(V27, resetarduinobut);
}



//======================================================================================
//==========================================================  START OF LIGHT RAMPS ===== 
//======================================================================================

void lunar2lowsun()          
{  
  lastColor.ch1 = currentColor.ch1;    // last color is the starting point of the fade
  lastColor.ch2 = currentColor.ch2;
  lastColor.ch3 = currentColor.ch3;
  lastColor.ch4 = currentColor.ch4;
  lastColor.ch5 = currentColor.ch5;
  lastColor.ch6 = currentColor.ch6;
  lastColor.ch7 = currentColor.ch7;
  
  targetColor.ch1 = lowsun.ch1;    // target color is low sun
  targetColor.ch2 = lowsun.ch2;
  targetColor.ch3 = lowsun.ch3;
  targetColor.ch4 = lowsun.ch4;
  targetColor.ch5 = lowsun.ch5;
  targetColor.ch6 = lowsun.ch6;
  targetColor.ch7 = lowsun.ch7;
 
  getlightcalcs();  
  fadeInProgress = 1;            // 0=false / 1=yes = true;
  currentLightMode=4;            // 0=lunar, 1=low sun, 2=mid sun, 3=high sun, 4=transition, 5=unknown
  transitionMode=1;              // 0- none, 1- l-ls, 2- ls-ms, 3- ms-hs, 4- hs-ms, 5- ms-ls, 6- ls-l
  savelightsMillis = millis();
  EEPROM.write(46,currentLightMode);
  EEPROM.write(47,transitionMode);
}


void lowsun2midsun()          // Low sun to mid sun 
{
  lastColor.ch1 = currentColor.ch1;
  lastColor.ch2 = currentColor.ch2;
  lastColor.ch3 = currentColor.ch3;
  lastColor.ch4 = currentColor.ch4;
  lastColor.ch5 = currentColor.ch5;
  lastColor.ch6 = currentColor.ch6;
  lastColor.ch7 = currentColor.ch7;
  
  targetColor.ch1 = midsun.ch1;
  targetColor.ch2 = midsun.ch2;
  targetColor.ch3 = midsun.ch3;
  targetColor.ch4 = midsun.ch4;
  targetColor.ch5 = midsun.ch5;
  targetColor.ch6 = midsun.ch6;
  targetColor.ch7 = midsun.ch7;
  
  getlightcalcs();
  fadeInProgress = 1;            // 0=false / 1=yes = true;
  currentLightMode=4;            // 0=lunar, 1=low sun, 2=mid sun, 3=high sun, 4=transition, 5=unknown
  transitionMode=2;              // 0- none, 1- l-ls, 2- ls-ms, 3- ms-hs, 4- hs-ms, 5- ms-ls, 6- ls-l
  savelightsMillis = millis();
  EEPROM.write(46,currentLightMode);
  EEPROM.write(47,transitionMode);
}


void midsun2highsun()          // Mid sun to high sun
{
  lastColor.ch1 = currentColor.ch1;
  lastColor.ch2 = currentColor.ch2;
  lastColor.ch3 = currentColor.ch3;
  lastColor.ch4 = currentColor.ch4;
  lastColor.ch5 = currentColor.ch5;
  lastColor.ch6 = currentColor.ch6;
  lastColor.ch7 = currentColor.ch7;
  
  targetColor.ch1 = highsun.ch1;
  targetColor.ch2 = highsun.ch2;
  targetColor.ch3 = highsun.ch3;
  targetColor.ch4 = highsun.ch4;
  targetColor.ch5 = highsun.ch5;
  targetColor.ch6 = highsun.ch6;
  targetColor.ch7 = highsun.ch7;
  
  getlightcalcs();
  fadeInProgress = 1;            // 0=false / 1=yes = true;
  currentLightMode=4;            // 0=lunar, 1=low sun, 2=mid sun, 3=high sun, 4=transition, 5=unknown
  transitionMode=3;              // 0- none, 1- l-ls, 2- ls-ms, 3- ms-hs, 4- hs-ms, 5- ms-ls, 6- ls-l
  savelightsMillis = millis();
  EEPROM.write(46,currentLightMode);
  EEPROM.write(47,transitionMode);
}

void highsun2midsun()          // High sun to mid sun
{
  lastColor.ch1 = currentColor.ch1;
  lastColor.ch2 = currentColor.ch2;
  lastColor.ch3 = currentColor.ch3;
  lastColor.ch4 = currentColor.ch4;
  lastColor.ch5 = currentColor.ch5;
  lastColor.ch6 = currentColor.ch6;
  lastColor.ch7 = currentColor.ch7;

  targetColor.ch1 = midsun.ch1;
  targetColor.ch2 = midsun.ch2;
  targetColor.ch3 = midsun.ch3;
  targetColor.ch4 = midsun.ch4;
  targetColor.ch5 = midsun.ch5;
  targetColor.ch6 = midsun.ch6;
  targetColor.ch7 = midsun.ch7;
  
  getlightcalcs();

  fadeInProgress = 1;            // 0=false / 1=yes = true;
  currentLightMode=4;            // 0=lunar, 1=low sun, 2=mid sun, 3=high sun, 4=transition, 5=unknown
  transitionMode=4;              // 0- none, 1- l-ls, 2- ls-ms, 3- ms-hs, 4- hs-ms, 5- ms-ls, 6- ls-l
  savelightsMillis = millis();
  EEPROM.write(46,currentLightMode);
  EEPROM.write(47,transitionMode);
}

void midsun2lowsun()          // Mid sun to low sun
{
  lastColor.ch1 = currentColor.ch1;
  lastColor.ch2 = currentColor.ch2;
  lastColor.ch3 = currentColor.ch3;
  lastColor.ch4 = currentColor.ch4;
  lastColor.ch5 = currentColor.ch5;
  lastColor.ch6 = currentColor.ch6;
  lastColor.ch7 = currentColor.ch7;
  
  targetColor.ch1 = lowsun.ch1;
  targetColor.ch2 = lowsun.ch2;
  targetColor.ch3 = lowsun.ch3;
  targetColor.ch4 = lowsun.ch4;
  targetColor.ch5 = lowsun.ch5;
  targetColor.ch6 = lowsun.ch6;
  targetColor.ch7 = lowsun.ch7;
  
  getlightcalcs();

  fadeInProgress = 1;            // 0=false / 1=yes = true;
  currentLightMode=4;            // 0=lunar, 1=low sun, 2=mid sun, 3=high sun, 4=transition, 5=unknown
  transitionMode=5;              // 0- none, 1- l-ls, 2- ls-ms, 3- ms-hs, 4- hs-ms, 5- ms-ls, 6- ls-l
  savelightsMillis = millis();
  EEPROM.write(46,currentLightMode);
  EEPROM.write(47,transitionMode);
}

void lowsun2lunar()          // Low sun to lunar
{
  lastColor.ch1 = currentColor.ch1;
  lastColor.ch2 = currentColor.ch2;
  lastColor.ch3 = currentColor.ch3;
  lastColor.ch4 = currentColor.ch4;
  lastColor.ch5 = currentColor.ch5;
  lastColor.ch6 = currentColor.ch6;
  lastColor.ch7 = currentColor.ch7;
  
  targetColor.ch1 = lunar.ch1;
  targetColor.ch2 = lunar.ch2;
  targetColor.ch3 = lunar.ch3;
  targetColor.ch4 = lunar.ch4;
  targetColor.ch5 = lunar.ch5;
  targetColor.ch6 = lunar.ch6;
  targetColor.ch7 = lunar.ch7;
  
  getlightcalcs();

  fadeInProgress = 1;            // 0=false / 1=yes = true;
  currentLightMode=4;            // 0=lunar, 1=low sun, 2=mid sun, 3=high sun, 4=transition, 5=unknown
  transitionMode=6;              // 0- none, 1- l-ls, 2- ls-ms, 3- ms-hs, 4- hs-ms, 5- ms-ls, 6- ls-l
  savelightsMillis = millis(); 
  EEPROM.write(46,currentLightMode);
  EEPROM.write(47,transitionMode);
}



void getlightcalcs()
{  
  // GET DIFFERENCE IN COLOUR
  colorDif1 = targetColor.ch1 - currentColor.ch1;    
  colorDif2 = targetColor.ch2 - currentColor.ch2;
  colorDif3 = targetColor.ch3 - currentColor.ch3;
  colorDif4 = targetColor.ch4 - currentColor.ch4;
  colorDif5 = targetColor.ch5 - currentColor.ch5;
  colorDif6 = targetColor.ch6 - currentColor.ch6;
  colorDif7 = targetColor.ch7 - currentColor.ch7;

 
  // TURN THE NEGATIVES INTO POSIITIVES
  if (colorDif1 < 0)
  {
colorDif1 = colorDif1 * -1;
  }
  
  if (colorDif2 < 0)
  {
colorDif2 = colorDif2 * -1;
  }
  
  if (colorDif3 < 0)
  {
colorDif3 = colorDif3 * -1;
  }
  
  if (colorDif4 < 0)
  {
colorDif4 = colorDif4 * -1;
  }
  
  if (colorDif5 < 0)
  {
colorDif5 = colorDif5 * -1;
  }
  
  if (colorDif6 < 0)
  {
colorDif6 = colorDif6 * -1;
  }
  
  if (colorDif7 < 0)
  {
colorDif7 = colorDif7 * -1;
  }
  
  // GET TIME BETWEEN INCREMENTS IN MILLISECONDS
  ch1timeincr = globalfadedurationMS / colorDif1 * 0.9;
  ch2timeincr = globalfadedurationMS / colorDif2 * 0.9;
  ch3timeincr = globalfadedurationMS / colorDif3 * 0.9;
  ch4timeincr = globalfadedurationMS / colorDif4 * 0.9;
  ch5timeincr = globalfadedurationMS / colorDif5 * 0.9;
  ch6timeincr = globalfadedurationMS / colorDif6 * 0.9;
  ch7timeincr = globalfadedurationMS / colorDif7 * 0.9;

  // RESET TIMER MILLIS 
  ch1LightMillis = millis();
  ch2LightMillis = millis();
  ch3LightMillis = millis();
  ch4LightMillis = millis();
  ch5LightMillis = millis();
  ch6LightMillis = millis();
  ch7LightMillis = millis();
}




/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////                                             ///////////////////////
/////////////////////        O  T  H  E  R    V  O  I  D  S       ///////////////////////
/////////////////////                                             ///////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

void printtemp1()
{
  if (tempC01 > 28)
  {
Blynk.setProperty(V21, "color", "#FF3333");
  }
  else if (tempC01 < 25)
  {
Blynk.setProperty(V21, "color", "#3399FF");
  }
  else
  {
Blynk.setProperty(V21, "color", "#000000");
  }
  
  Blynk.virtualWrite(V21, tempC01);
}

void printtemp2()
{
  if (tempC02 > 28)
  {
Blynk.setProperty(V22, "color", "#FF3333");
  }
  else if (tempC02 < 25)
  {
Blynk.setProperty(V22, "color", "#3399FF");
  }
  else
  {
Blynk.setProperty(V22, "color", "#000000");
  }
  
  Blynk.virtualWrite(V22, tempC02);
}

void printtemp3()
{
  if (tempC03 > 28)
  {
Blynk.setProperty(V23, "color", "#FF3333");
  }
  else if (tempC03 < 25)
  {
Blynk.setProperty(V23, "color", "#3399FF");
  }
  else
  {
Blynk.setProperty(V23, "color", "#000000");
  }
  
  Blynk.virtualWrite(V23, tempC03);
}



void showeditleds()
{
  analogWrite(led1, edit.ch1);
  analogWrite(led2, edit.ch2);
  analogWrite(led3, edit.ch3);
  analogWrite(led4, edit.ch4);
  analogWrite(led5, edit.ch5);
  analogWrite(led6, edit.ch6);
  analogWrite(led7, edit.ch7);
}

void savevalues()        // SAVE VALUES TO EEPROM
{
  if (lightedit==1)    // 0-NOT EDITING  1-EDIT LUNAR  2-EDIT LOWSUN  3-EDIT MIDSUN  3-EDIT HIGHSUN  4-EDIT HIGHSUN
  {
lunar.ch1=edit.ch1;
lunar.ch2=edit.ch2;
lunar.ch3=edit.ch3;
lunar.ch4=edit.ch4;
lunar.ch5=edit.ch5;
lunar.ch6=edit.ch6;
lunar.ch7=edit.ch7;
EEPROM.write(1, lunar.ch1);
EEPROM.write(2, lunar.ch2);
EEPROM.write(3, lunar.ch3);
EEPROM.write(4, lunar.ch4);
EEPROM.write(5, lunar.ch5);
EEPROM.write(6, lunar.ch6);
EEPROM.write(7, lunar.ch7);
  }
  else if (lightedit==2)
  {
lowsun.ch1=edit.ch1;
lowsun.ch2=edit.ch2;
lowsun.ch3=edit.ch3;
lowsun.ch4=edit.ch4;
lowsun.ch5=edit.ch5;
lowsun.ch6=edit.ch6;
lowsun.ch7=edit.ch7;
EEPROM.write(8, lowsun.ch1);
EEPROM.write(9, lowsun.ch2);
EEPROM.write(10, lowsun.ch3);
EEPROM.write(11, lowsun.ch4);
EEPROM.write(12, lowsun.ch5);
EEPROM.write(13, lowsun.ch6);
EEPROM.write(14, lowsun.ch7);
  }
  else if (lightedit==3)
  {
midsun.ch1=edit.ch1;
midsun.ch2=edit.ch2;
midsun.ch3=edit.ch3;
midsun.ch4=edit.ch4;
midsun.ch5=edit.ch5;
midsun.ch6=edit.ch6;
midsun.ch7=edit.ch7;
EEPROM.write(15, midsun.ch1);
EEPROM.write(16, midsun.ch2);
EEPROM.write(17, midsun.ch3);
EEPROM.write(18, midsun.ch4);
EEPROM.write(19, midsun.ch5);
EEPROM.write(20, midsun.ch6);
EEPROM.write(21, midsun.ch7);
  }
  else if (lightedit==4)
  {
highsun.ch1=edit.ch1;
highsun.ch2=edit.ch2;
highsun.ch3=edit.ch3;
highsun.ch4=edit.ch4;
highsun.ch5=edit.ch5;
highsun.ch6=edit.ch6;
highsun.ch7=edit.ch7;
EEPROM.write(22, highsun.ch1);
EEPROM.write(23, highsun.ch2);
EEPROM.write(24, highsun.ch3);
EEPROM.write(25, highsun.ch4);
EEPROM.write(26, highsun.ch5);
EEPROM.write(27, highsun.ch6);
EEPROM.write(28, highsun.ch7);
  }
  
  analogWrite(led1, 0);
  analogWrite(led2, 0);
  analogWrite(led3, 0);
  analogWrite(led4, 0);
  analogWrite(led5, 0);
  analogWrite(led6, 0);
  analogWrite(led7, 0);
  delay(250);
  analogWrite(led2, 200);
  delay(250);
  analogWrite(led2, 0);
  delay(250);
  analogWrite(led2, 200);
  delay(250);
  analogWrite(led2, 0);
  delay(250);
 
  analogWrite(led1, edit.ch1);
  analogWrite(led2, edit.ch2);
  analogWrite(led3, edit.ch3);
  analogWrite(led4, edit.ch4);
  analogWrite(led5, edit.ch5);
  analogWrite(led6, edit.ch6);
  analogWrite(led7, edit.ch7);
  
  savechangesbut=0;
  Blynk.virtualWrite(V9, savechangesbut);
  

  //synclights();
}


void resetlights()
{
  Blynk.virtualWrite(V1, 1);
  Blynk.virtualWrite(V2, 0);
  Blynk.virtualWrite(V3, 0);
  Blynk.virtualWrite(V4, 0);
  Blynk.virtualWrite(V5, 0);
  Blynk.virtualWrite(V6, 0);
  Blynk.virtualWrite(V7, 0);
  Blynk.virtualWrite(V8, 0);
  lightedit=0;
  
  resetchangesbut=0;
  Blynk.virtualWrite(V10, resetchangesbut);
}

//======================================================================================
//=============================================================  POWER BANK ON/OFF ===== 
//======================================================================================

//                                                     Need to check if LOW is on or off or vice versa

void pwr1on()            //=====  UV === 
{
  digitalWrite(22, HIGH);
  //led30.on();
  pwr1=1;
  EEPROM.write(60,pwr1);
  Blynk.virtualWrite(V30, 1);
  Blynk.virtualWrite(V40, 1);
}

void pwr1off()
{
  digitalWrite(22, LOW);
  //led30.off();
  pwr1=0;
  EEPROM.write(60,pwr1);
  Blynk.virtualWrite(V30, 0);
  Blynk.virtualWrite(V40, 0);
}

void pwr2on()            //=====  ATO === 
{
  digitalWrite(23, HIGH);
  //led31.on();
  pwr2=1;
  EEPROM.write(61,pwr2);
  Blynk.virtualWrite(V31, 1);
  Blynk.virtualWrite(V41, 1);
}

void pwr2off()
{
  digitalWrite(23, LOW);
  //led31.off();
  pwr2=0;
  EEPROM.write(61,pwr2);
  Blynk.virtualWrite(V31, 0);
  Blynk.virtualWrite(V41, 0);
}

void pwr3on()            //=====  REACTOR PUMP === 
{
  digitalWrite(24, HIGH);
  //led32.on();
  pwr3=1;
  EEPROM.write(62,pwr3);
  Blynk.virtualWrite(V32, 1);
  Blynk.virtualWrite(V42, 1);
}

void pwr3off()
{
  digitalWrite(24, LOW);
  //led32.off();
  pwr3=0;
  EEPROM.write(62,pwr3);
  Blynk.virtualWrite(V32, 0);
  Blynk.virtualWrite(V42, 0);
}

void pwr4on()            //=====  DOSER PUMP POWER === 
{
  digitalWrite(25, HIGH);
  //led33.on();
  pwr4=1;
  EEPROM.write(63,pwr4);
  Blynk.virtualWrite(V33, 1);
  Blynk.virtualWrite(V43, 1);
}

void pwr4off()
{
  digitalWrite(25, LOW);
  //led33.off();
  pwr4=0;
  EEPROM.write(63,pwr4);
  Blynk.virtualWrite(V33, 0);
  Blynk.virtualWrite(V43, 0);
}

void pwr5on()            //=====  SUMP LIGHT === 
{
  digitalWrite(26, HIGH);
  //led34.on();
  pwr5=1;
  EEPROM.write(64,pwr5);
  Blynk.virtualWrite(V34, 1);
  Blynk.virtualWrite(V44, 1);
}

void pwr5off()
{
  digitalWrite(26, LOW);
  //led34.off();
  pwr5=0;
  EEPROM.write(64,pwr5);
  Blynk.virtualWrite(V34, 0);
  Blynk.virtualWrite(V44, 0);
}

void pwr6on()            //=====  COOLING FANS === 
{
  digitalWrite(27, HIGH);
  //led35.on();
  pwr6=1;
  EEPROM.write(65,pwr6);
  Blynk.virtualWrite(V35, 1);
  Blynk.virtualWrite(V45, 1);
}

void pwr6off()
{
  digitalWrite(27, LOW);
  //led35.off();
  pwr6=0;
  EEPROM.write(65,pwr6);
  Blynk.virtualWrite(V35, 0);
  Blynk.virtualWrite(V45, 0);
}

void pwr7on()            //=====  HEATER === 
{
  digitalWrite(28, HIGH);
  //led36.on();
  pwr7=1;
  EEPROM.write(66,pwr7);
  Blynk.virtualWrite(V36, 1);
  Blynk.virtualWrite(V46, 1);
}

void pwr7off()
{
  digitalWrite(28, LOW);
  //led36.off();
  pwr7=0;
  EEPROM.write(66,pwr7);
  Blynk.virtualWrite(V36, 0);
  Blynk.virtualWrite(V46, 0);
}

void pwr8on()            //=====  SKIMMER === 
{
  digitalWrite(29, HIGH);
  //led37.on();
  pwr8=1;
  EEPROM.write(67,pwr8);
  Blynk.virtualWrite(V37, 1);
  Blynk.virtualWrite(V47, 1); 
}

void pwr8off()
{
  digitalWrite(29, LOW);
  //led37.off();
  pwr8=0;
  EEPROM.write(67,pwr8);
  Blynk.virtualWrite(V37, 0);
  Blynk.virtualWrite(V47, 0);
}

void pwr9on()            //=====  POWER HEADS  === 
{
  digitalWrite(30, HIGH);
  //led38.on();
  pwr9=1;
  EEPROM.write(68,pwr9);
  Blynk.virtualWrite(V38, 1);
  Blynk.virtualWrite(V48, 1);
}

void pwr9off()
{
  digitalWrite(30, LOW);
  //led38.off();
  pwr9=0;
  EEPROM.write(68,pwr9);
  Blynk.virtualWrite(V38, 0);
  Blynk.virtualWrite(V48, 0);
}

void pwr10on()            //=====  RETURN PUMP === 
{
  digitalWrite(31, HIGH);
  //led39.on();
  pwr10=1;
  EEPROM.write(69,pwr10);
  Blynk.virtualWrite(V39, 1);
  Blynk.virtualWrite(V49, 1);
}

void pwr10off()
{
  digitalWrite(31, LOW);
  //led39.off();
  pwr10=0;
  EEPROM.write(69,pwr10);
  Blynk.virtualWrite(V39, 0);
  Blynk.virtualWrite(V49, 0);
}
  
  
void lunarFull()                                
{
  currentLightMode=0;       // 0=lunar, 1=low sun, 2=mid sun, 3=high sun, 4=transition, 5=unknown
  fadeInProgress = 0;
  EEPROM.write(46,currentLightMode);                       // Save currentlightmode to EEPROM
  
  currentColor.ch1 = lunar.ch1;   
  currentColor.ch2 = lunar.ch2;
  currentColor.ch3 = lunar.ch3;
  currentColor.ch4 = lunar.ch4;
  currentColor.ch5 = lunar.ch5;
  currentColor.ch6 = lunar.ch6;
  currentColor.ch7 = lunar.ch7;
  
  analogWrite(led1, currentColor.ch1);      
  analogWrite(led2, currentColor.ch2);       
  analogWrite(led3, currentColor.ch3);      
  analogWrite(led4, currentColor.ch4); 
  analogWrite(led5, currentColor.ch5);       
  analogWrite(led6, currentColor.ch6);      
  analogWrite(led7, currentColor.ch7); 
}

void lowsunFull()
{
  currentLightMode=1;       // 0=lunar, 1=low sun, 2=mid sun, 3=high sun, 4=transition, 5=unknown
  transitionMode=0;              // 0- none, 1- l-ls, 2- ls-ms, 3- ms-hs, 4- hs-ms, 5- ms-ls, 6- ls-l
  fadeInProgress = 0;
  EEPROM.write(46,currentLightMode);                       // Save currentlightmode to EEPROM  
  
  currentColor.ch1 = lowsun.ch1;   
  currentColor.ch2 = lowsun.ch2;
  currentColor.ch3 = lowsun.ch3;
  currentColor.ch4 = lowsun.ch4;
  currentColor.ch5 = lowsun.ch5;
  currentColor.ch6 = lowsun.ch6;
  currentColor.ch7 = lowsun.ch7;
  
  analogWrite(led1, currentColor.ch1);      
  analogWrite(led2, currentColor.ch2);       
  analogWrite(led3, currentColor.ch3);      
  analogWrite(led4, currentColor.ch4); 
  analogWrite(led5, currentColor.ch5);       
  analogWrite(led6, currentColor.ch6);      
  analogWrite(led7, currentColor.ch7); 
}

void midsunFull()
{
  currentLightMode=2;       // 0=lunar, 1=low sun, 2=mid sun, 3=high sun, 4=transition, 5=unknown
  transitionMode=0;              // 0- none, 1- l-ls, 2- ls-ms, 3- ms-hs, 4- hs-ms, 5- ms-ls, 6- ls-l
  fadeInProgress = 0;
  EEPROM.write(46,currentLightMode);                       // Save currentlightmode to EEPROM
  
  currentColor.ch1 = midsun.ch1;   
  currentColor.ch2 = midsun.ch2;
  currentColor.ch3 = midsun.ch3;
  currentColor.ch4 = midsun.ch4;
  currentColor.ch5 = midsun.ch5;
  currentColor.ch6 = midsun.ch6;
  currentColor.ch7 = midsun.ch7;
  
  analogWrite(led1, currentColor.ch1);      
  analogWrite(led2, currentColor.ch2);       
  analogWrite(led3, currentColor.ch3);      
  analogWrite(led4, currentColor.ch4); 
  analogWrite(led5, currentColor.ch5);       
  analogWrite(led6, currentColor.ch6);      
  analogWrite(led7, currentColor.ch7); 
  
}

void highsunFull()
{
  currentLightMode=3;       // 0=lunar, 1=low sun, 2=mid sun, 3=high sun, 4=transition, 5=unknown
  transitionMode=0;              // 0- none, 1- l-ls, 2- ls-ms, 3- ms-hs, 4- hs-ms, 5- ms-ls, 6- ls-l
  fadeInProgress = 0;
  EEPROM.write(46,currentLightMode);                     // Save currentlightmode to EEPROM

  currentColor.ch1 = highsun.ch1;   
  currentColor.ch2 = highsun.ch2;
  currentColor.ch3 = highsun.ch3;
  currentColor.ch4 = highsun.ch4;
  currentColor.ch5 = highsun.ch5;
  currentColor.ch6 = highsun.ch6;
  currentColor.ch7 = highsun.ch7;
  
  analogWrite(led1, currentColor.ch1);      
  analogWrite(led2, currentColor.ch2);       
  analogWrite(led3, currentColor.ch3);      
  analogWrite(led4, currentColor.ch4); 
  analogWrite(led5, currentColor.ch5);       
  analogWrite(led6, currentColor.ch6);      
  analogWrite(led7, currentColor.ch7); 
}  
  
  
//======================================================================================
//======================================================================  STORM  ======= 
//======================================================================================


void stormbegin()
{
  stormstatus = 1;     // 0- Off  1- fade down  2- storm on  3- fade up 
 
  storm1 = currentColor.ch1;
  storm2 = currentColor.ch2;
  storm3 = currentColor.ch3;
  storm4 = currentColor.ch4;
  storm5 = currentColor.ch5;
  storm6 = currentColor.ch6;
  storm7 = currentColor.ch7;
  
  while ((storm1>0) || (storm2>0) || (storm3>0) || (storm4>0) || (storm5>0) || (storm6>0) || (storm7>5))
  {
if (storm1>0)
{
storm1--;
}
if (storm2>0)
{
storm2--;
}
if (storm3>0)
{
storm3--;
}
if (storm4>0)
{
storm4--;
}
if (storm5>0)
{
storm5--;
}
if (storm6>0)
{
storm6--;
}
if (storm7>0)
{
storm7--;
}

analogWrite(led1, storm1);
analogWrite(led2, storm2);
analogWrite(led3, storm3);
analogWrite(led4, storm4);
analogWrite(led5, storm5);
analogWrite(led6, storm6);
analogWrite(led7, storm7);
  }
  stormstatus = 2;   // 0- Off  1- fade down  2- storm on  3- fade up
  stormMillis = millis();
}


void uploadToBlynk()            // LOAD VALUES FROM EEPROM     cdo i need this??
{/*
  Blynk.virtualWrite(V11, lunartolowsunStartH);
  Blynk.virtualWrite(V11, lowsuntomidsunStartH);
  Blynk.virtualWrite(V12, midsuntohighsunStartH);
  Blynk.virtualWrite(V13, highsuntomidsunStartH);
  Blynk.virtualWrite(V14, midsuntolowsunStartH);
  Blynk.virtualWrite(V15, lowsuntolunarStartH);
  
  Blynk.virtualWrite(V16, lunartolowsunStartM);
  Blynk.virtualWrite(V17, lowsuntomidsunStartM);
  Blynk.virtualWrite(V18, midsuntohighsunStartM);
  Blynk.virtualWrite(V19, highsuntomidsunStartM);
  Blynk.virtualWrite(V10, midsuntolowsunStartM);
  Blynk.virtualWrite(V21, lowsuntolunarStartM);  
  
  Blynk.virtualWrite(V12, globalfadedurationM);         // fade time in minutes to V12 labled display
  */
}


void startup()
{
  lunar.ch1 = EEPROM.read(1);
  lunar.ch2 = EEPROM.read(2);
  lunar.ch3 = EEPROM.read(3);
  lunar.ch4 = EEPROM.read(4);
  lunar.ch5 = EEPROM.read(5);
  lunar.ch6 = EEPROM.read(6);
  lunar.ch7 = EEPROM.read(7);
  lowsun.ch1 = EEPROM.read(8);
  lowsun.ch2 = EEPROM.read(9);
  lowsun.ch3 = EEPROM.read(10);
  lowsun.ch4 = EEPROM.read(11);
  lowsun.ch5 = EEPROM.read(12);
  lowsun.ch6 = EEPROM.read(13);
  lowsun.ch7 = EEPROM.read(14);
  midsun.ch1 = EEPROM.read(15);
  midsun.ch2 = EEPROM.read(16);
  midsun.ch3 = EEPROM.read(17);
  midsun.ch4 = EEPROM.read(18);
  midsun.ch5 = EEPROM.read(19);
  midsun.ch6 = EEPROM.read(20);
  midsun.ch7 = EEPROM.read(21);
  highsun.ch1 = EEPROM.read(22);
  highsun.ch2 = EEPROM.read(23);
  highsun.ch3 = EEPROM.read(24);
  highsun.ch4 = EEPROM.read(25);
  highsun.ch5 = EEPROM.read(26);
  highsun.ch6 = EEPROM.read(27);
  highsun.ch7 = EEPROM.read(28);
 
  lunartolowsunStartH = EEPROM.read(30);
  lowsuntomidsunStartH = EEPROM.read(31);
  midsuntohighsunStartH = EEPROM.read(32);
  highsuntomidsunStartH = EEPROM.read(33);
  midsuntolowsunStartH = EEPROM.read(34);
  lowsuntolunarStartH = EEPROM.read(35);
  lunartolowsunStartM = EEPROM.read(36);
  lowsuntomidsunStartM = EEPROM.read(37);
  midsuntohighsunStartM = EEPROM.read(38);
  highsuntomidsunStartM = EEPROM.read(39);
  midsuntolowsunStartM = EEPROM.read(40);
  lowsuntolunarStartM = EEPROM.read(41);
  globalfadedurationM = EEPROM.read(42);
  
  currentLightMode = EEPROM.read(46);             // Retrieve last known currentLightMode
  transitionMode = EEPROM.read(47);               // Retrieve last known transitionMode
  lightedit = EEPROM.read(48);
  fadeInProgress = EEPROM.read(49);
  
  currentColor.ch1 = EEPROM.read(51);              // Retrieve last known color values
  currentColor.ch2 = EEPROM.read(52);
  currentColor.ch3 = EEPROM.read(53);
  currentColor.ch4 = EEPROM.read(54);
  currentColor.ch5 = EEPROM.read(55);
  currentColor.ch6 = EEPROM.read(56);
  currentColor.ch7 = EEPROM.read(57);
  
  globalfadedurationS = globalfadedurationM * 60;     // Calculate fade seconds
  globalfadedurationMS = globalfadedurationS * 1000;  // Calculate fade miliseconds
  
  pwr1 = EEPROM.read(60);        // Read power status
  pwr2 = EEPROM.read(61);
  pwr3 = EEPROM.read(62);
  pwr4 = EEPROM.read(63);
  pwr5 = EEPROM.read(64);
  pwr6 = EEPROM.read(65);
  pwr7 = EEPROM.read(66);
  pwr8 = EEPROM.read(67);
  pwr9 = EEPROM.read(68);
  pwr10 = EEPROM.read(69);
  
  
  synclights();
  syncPower();
}
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////                                             ///////////////////////
/////////////////////           V  O  I  D    L  O  O  P          ///////////////////////
/////////////////////                                             ///////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

void loop()
{
  Blynk.run();
  
  if (millis() - tempMillis > 5000)
  {
sensors.requestTemperatures();  

tempC01 = sensors.getTempC(Probe01);      // Tank 
tempC02 = sensors.getTempC(Probe02);      // Sump
tempC03 = sensors.getTempC(Probe03);      // LEDS
printtemp1();
printtemp2();
printtemp3();
tempMillis = millis();
  }
}

Which MCU are you using?

Android / iOS?

Android.

And the MCU?

Sorry. Am using USB connection to a PC on an Arduino Mega board.

How much memory do you have when you compile the sketch and have you checked the memory whilst the sketch is running?

Have you tried a basic Mega with USB sketch to confirm it will run “forever” which would then suggest you have a bad sketch.

You would then need to check small sections of your code to see if you have any bugs.

Ok so i cut the code right down so the only thing running is the light programming section. So basically 4 different menus (lunar, low sun, mid sun and high sun) and 7 channels of lights for different colour LEDs. The save button saves them and the finished/reset button should return the sliders to 0 along with the menu on V1.

It works 80% of the time but now and again it will just stop and not do anything. I don’t know if its not uploading the button pushes to Blynk, or if there is an intimitant problem with my internet connection and the Arduino is ‘missing’ the commands that Blynk is sending it. But the after it ‘misses’ it once, it won’t work again until I disconnect and reconnect it.

The sketch is only taking up 5% of available memory.

The stripped down code is below. Can anyone see why there might be this halting error? Could it be flooding Blynk? Also, if the Arduino is missing the new commands, shouldn’t it be constantly checking the Blynk server for any changes and apply them when it reconnected?

#define BLYNK_PRINT Serial1
//#define ONE_WIRE_BUS_PIN 44   // TEMP SENSORS
//#define BETWEEN 2579          // STORM
//#define DURATION 100          // STORM
//#define TIMES 7               // STORM

#include <BlynkSimpleStream.h>
#include <Wire.h>
#include <Time.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DS1307RTC.h>
#include <EEPROM.h>

//OneWire oneWire(ONE_WIRE_BUS_PIN);
//DallasTemperature sensors(&oneWire);

//DeviceAddress Probe01 = { 0x28, 0x5E, 0x72, 0xC5, 0x06, 0x00, 0x00, 0xA4 }; 
//DeviceAddress Probe02 = { 0x28, 0x9F, 0x49, 0x2F, 0x06, 0x00, 0x00, 0xAA };
//DeviceAddress Probe03 = { 0x28, 0x7A, 0xFB, 0xC4, 0x06, 0x00, 0x00, 0x79 };

char auth[] = "65b59e***************************5f4166";

//-----Temp Sensors
float tempC01, tempC02, tempC03;
unsigned int tempMillis = millis();

//----- Power Plugs
int pwr1, pwr2, pwr3, pwr4, pwr5, pwr6, pwr7, pwr8, pwr9, pwr10;
int pwrbut1, pwrbut2, pwrbut3, pwrbut4, pwrbut5, pwrbut6, pwrbut7, pwrbut8, pwrbut9, pwrbut10;
int sensorVal1, sensorVal2;
int sump1ontime, sump2ontime, sump1onH, sump1onM, sump2onH, sump2onM, sump1offH, sump1offM, sump2offH, sump2offM;
int sumptimer1active, sumptimer2active;

//----- Storm
unsigned long lastTime = 0;
int waitTime = 0;
int mixer, srumble, brumble, stormstatus=0, stormbutton;
unsigned long stormMillis=0;
unsigned int storm1, storm2, storm3, storm4, storm5, storm6, storm7;

//----- Lighting
int savechangesbut;    // button used to save new light levels
int resetchangesbut;    // button used to reset lighting after editing
int resetarduinobut;   // button used to reset the whooe Arduino

int lightedit=0;       // editing lights? // 0-NOT EDITING  1-EDIT LUNAR  2-EDIT LOWSUN  3-EDIT MIDSUN  4-EDIT HIGHSUN

int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int led6 = 7;
int led7 = 8;

long lunartolowsunSec, lowsuntomidsunSec, midsuntohighsunSec, highsuntomidsunSec, midsuntolowsunSec, lowsuntolunarSec;
int globalfadedurationM;
unsigned long globalfadedurationS, globalfadedurationMS;

int lunartolowsunStartH, lunartolowsunStartM;            // Start fade times
int lowsuntomidsunStartH, lowsuntomidsunStartM;
int midsuntohighsunStartH, midsuntohighsunStartM;
int highsuntomidsunStartH, highsuntomidsunStartM;
int midsuntolowsunStartH, midsuntolowsunStartM;
int lowsuntolunarStartH, lowsuntolunarStartM;

int fadeInProgress;   // 0=false / 1=yes
int currentLightMode, transitionMode;

unsigned long ch1LightMillis, ch2LightMillis, ch3LightMillis, ch4LightMillis, ch5LightMillis, ch6LightMillis, ch7LightMillis;
int ch1timeincr, ch2timeincr, ch3timeincr, ch4timeincr, ch5timeincr, ch6timeincr, ch7timeincr;
int colorDif1, colorDif2, colorDif3, colorDif4, colorDif5, colorDif6, colorDif7;
unsigned long savelightsMillis = 0;
int butLunar, butLowsun, butMidsun, butHighsun;


struct LEDS // for storing light intensity values
{
  int ch1;
  int ch2;
  int ch3;
  int ch4;
  int ch5;
  int ch6;
  int ch7;
};

typedef struct LEDS LightColor;

LightColor currentColor = {
  0,0,0,0,0,0,0};               // The current color of the light (used for fading)
LightColor lastColor = {
  0,0,0,0,0,0,0};               // The previous color of the light (used for fading)
LightColor targetColor = {
  0,0,0,0,0,0,0};               // The target color of the light (used for fading)
  
LightColor lunar = {            // Lunar colours
  0,0,0,0,0,0,0};             
LightColor lowsun = {           // Lowsun colours
  0,0,0,0,0,0,0};              
LightColor midsun = {           // Midsun colours
  0,0,0,0,0,0,0};               
LightColor highsun = {          // Highsun colours
  0,0,0,0,0,0,0};       
LightColor edit = {             // Used to edit colours
  0,0,0,0,0,0,0};

//----- Feeding
int feeding;

//----- RTC
unsigned long time;
tmElements_t tm; 

//----- Dosing
long dose1startTime, dose2startTime, dose3startTime, dose4startTime;


/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////                                             ///////////////////////
/////////////////////      B  L  Y  N  K    I  N  P  U  T  S      ///////////////////////
/////////////////////                                             ///////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////


BLYNK_WRITE(V1) 
{
  switch (param.asInt())
  {
    case 1: 
    
      lightedit=0;    // 0-NOT EDITING  1-EDIT LUNAR  2-EDIT LOWSUN  3-EDIT MIDSUN  4-EDIT HIGHSUN
      edit.ch1=highsun.ch1;
      edit.ch2=highsun.ch2;
      edit.ch3=highsun.ch3;
      edit.ch4=highsun.ch4;
      edit.ch5=highsun.ch5;
      edit.ch6=highsun.ch6;
      edit.ch7=highsun.ch7;
      Blynk.virtualWrite(V2, edit.ch1);
      Blynk.virtualWrite(V3, edit.ch2);
      Blynk.virtualWrite(V4, edit.ch3);
      Blynk.virtualWrite(V5, edit.ch4);
      Blynk.virtualWrite(V6, edit.ch5);
      Blynk.virtualWrite(V7, edit.ch6);
      Blynk.virtualWrite(V8, edit.ch7);
      showeditleds();
      break;
      
    case 2: // E  D  I  T    L  U  N  A  R    L  I  G  H  T  I  N  G
    
      lightedit=1;    // 0-NOT EDITING  1-EDIT LUNAR  2-EDIT LOWSUN  3-EDIT MIDSUN  4-EDIT HIGHSUN
      edit.ch1=lunar.ch1;
      edit.ch2=lunar.ch2;
      edit.ch3=lunar.ch3;
      edit.ch4=lunar.ch4;
      edit.ch5=lunar.ch5;
      edit.ch6=lunar.ch6;
      edit.ch7=lunar.ch7;
      Blynk.virtualWrite(V2, edit.ch1);
      Blynk.virtualWrite(V3, edit.ch2);
      Blynk.virtualWrite(V4, edit.ch3);
      Blynk.virtualWrite(V5, edit.ch4);
      Blynk.virtualWrite(V6, edit.ch5);
      Blynk.virtualWrite(V7, edit.ch6);
      Blynk.virtualWrite(V8, edit.ch7);
      showeditleds();
      break;
      
    case 3: // E  D  I  T    L  O  W    S  U  N    L  I  G  H  T  I  N  G
    
      lightedit=2;    // 0-NOT EDITING  1-EDIT LUNAR  2-EDIT LOWSUN  3-EDIT MIDSUN  4-EDIT HIGHSUN
      edit.ch1=lowsun.ch1;
      edit.ch2=lowsun.ch2;
      edit.ch3=lowsun.ch3;
      edit.ch4=lowsun.ch4;
      edit.ch5=lowsun.ch5;
      edit.ch6=lowsun.ch6;
      edit.ch7=lowsun.ch7;
      Blynk.virtualWrite(V2, edit.ch1);
      Blynk.virtualWrite(V3, edit.ch2);
      Blynk.virtualWrite(V4, edit.ch3);
      Blynk.virtualWrite(V5, edit.ch4);
      Blynk.virtualWrite(V6, edit.ch5);
      Blynk.virtualWrite(V7, edit.ch6);
      Blynk.virtualWrite(V8, edit.ch7);
      showeditleds();
      break;
      
    case 4: // E  D  I  T    M  I  D    S  U  N    L  I  G  H  T  I  N  G
    
      lightedit=3;    // 0-NOT EDITING  1-EDIT LUNAR  2-EDIT LOWSUN  3-EDIT MIDSUN  4-EDIT HIGHSUN
      edit.ch1=midsun.ch1;
      edit.ch2=midsun.ch2;
      edit.ch3=midsun.ch3;
      edit.ch4=midsun.ch4;
      edit.ch5=midsun.ch5;
      edit.ch6=midsun.ch6;
      edit.ch7=midsun.ch7;
      Blynk.virtualWrite(V2, edit.ch1);
      Blynk.virtualWrite(V3, edit.ch2);
      Blynk.virtualWrite(V4, edit.ch3);
      Blynk.virtualWrite(V5, edit.ch4);
      Blynk.virtualWrite(V6, edit.ch5);
      Blynk.virtualWrite(V7, edit.ch6);
      Blynk.virtualWrite(V8, edit.ch7);
      showeditleds();
      break;
      
    case 5: // E  D  I  T    H  I  G  H    S  U  N    L  I  G  H  T  I  N  G
    
      lightedit=4;    // 0-NOT EDITING  1-EDIT LUNAR  2-EDIT LOWSUN  3-EDIT MIDSUN  4-EDIT HIGHSUN
      edit.ch1=highsun.ch1;
      edit.ch2=highsun.ch2;
      edit.ch3=highsun.ch3;
      edit.ch4=highsun.ch4;
      edit.ch5=highsun.ch5;
      edit.ch6=highsun.ch6;
      edit.ch7=highsun.ch7;
      Blynk.virtualWrite(V2, edit.ch1);
      Blynk.virtualWrite(V3, edit.ch2);
      Blynk.virtualWrite(V4, edit.ch3);
      Blynk.virtualWrite(V5, edit.ch4);
      Blynk.virtualWrite(V6, edit.ch5);
      Blynk.virtualWrite(V7, edit.ch6);
      Blynk.virtualWrite(V8, edit.ch7);
      showeditleds();
      break;
  }
}


BLYNK_WRITE(V2)
{
  edit.ch1 = param.asInt();
  analogWrite(led1, edit.ch1);
}

BLYNK_WRITE(V3)
{
  edit.ch2 = param.asInt();
  analogWrite(led2, edit.ch2);
}

BLYNK_WRITE(V4)
{
  edit.ch3 = param.asInt(); 
  analogWrite(led3, edit.ch3);
}

BLYNK_WRITE(V5)
{
  edit.ch4 = param.asInt(); 
  analogWrite(led4, edit.ch4);
}

BLYNK_WRITE(V6)
{
  edit.ch5 = param.asInt(); 
  analogWrite(led5, edit.ch5);
}

BLYNK_WRITE(V7)
{
  edit.ch6 = param.asInt(); 
  analogWrite(led6, edit.ch6);
}

BLYNK_WRITE(V8)
{
  edit.ch7 = param.asInt(); 
  analogWrite(led7, edit.ch7);
}



BLYNK_WRITE(V9)    // Save Lighting Changes
{
  savechangesbut = param.asInt(); 
  
  if (savechangesbut == 1)
    {
      savevalues();
    }
}

BLYNK_WRITE(V10)    // Save Lighting Changes
{
  resetchangesbut = param.asInt(); 
  
  if (resetchangesbut==1)
    {
      resetlights();
    }
}





void setup() 
{
  Serial1.begin(9600);
  Serial.begin(9600);
 
  //sensors.begin();
  //sensors.setResolution(Probe01, 10);
  //sensors.setResolution(Probe02, 10);
  //sensors.setResolution(Probe03, 10);
  
  pinMode(led1, OUTPUT);  // LED CH1
  pinMode(led2, OUTPUT);  // LED CH2
  pinMode(led3, OUTPUT);  // LED CH3
  pinMode(led4, OUTPUT);  // LED CH4
  pinMode(led5, OUTPUT);  // LED CH5
  pinMode(led6, OUTPUT);  // LED CH6
  pinMode(led7, OUTPUT);  // LED CH7
  
  pinMode(22, OUTPUT); // POWER SECTION
  pinMode(23, OUTPUT); // ----"---"----
  pinMode(24, OUTPUT); // ----"---"----
  pinMode(25, OUTPUT); // ----"---"----
  pinMode(26, OUTPUT); // ----"---"----
  pinMode(27, OUTPUT); // ----"---"----
  pinMode(28, OUTPUT); // ----"---"----
  pinMode(29, OUTPUT); // ----"---"----
  pinMode(30, OUTPUT); // ----"---"----
  pinMode(31, OUTPUT); // ----"---"----
  
  pinMode(51, INPUT_PULLUP);    // Float switch 1
  pinMode(52, INPUT_PULLUP);    // Float switch 2
  
  //uploadToBlynk();
  //startup();
  
  Blynk.begin(Serial, auth);
  
  //resetarduinobut=0;
  //Blynk.virtualWrite(V27, resetarduinobut);
}


void savevalues()        // SAVE VALUES TO EEPROM
{
  if (lightedit==1)    // 0-NOT EDITING  1-EDIT LUNAR  2-EDIT LOWSUN  3-EDIT MIDSUN  3-EDIT HIGHSUN  4-EDIT HIGHSUN
  {
    lunar.ch1=edit.ch1;
    lunar.ch2=edit.ch2;
    lunar.ch3=edit.ch3;
    lunar.ch4=edit.ch4;
    lunar.ch5=edit.ch5;
    lunar.ch6=edit.ch6;
    lunar.ch7=edit.ch7;
    EEPROM.write(1, lunar.ch1);
    EEPROM.write(2, lunar.ch2);
    EEPROM.write(3, lunar.ch3);
    EEPROM.write(4, lunar.ch4);
    EEPROM.write(5, lunar.ch5);
    EEPROM.write(6, lunar.ch6);
    EEPROM.write(7, lunar.ch7);
  }
  else if (lightedit==2)
  {
    lowsun.ch1=edit.ch1;
    lowsun.ch2=edit.ch2;
    lowsun.ch3=edit.ch3;
    lowsun.ch4=edit.ch4;
    lowsun.ch5=edit.ch5;
    lowsun.ch6=edit.ch6;
    lowsun.ch7=edit.ch7;
    EEPROM.write(8, lowsun.ch1);
    EEPROM.write(9, lowsun.ch2);
    EEPROM.write(10, lowsun.ch3);
    EEPROM.write(11, lowsun.ch4);
    EEPROM.write(12, lowsun.ch5);
    EEPROM.write(13, lowsun.ch6);
    EEPROM.write(14, lowsun.ch7);
  }
  else if (lightedit==3)
  {
    midsun.ch1=edit.ch1;
    midsun.ch2=edit.ch2;
    midsun.ch3=edit.ch3;
    midsun.ch4=edit.ch4;
    midsun.ch5=edit.ch5;
    midsun.ch6=edit.ch6;
    midsun.ch7=edit.ch7;
    EEPROM.write(15, midsun.ch1);
    EEPROM.write(16, midsun.ch2);
    EEPROM.write(17, midsun.ch3);
    EEPROM.write(18, midsun.ch4);
    EEPROM.write(19, midsun.ch5);
    EEPROM.write(20, midsun.ch6);
    EEPROM.write(21, midsun.ch7);
  }
  else if (lightedit==4)
  {
    highsun.ch1=edit.ch1;
    highsun.ch2=edit.ch2;
    highsun.ch3=edit.ch3;
    highsun.ch4=edit.ch4;
    highsun.ch5=edit.ch5;
    highsun.ch6=edit.ch6;
    highsun.ch7=edit.ch7;
    EEPROM.write(22, highsun.ch1);
    EEPROM.write(23, highsun.ch2);
    EEPROM.write(24, highsun.ch3);
    EEPROM.write(25, highsun.ch4);
    EEPROM.write(26, highsun.ch5);
    EEPROM.write(27, highsun.ch6);
    EEPROM.write(28, highsun.ch7);
  }
  
  analogWrite(led1, 0);
  analogWrite(led2, 0);
  analogWrite(led3, 0);
  analogWrite(led4, 0);
  analogWrite(led5, 0);
  analogWrite(led6, 0);
  analogWrite(led7, 0);
  delay(250);
  analogWrite(led2, 200);
  delay(250);
  analogWrite(led2, 0);
  delay(250);
  analogWrite(led2, 200);
  delay(250);
  analogWrite(led2, 0);
  delay(250);
 
  analogWrite(led1, edit.ch1);
  analogWrite(led2, edit.ch2);
  analogWrite(led3, edit.ch3);
  analogWrite(led4, edit.ch4);
  analogWrite(led5, edit.ch5);
  analogWrite(led6, edit.ch6);
  analogWrite(led7, edit.ch7);
  
  savechangesbut=0;
  Blynk.virtualWrite(V9, savechangesbut);
  

  //synclights();
}

void showeditleds()
{
  analogWrite(led1, edit.ch1);
  analogWrite(led2, edit.ch2);
  analogWrite(led3, edit.ch3);
  analogWrite(led4, edit.ch4);
  analogWrite(led5, edit.ch5);
  analogWrite(led6, edit.ch6);
  analogWrite(led7, edit.ch7);
}

void resetlights()
{
  Blynk.virtualWrite(V1, 1);
  Blynk.virtualWrite(V2, 0);
  Blynk.virtualWrite(V3, 0);
  Blynk.virtualWrite(V4, 0);
  Blynk.virtualWrite(V5, 0);
  Blynk.virtualWrite(V6, 0);
  Blynk.virtualWrite(V7, 0);
  Blynk.virtualWrite(V8, 0);
  lightedit=0;
  
  resetchangesbut=0;
  Blynk.virtualWrite(V10, resetchangesbut);
}

/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////                                             ///////////////////////
/////////////////////           V  O  I  D    L  O  O  P          ///////////////////////
/////////////////////                                             ///////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

void loop()
{
  Blynk.run();
  
  /*
  if (millis() - tempMillis > 5000)
  {
    sensors.requestTemperatures();  
    
    tempC01 = sensors.getTempC(Probe01);      // Tank 
    tempC02 = sensors.getTempC(Probe02);      // Sump
    tempC03 = sensors.getTempC(Probe03);      // LEDS
    printtemp1();
    printtemp2();
    printtemp3();
    tempMillis = millis();
  }
  */
}

I also have a large (10% program space) testbed project running on a MEGA via a USB link. It will also tend to “stall” at random times. Sometimes I need to stop and restart the blynk-ser.bat file (which reboots the MEGA), but usually I just start pressing buttons on the app and eventually (in a few seconds) it just stutters awake and catches up as if nothing is wrong.

I personally think it has something to do with the USB link, as occasionally I see only one-way communication. E.g. Uptime counter and Hardware → App displays are working, but App → Hardware commands are not responding… until again like above, a few seconds of button/slider poking and everything just starts working as if nothing was wrong.

I use another TTL-USB adapter on the MEGA’s Serial1 to allow debugging on a serial terminal monitor; Blynk’s Debug at times, but usually just #define BLYNK_PRINT Serial1 and my own print markers to indicate function activities.

Sometimes I see flooding warnings on some heavy action functions, but usually not. I have Local Server and have fiddled with various adjustments, but none of them have seemed to affect the issue one way or another.

Try something like that debugging for your own sketch.

That is an option you can try to program in, via a combo of Blynk’s connection management and sync commands.

Right I’m getting fed up with this crap. I have spent months writing code around this Blynk app and paid out for bluetooth modules of which don’t connect to the app properly. Ive NOW paid for the full package of credits to buy widgets and re-written the code for USB connection and now that don’t work either because it keeps stalling. Can you please tell me if there actually is a way to connect this app to an Arduino that actually works because I am struggling to find one!

I have just spent another WHOLE day re writing code and testing to try and get this thing to not stall and it does it at random times for no apparent reason. Its not good enough!!!

WHY BLYNK CREATORS? WHY?

Please calm down.
Why do you use USB in nowadays? Couldn’t you just buy a wi-fi, 3g, ethernet shield and save own time? It will cost you few bucks. USB support was introduced just for the simple demos, so users can quickly check how Blynk works. And it was done many years ago when Wi-Fi MCUs were expensive. Are you really going to use USB in your real project?

Woops, sorry if I gave the impression that USB was useless… I said I “suspect” but it could be simply my own setup as well, as I am running many USB connections (2-Blynkified Arduinos and 1-TTL are running 24/7) plus the usual Mouse/KB/UPS/Cam etc.

That said, aside from the occasional “stall” Blynk is working fine. However you do have some heavier bulk BlynkWrite() processes running, so as @Dmitriy mentioned, alternate network type connections might be better suited, and perhaps you might want to consider a Local Server… A) less lag may help and B) less “expense” as you control your energy allotment (not that I found Blynk “expensive”… I have seen daily coffee expenditure in some people that could run my Blynk activity for a month ;))

Hi Dimitry. I apologise but it is so frustrating I have rebuilt my project twice now because I was told to use Bluetooth in the beginning which won’t work properly, then tried USB and that isn’t working either. From what I have seen the wifi boards aren’t cheap, certainly not a few bucks. I was told the bluetooth and BLE would work and it does’t.

What do you recommend as the best and most reliable to way to connect?

It weren’t what you said mate, I just stripped everything out and it was still stalling. Nothing else is connected to it, just the arduino and its still doing it. What is annoying is that I have paid out for two different types of bluetooth modules and they don’t even work. I’m loosing weeks and months of time trying to get this thing working and getting absolutely nowhere.

What is this local servers and how does it work? The arduino will be plugged into a laptop 24/7 so can int be run from that?

“Local” Server is offered by Blynk for install on any hardware that can run Java - so yes, your Laptop could become a Local Server for your USB tethered Mega, a bit of overkill on the hardware side, but usable :wink:

https://github.com/blynkkk/blynk-server

Be aware that it can be a possible challenge of it’s own in the install, setup and migration from the Cloud Server to it (they are totally separate, so new login, project transfers and auth codes will be required), but I think you can handle it :smiley:

You need to choose hardware based on your needs and not what other people say :wink:.

In general, it works well. But, USB requires intermediate part - the socat utility. This utility establish SSL connection between your hardware serial port and Blynk server ssl port. The socat is not our software, this is open-source that we use because it is already there. And it may stop working in case connection was dropped. Sometimes socat may not detect connection drop and we can do nothing here as this is not our software. We made a script that tries to reconnect periodically, but it also may fail.

And all this applied only for connection between your hardware and blynk server. Ther other part is your code. And here very hard to give you an answer what’s is wrong. As hardware has bugs, software for your hardware has bugs, your code probably has bugs. So it is really not that simple. We try to help when we have time. However, everyone here has other things to do.

https://www.ebay.com/p/NodeMCU-Lua-WiFi-Internet-Things-Development-Board-Based-Esp8266-Cp2102-Module/1250202765

What do you recommend as the best and most reliable to way to connect?

For MEGA the easiest way would be ethernet shield (china clone costs ~3-5$, I have such on my MEGA). However, ethernet from my point of view is outdated. As all IoT now is mostly Wi-Fi or 3G.

1 Like

Oh, thanks for that info :+1: Socat… Something more for me to look at and get confused by :stuck_out_tongue:

@Gunner don’t do that if you want to sleep well :slight_smile:.

1 Like

Yeah I just looked into this… looks a bit intense. Think I’ll give that a miss.