Program Halting after 24 hours

Greetings,

So I have a aquarium(oh yes another one), and I'm using an Arduino Mega to turn on the lights and other functions however this software just does the lights as the program is halting and is no longer responding as expected.

What I want the code to do and what works at least for a 24 hours period turn the lights on and off based off the sun positions IE sunrise twilight, sunrise, sunset and sunset twilight. I did this with suncalc() and is my code. The other thing it does is Ramp UP/Down based on a curve, this part was not programmed by myself I asked a friend that has programming experience for help. I tied a potentiometer to the ramp up/down to control intensity. You can fry corals by turning the LEDS on full blast!

What happens If I turn the arduino on in the morning it runs fine though out the day turns on ramps up ramps down all turns off. All Good. However over night things go downhill. Suncalc() runs but at some point in time after that the ramp up/down crashes it turns on one maybe two channels and that all she wrote. Adjusting the pot does nothing only thing that works is hitting reset which is hard to get to or unplugging the arduino and pluggging it back in. For the moment to solve this issue I have the arduino on an Aquarium timer that turns it on and off at night.

Am I asking too much of the mega or do I need to try and clean this up?

I attached my code.

Thanks
-Eilelwen

Reef_Main_V4.ino (10 KB)

I haven't downloaded your code - I'm just not that generous, sorry.

You will need to isolate whether the problem has to do with your clock or with some other aspect. To do that I would follow two strategies.

I would make a simplified version of your clock code that just blinks an led every 10 minutes (or some such) and leave it run for a couple of days just to ensure the clock can run past 24 hrs.

Separately I would make a slightly different version of your entire project arranged so that it runs through a full 24 hr cycle in (say) 10 minutes. This will show if non-timer problems are causing the crash and because it is running quickly you can add Serial.print statements to monitor values on your PC.

...R

Alright I'll give these a try!

on the 1st one as this is all connected now, would it be too complicated to have it still turn the lights on/off no ramp nothing fancy just on at a time off at a time, and increment a number on the LCD? Problem I'm having right now is my old lights are bad and I until I get some Coral recovery using the arduino to just blink an LED isn't an option atm when it is I can do that for sure!!

2nd one easy I'll do this next time I'm sitting next to the Arduino!

Thanks. I will let you know the results
-Eilelwen

Nothing sticks out as an obvious issue. Here's a possible hazard though:

     deg = 360.0 * position / total;

Can total ever be zero? I'm not sure what the arduino does on a divide by zero condition, but you might want to test for it.

Other than that, it's time to pepper your sketch with serial print. Since you're resetting nightly, do you have another arduino with the ability to write to an SD card - might make it easier to gather and save telemetry.

Well

total = ((double)end_time) - start;

End time should almost always be around 8 or 9pm PST.

However if suncalc failed to run properly it could give a 0 end time.

Unfortunately I only have the 1 arduino and to make everything even more interesting I don't live at the same house as this arduino as I'm in the process of moving.

I'll make a trip out tonight and see what information I can gather.

I do have an extra laptop I can leave plugged in however with a serial print. I'll take it with me and see what I can get from it.

Perhaps you could write a small program on your Laptop (using Python or Ruby, for example) that collects the Serial.print messages and saves them to a text file along with Laptop generated time-stamps. If you can leave the laptop running for a day or two you may get some useful data that would be too tedious to collect manually.

You could then search through the text file with a text editor to find the interesting bits.

...R

eilelwen:
I do have an extra laptop I can leave plugged in however with a serial print. I'll take it with me and see what I can get from it.

Perhaps you could write a small program on your Laptop (using Python or Ruby, for example) that collects the Serial.print messages and saves them to a text file along with Laptop generated time-stamps. If you can leave the laptop running for a day or two you may get some useful data that would be too tedious to collect manually.

You could then search through the text file with a text editor to find the interesting bits.

...R

I'm afraid this is beyond my abilities, I wouldn't even know where to begin on that :frowning:

However I will do some googling it seems there is already an article on it so I'll see what I can come up with :slight_smile:

-Danny

Ok I think this will do what I want. I'll give it a go later and get back to you guys. Thanks for your help so far!

#!/usr/bin/python
import time;
import serial;

localtime = time.asctime( time.localtime(time.time()) )

ser = serial.Serial("COM1", 9600)

while True:
	if(ser.isClosed()):
		break
	try:
	# This tries to open an existing file but creates a new file if necessary.
		logfile = open("c:\log.txt", "a")
		try:
			str = ser.readline()
			logfile.write(localtime)
			logfile.write(str)
		finally:
			logfile.close()
	except IOError:
		pass

As far as I can see this will nearly do what you want. I think there is a minor problem as it only gets the time once at startup. You should probably move the line

localtime = time.asctime( time.localtime(time.time()) )

inside the loop before "str = ser.readline()" so that it updates the time before it writes to the file

You might also want to consider whether you need to write the time to the file every time or just once every 15 or 30 minutes. But that's a little more complex.

I don't know enough about Python to know if your code will write the time on one line and the Arduino output on another line. It would be better if both were on the same line separated by a comma as that would make it easy to read the Logfile into a spreadsheet as a CSV file. I hope some other Forum reader can help on this point as I don't want to suggest changes that might break things.

...R

eilelwen:
Ok I think this will do what I want. I'll give it a go later and get back to you guys. Thanks for your help so far!

Quick update, I'll edit this as I get more information:

I just ran a speed test and it failed as soon as the lights started to come on. so now at least I have a starting point!

Had to change the python code a bit, this reads it but its very messy.

Update so watching the speed test and something interesting happened, It didn't crash but it sure bogged way down. I went from a minute being 1 second to 24 seconds. This all happened when I hit the ramp up/down code. it would appear I may want to simplify that code.

#!/usr/bin/python
import time
import serial

ser = serial.Serial(3, 9600)
time.sleep(2)

while True:
	localtime = time.asctime( time.localtime(time.time()) )
	logfile = open("D:\log.txt", "a")
	ard = str(ser.read(100))
	logfile.write(localtime)
	logfile.write(ard)
	logfile.write("\n")
	line = ser.read(100)
	print(line)

Solved:

it was the ramp up/down a friend of mine who codes for a living redid a vast portion of the code. This has solved the crashing when the program ramps and ramps down the LEDS!!

Thanks for your help everyone!

-Eilelwen