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 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.
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.
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.
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.
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:
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: