Program stops working after a while

Hello, I have a simple program which switches a relay after 100 seconds and then after 7 seconds and then after 100 seconds and then after 7 seconds (and goes on like this). The relay stops switching after around half an hour. What might be the problem? Maybe some kind of overflow?

Here is the code:

int relay = 8;
  
boolean onState = true;
unsigned long updateTime = 1000;
unsigned long lastUpdateTime = 0;
unsigned long stateChangeTime = 100000L;
unsigned long lastStateChangeTime = 0;

void setup()
{
  pinMode(relay, OUTPUT);
}

void loop()
{
  unsigned long currentTime = millis();
  
  if (currentTime - lastStateChangeTime > stateChangeTime)
  {
    if (onState == true)
    {
      onState = false;
      stateChangeTime = 7000L;
    }
    else
    {
      onState = true;
      stateChangeTime = 100000L;
    }
    
    lastStateChangeTime = currentTime;
  }
  
  if (currentTime - lastUpdateTime > updateTime)
  {
    if (onState == true)
    {
      digitalWrite(relay, LOW);
    }
    else
    {
      digitalWrite(relay, HIGH);
    }
    
    lastUpdateTime = currentTime;
  }
}
    if (onState == true)

If onState is true, this statement compares true to true, which will be true. If onState is false, the statement compares false to true, which will be false. So, it should be obvious that the == true part of that statement is not needed.

    if (onState == true)
    {
      digitalWrite(relay, LOW);
    }
    else
    {
      digitalWrite(relay, HIGH);
    }

Since true and HIGH have the same value, and false and LOW have the same value, this entire block of code could (and should) be replaced with:

  digitalWrite(relay, onState);

What might be the problem?

It's OK to add Serial.print() statements to the code, to learn what is happening.

How is the relay powered ?

the code hasnt stop working it went under delay. I have modify code litlle bit to under stand how its working. HOpe you got it

#include <avr/wdt.h>
int relay = 8;

boolean onState = true;
unsigned long updateTime = 1000;
unsigned long lastUpdateTime = 0;
unsigned long stateChangeTime = 1000;
unsigned long lastStateChangeTime = 0;
static int count = 0;
void setup()
{
  pinMode(relay, OUTPUT);
  wdt_enable(WDTO_8S);
  Serial.begin(9600);
}

void loop()
{



  while (1)
  {
    wdt_reset();
    unsigned long currentTime = millis();
    if (currentTime - lastStateChangeTime > stateChangeTime)
    {
      Serial.println("loop enter");
      if (onState == true)
      {
        onState = false;
        stateChangeTime = 7000L;
        digitalWrite(relay, LOW);
        Serial.println("7S delay");
      }
      else
      {
        onState = true;
        stateChangeTime = 10000L;
        digitalWrite(relay, HIGH);
        Serial.println("10S delay");
      }
      count++;
      Serial.print("count:"); Serial.println(count);

      lastStateChangeTime = currentTime;


      if (count > 1000)
      {
        count = 0 ;
      }
    }
  }

}

UKHeliBob:
How is the relay powered ?

It is powered by a transistor circuit which switches a 2A/5V power supply. Arduino only signals the base pin of the transistor, the power is supplied by the power supply.

AMPS-N:
the code hasnt stop working it went under delay. I have modify code litlle bit to under stand how its working. HOpe you got it

#include <avr/wdt.h>

int relay = 8;

boolean onState = true;
unsigned long updateTime = 1000;
unsigned long lastUpdateTime = 0;
unsigned long stateChangeTime = 1000;
unsigned long lastStateChangeTime = 0;
static int count = 0;
void setup()
{
  pinMode(relay, OUTPUT);
  wdt_enable(WDTO_8S);
  Serial.begin(9600);
}

void loop()
{

while (1)
  {
    wdt_reset();
    unsigned long currentTime = millis();
    if (currentTime - lastStateChangeTime > stateChangeTime)
    {
      Serial.println("loop enter");
      if (onState == true)
      {
        onState = false;
        stateChangeTime = 7000L;
        digitalWrite(relay, LOW);
        Serial.println("7S delay");
      }
      else
      {
        onState = true;
        stateChangeTime = 10000L;
        digitalWrite(relay, HIGH);
        Serial.println("10S delay");
      }
      count++;
      Serial.print("count:"); Serial.println(count);

lastStateChangeTime = currentTime;

if (count > 1000)
      {
        count = 0 ;
      }
    }
  }

}

Sorry, can you explain it a little more? What is WDT ("watch dog timer" as I see from the internet) and why do we need it here?

http://playground.arduino.cc/Main/ArduinoReset

i have attached link for reference.

sometime you could have found if using infinite loop or motorsheild you might getting error could not able to code/ avrdude error . during these session you will upload bootloader or replace ic with upldated bootloader.

using these instruction whenever program gets struct in particular part of code , the code below fault will not work. it tries to keep repeating loops. this instruction resets your boot loader will all initial value when it got struck.

AMPS-N:
the code hasnt stop working it went under delay. I have modify code litlle bit to under stand how its working. HOpe you got it

Simple code like the OP is using does not need the complexity of a watchdog timer. That's like employing a mechanic instead of getting a reliable car.

The code just needs to be fixed - if it is broken.

Adding print statements will help to show if and where it is broken.

...R

The code just needs to be fixed - if it is broken.

All of AMPS's code is broken. That's why he needs the watch dog timer.

@ paul &robin2

if you guys thinking my code are broken , yes that's reason i am using watchdog timer all time in all part of code i pasted. But i am not you like expert guys instead of helping person helping him finding solution &divert other from actual probelm. I have seen all of you thread you being pasted , you guys just discourage all people who are helping. If you guys really want to help instead of diverting other just reply your geniue answers to question asked.

I accept i may not be perfect you like programmer.but i am not misleading other, whatever solution these i have tried with arduino forum & arduino kit, i am posting it.

its upto the code poster take solution or not to take it.

But i am not you like expert guys instead of helping person helping him finding solution &divert other from actual probelm

But you did divert him, by posting waffle about a watchdog timer, when you should have been telling him to POST HIS SCHEMATIC.

Disconnect the relay and substitute an LED. See if it still stops.

Your code doesn't work at all. Not even for half an hour. Why didn't you tell us? You think we won't notice? I assume that you want to toggle the relay at a 0.5Hz rate, but gated on for 7 seconds, gated off for 100 seconds. Like this:

int relay = 8;
//int relay = LED_BUILTIN;
 
boolean onState = true;
boolean blinkState = true;

unsigned long updateTime = 1000;
unsigned long lastUpdateTime = 0;
unsigned long stateChangeTime = 0;
unsigned long lastStateChangeTime = 0;

void setup()
{
  pinMode(relay, OUTPUT);
}

void loop()
{
  unsigned long currentTime = millis();
 
  if (currentTime - lastStateChangeTime > stateChangeTime)
  {
    if (onState == true)
    {
      onState = false;
      stateChangeTime = 7000L;
    }
    else
    {
      onState = true;
      stateChangeTime = 100000L;
    }
   
    lastStateChangeTime = currentTime;
  }
 
  if (currentTime - lastUpdateTime > updateTime)
  {
    if (blinkState == false && !onState)
    {
      digitalWrite(relay, HIGH);
      blinkState = true;
    }
    else
    {
      digitalWrite(relay, LOW);
      blinkState = false;
    }
   
    lastUpdateTime = currentTime;
  }
}

I anticipated and fixed another problem - since only the toggle is gated, not the output state, there is a risk that the relay could be left in the on state between activations. So I only turn it on if the onState is active.

aarg:
Your code doesn't work at all. Not even for half an hour. Why didn't you tell us? You think we won't notice? I assume that you want to toggle the relay at a 0.5Hz rate, but gated on for 7 seconds, gated off for 100 seconds.

I don't understand what you mean by this. And I don't understand what you fixed in your code. Can you elaborate?

I ran your code, unmodified except for redirecting the relay output to the onboard LED. It didn't work as you described. Perhaps you uploaded the wrong code.

aarg:
I ran your code, unmodified except for redirecting the relay output to the onboard LED. It didn't work as you described. Perhaps you uploaded the wrong code.

I changed the pin number from 8 to 13 to test the LED and it is working. Perhaps you didn't understand what it is doing?

The led is off for 100 seconds, then it is on for 7 seconds, then it is off for 100 seconds, then it is on for 7 seconds...

AWOL:
But you did divert him, by posting waffle about a watchdog timer, when you should have been telling him to POST HIS SCHEMATIC.

I really don't think hardware is the problematic part since I have the same circuit running for days with the relay working. Just not with this program.

Here is the relay part of the circuit, Arduino pin goes to the RESET_CONT pin:

http://imgur.com/ffD5aPs

ZuLuuuuuu:
I changed the pin number from 8 to 13 to test the LED and it is working. Perhaps you didn't understand what it is doing?

The led is off for 100 seconds, then it is on for 7 seconds, then it is off for 100 seconds, then it is on for 7 seconds...

Oh, really? Then what is the purpose of your 1000 ms. time constant and the associated if statement inside loop()?

aarg:
Oh, really? Then what is the purpose of your 1000 ms. time constant and the associated if statement inside loop()?

If you don't understand the code please ask nicely so that I explain. You are embarrassing yourself.

I asked a simple question. No need to get your feathers in a ruffle.