Code stops running after curtain time

Hi,
I have a code that stops running after a curtain time. It controls two pumps toggling ON/OFF at a given rate, manual switches overrides the automatic operation. But after a while it doesn't work anymore.



#define CLOSED                       LOW
#define OPENED                       HIGH

#define ENABLED                      true
#define DISABLED                     false

#define PUMPon                       LOW
#define PUMPoff                      HIGH

//***************************************************************

boolean switch1Flag                = !ENABLED;
boolean switch2Flag                = !ENABLED;

byte lastSwitch1State              = OPENED;
byte lastSwitch2State              = OPENED;

byte pump1State                    = PUMPoff;
byte pump2State                    = PUMPoff;

const byte pump1Relay              = 2;
const byte pump2Relay              = 4;
const byte SWITCH1                 = 7;
const byte SWITCH2                 = 8;
const byte heartbeatLED            = 13;

//timing stuff
unsigned long pump1Millis;
unsigned long pump2Millis;
unsigned long heartbeatMillis;
unsigned long switchMillis;

unsigned long pump1_interval;
unsigned long pump2_interval;

// PUMP 1 right 6min
const unsigned long pump1_ON_time  = 15000;
const unsigned long pump1_OFF_time = 40000;       

// PUMP 2 left 4min
const unsigned long pump2_ON_time  = 13000;
const unsigned long pump2_OFF_time = 35000;


//***************************************************************
void setup()
{
  Serial.begin(115200);

  pinMode(heartbeatLED, OUTPUT);
  pinMode(pump1Relay, OUTPUT);
  pinMode(pump2Relay, OUTPUT);

  pinMode(SWITCH1, INPUT_PULLUP);
  pinMode(SWITCH2, INPUT_PULLUP);

  digitalWrite(pump1Relay, PUMPoff);
  pump1State = PUMPoff;
  pump1_interval = pump1_OFF_time;

  digitalWrite(pump2Relay, PUMPoff);
  pump2State = PUMPoff;
  pump2_interval = pump2_OFF_time;

} //END of setup()

//***************************************************************
void loop()
{
  //*************************************                          h e a r t b e a t   T I M E R
  //to see if the sketch is blocking,
  //toggle the heartbeat LED every 500ms
  if (millis() - heartbeatMillis >= 500)
  {
    //restart the TIMER
    heartbeatMillis = millis();

    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //*************************************                          c h e c k S w i t c h   T I M E R
  //is it time to read the switches ?
  if (millis() - switchMillis >= 50)
  {
    //restart the TIMER
    switchMillis = millis();

    checkSwitches();
  }

  //*************************************                          P U M P   1 
  //if the switch is 'not enabled' proceed with autmatic operation
  if (switch1Flag == !ENABLED)
  {
    //***********************************                          p u m p 1   T I M E R
    //is it time to toggle the pump relay ?
    if (millis() - pump1Millis >= pump1_interval)
    {
      //restart the TIMER
      pump1Millis = millis();

      //is pump1 now OFF ?
      if (pump1State == PUMPoff)
      {
        digitalWrite(pump1Relay, PUMPon);

        pump1State = PUMPon;

        pump1_interval = pump1_ON_time;
      }

      else
      {
        digitalWrite(pump1Relay, PUMPoff);

        pump1State = PUMPoff;

        pump1_interval = pump1_OFF_time;
      }
    }
  } //END of   if(switch1Flag == !ENABLED)


  //*************************************                          P U M P   2 
  //if the switch is 'not enabled' proceed with autmatic operation
  if (switch2Flag == !ENABLED)
  {
    //***********************************                          p u m p 2   T I M E R
    //is it time to toggle the pump relay ?
    if (millis() - pump2Millis >= pump2_interval)
    {
      //restart the TIMER
      pump2Millis = millis();

      //is pump2 now OFF ?
      if (pump2State == PUMPoff)
      {
        digitalWrite(pump2Relay, PUMPon);

        pump2State = PUMPon;

        pump2_interval = pump2_ON_time;
      }

      else
      {
        digitalWrite(pump2Relay, PUMPoff);

        pump2State = PUMPoff;

        pump2_interval = pump2_OFF_time;
      }
    }
  } //END of    if(switch2Flag == !ENABLED)

} //END of loop()


//********************************************************************************
void checkSwitches()
{
  //*********************************************                    S W I T C H 1
  //SWITCH1 code
  byte currentState = digitalRead(SWITCH1);

  //**********************
  //was there a change in state ?
  if (lastSwitch1State != currentState)
  {
    //update to the new state
    lastSwitch1State = currentState;

    //**********************                                         C L O S E D
    //is the switch closed ?
    if (currentState == CLOSED)
    {
      digitalWrite(pump1Relay, PUMPon);

      pump1State = PUMPon;

      //allow manual switch bypass
      switch1Flag = ENABLED;
    }

    //**********************                                         O P E N E D
    //the switch is opened
    else
    {
      digitalWrite(pump1Relay, PUMPoff);

      pump1State = PUMPoff;

      pump1_interval = pump1_OFF_time;

      pump1Millis = millis();

      //cancel manual switch bypass
      switch1Flag = DISABLED;
    }

  } //END of SWITCH1 code


  //*********************************************                    S W I T C H 2
  //SWITCH2 code
  currentState = digitalRead(SWITCH2);

  //**********************
  //was there a change in state ?
  if (lastSwitch2State != currentState)
  {
    //update to the new state
    lastSwitch2State = currentState;

    //**********************                                         C L O S E D
    //is the switch closed ?
    if (currentState == CLOSED)
    {
      digitalWrite(pump2Relay, PUMPon);

      pump2State = PUMPon;

      //allow manual switch bypass
      switch2Flag = ENABLED;
    }

    //**********************                                         O P E N E D
    //the switch is opened
    else
    {
      digitalWrite(pump2Relay, PUMPoff);

      pump2State = PUMPoff;

      pump2_interval = pump2_OFF_time;

      pump2Millis = millis();

      //cancel manual switch bypass
      switch2Flag = DISABLED;
    }

  } //END of SWITCH2 code



  //*********************************************                    o t h e r S w i t c h e s
  //next switch code
  //*********************************************

} //END of   checkSwitches()



//********************************************************************************

How long does it work correctly? Many minutes, curtainly longer than a few cycles?

Sry, couldn’t resist. Assume you mean “certain”. But I ask the question. If it fails after many cycles, it may not be software at all.

a7

probably between 20-30min

What could it be when it's a hardware issue?

Show us good images of the wiring.


Show the motor/relay/power supply connections.

Are the red and brown wires soldered ?



It's a bit messy and other colors of wires.

The black cord with is directly attached to the rely, is an extension cable. Because I didn't want to cut directly in the pumps cord.

Worst wiring seen here !

image

do you think, this causes the problem??

Yes.

ok, thank you!

And the code is definitely able to repeat infintly?

I will do it again a clean. Might you know a good tutorial on how to learn clean good wiring?

TBH no one is going to look closely…

The fact that it runs OK for some time, coupled to the rats’ nest that is your wiring, it is very probably an electrical problem, either power or intermittent connections.

Please hand draw a schematic showing how everything is (at least supposed to be) wired, inluding any relay control circuitry and how power is provided.

… no one will dive into the code I think until.

a7

Thanks.

I think LarryD might know, because he guided me through this code in the past.

The code should run continually/constantly.


Add a LED and switch to your circuit.

Turn ON a this LED in setup( ).

After the LED turns ON (after power up) use the switch to manually turn OFF the LED.

If the LED ever comes on you know the Arduino has reset/re-powered.

EDIT
BTW, These time comments don’t make sense.


// PUMP 1 right 6min
const unsigned long pump1_ON_time  = 15000;
const unsigned long pump1_OFF_time = 40000;       

// PUMP 2 left 4min
const unsigned long pump2_ON_time  = 13000;
const unsigned long pump2_OFF_time = 35000;

In a case like this, it is nice to have ways of testing the logic independent, to the extent possible, of the hardware attached.

Serial printing, serial input, pushbuttons and LEDs standing in.

And vice versa: small programs that listen to the real inputs and verify plausible behaviour, another to test relays, &c.

Having the whole thing and trying to bring it up all at once is a challenge, as you are finding.

Quite sure that it is a wiring thing or power issue. You have to fiz that anyway, so might as well first.

As for how, I can only suggest to google some basic hobby electronics tutorials.

a7

Those solder connections at the top of the image going to some external device (switch?) look cold.

Does the power for the pumps run through the breadboard?

Yes, these are the switches

No only the switches are connected to the breadbord.

Thanks good advice. I’ll attache a light. Could I also just use the heartbeat?