Oscillating pin causes memory error

Ok, so I got some help in the way of millis() with this project already, but I noticed this strange occurrence:

Exception in thread "AWT-EventQueue-0"
java.lang.OutOfMemoryError: Java heap space

I'd run my reef controller and has this thing going supposedly 24/7, but strangely after a couple hours it would stop and stick in one state. So I ran it with my Serial attached, displayed the numbers to see if the coincided or if they were messing up when the function stopped working. That error was the result. Here's the code:

void updateWave() {
  if(millis() - switchTime <= waveFrequency) {
    waveState = !waveState;
    switchTime = millis() + waveFrequency;
    
    Serial.print(waveState);
    Serial.print(", ");
    Serial.print(switchTime);
    Serial.print(", ");
    Serial.print(millis());
  }
  
  if(waveState)
    digitalWrite(waveMaker, HIGH);
  else
    digitalWrite(waveMaker, LOW);
}

Is there a way to clear memory or the memory for that specific event so I can run this 24/7?
Thanks,

Use a different terminal application (Serial Monitor replacement). I have had good luck with these...

http://www.chiark.greenend.org.uk/~sgtatham/putty/

void updateWave() {
  if(millis() - switchTime <= waveFrequency) {
    waveState = !waveState;
    switchTime = millis() + waveFrequency;

Interesting way to code the blink-without-delay pattern. I suspect it will go haywire when millis is close to wrapping. Have you carefully evaluated the code as millis approaches and the passes the wrap? If switchTime or waveFrequency are signed I suspect the code will simply not work.

In any case, the code in question will never cause your Arduino to "stop and stick in one state".

I guess I can't say I've tested the wrap around issue yet, but after talking with some people on here and some research, in theory, when millis() wraps around back to zero, switchTime will simple drop all the way back down to millis() + waveFrequency, so roughly 2000. Regardless, having the values print to Serial, millis() doesn't even make it to its wrap around point by the time the function fails. Like I said, it runs for a couple hours (instead of the ~50 days millis() is suppose to last). I've never had this issue before until the addition of this function, so I can only assume I'm loading up something in memory by doing this repeatedly and it's unable to clear it out. I declared switchTime as a variable in the main program (I guess you can call it a global variable) as an 'unsigned long'. I've looked through the forums, and I guess after a while my SRAM is filling up, but there's no real way to clear it. Is there a way to avoid this error or run this function in a more efficient manner?

I don't think it's a terminal issue. The system runs the same way on stand alone. After a few hours, this function in particular stops and remains in one state -> Either the pin is on or off instead of oscillating.

....Although... If the Serial.print statements are still in the code even though it may not be attached to a terminal of some kind, would that still load up the SRAM?

Endevor:

Exception in thread "AWT-EventQueue-0"

java.lang.OutOfMemoryError: Java heap space

This error is occurring on the PC not on the Arduino. Someone suggested trying a different terminal application which will help to identify if the problem is within the Arduino IDE - which is based on the JVM.

What interaction happens between your Arduino and the PC? If the PC is just receiving and displaying serial data from the Arduino I can't see why it is running out of Java Heap unless there is a bug in the Arduino IDE. What baud rate are you using? Does a different baud rate have any impact?

...R

I'm using 9600. I guess I can try a different baud and see what that does, of another Terminal. Maybe I'll figure out why it keeps stopping when the Arduino is on stand alone. Still, I can't think of it that would make it stop. As far as all the other functions go, they all work. It's just this function that's giving me problems.

Endevor:
I don't think it's a terminal issue.

This is a problem with Serial Monitor...

Exception in thread "AWT-EventQueue-0"
java.lang.OutOfMemoryError: Java heap space

Endevor:
I've never had this issue before until the addition of this function, so I can only assume I'm loading up something in memory by doing this repeatedly and it's unable to clear it out.

It's just this function that's giving me problems.

Did you not read my post or are you choosing to ignore what I wrote?

Robin2:
This error is occurring on the PC not on the Arduino.

What part of this sentence isn't clear?

Or is it the case that you have two separate problems and have only told us about one of them?

Maybe I'll figure out why it keeps stopping when the Arduino is on stand alone

...R

It's impossible to tell exactly what's wrong with your code since you have only posted a snippet, but that code you posted is wrong and unlikely to handle rollover correctly. This could cause your sketch to produce a large amount of output when rollover occurs, which might be what has provoked the out-of-memory problem on the PC side. You should be able to avoid the out-of-memory problem by using a more capable serial monitor such as Realterm, but you also need to correct your sketch so that it handles rollover correctly.

I'm sorry, I'm not trying to ignore anything, I don't think I'm making myself as clear as I probably should be.

I understand the error I received is a Terminal issue. I guess what I should say then is I have two problems.

Problem 1: When the Arduino is on its own, running my aquarium and whatnot, it will run this function for a couple hours then stop, leaving the wave maker either on or off. I want it to oscillate 24/7 at a specific interval. Currently, it's every two seconds. I tried to figure out what's going on which led to problem 2.

Problem 2: when I print the values of my wave maker's state, the time it needs to switch, and millis() to the Serial, I get that error. So when I first got that error, I thought it was occurring with the Arduino, not the terminal. I'm sorry for that confusion. I'm also sorry, I don't mean to sound like I'm ignoring any advice, when I say "I don't think it's a terminal issue" I'm referring back to my first problem, when it's not printing anything to Serial. I should've made that clearer, my bad.

So I know the solution to Problem 2 at least which is use a different Terminal and run the tests again.

PeterH:
It's impossible to tell exactly what's wrong with your code since you have only posted a snippet, but that code you posted is wrong and unlikely to handle rollover correctly. This could cause your sketch to produce a large amount of output when rollover occurs, which might be what has provoked the out-of-memory problem on the PC side. You should be able to avoid the out-of-memory problem by using a more capable serial monitor such as Realterm, but you also need to correct your sketch so that it handles rollover correctly.

If you want, I can share a dropbox link to my code since I have it broken up in several tabs and it's not exactly short if you think it'll help. Otherwise, everything else seems to run fine when it stands along, this function just stops oscillating the pin after a few hours. I do see your point when it reaches the wrap around. My switchTime won't be able to handle when millis() is less than the frequency's worth from the wrap around point. My thought was that switchTime would also wrap around as an unsigned long. I.E. As millis() approached 4,294,967,295, say it hit 4,294,966,295, switchTime would then equal 4,294,967,295 + 2000(frequency). So switchTime would result in the number 1000, or is millis() the only value that will return to 0 and start over? If I'm wrong, then yes, I need to correct that. If not, the issue's elsewhere since millis() is suppose to last for ~50 days and I'm not reaching that point before it fails.

All in all, looks like I need to run my tests again with a different terminal and see what I get. Again, sorry for any confusion, I don't mean to step on anyone's toes.

Here's a link to my project thus far if you think it'll help.

https://www.dropbox.com/s/h9dw2u1p9nfj8oy/Reef%20Controller%20v0.2.zip

Board?

#include <LED.h>

Download from?

I got the library from here:Google Code Archive - Long-term storage for Google Code Project Hosting.

I'm using the Arduino Mega 2560.

I haven't been able to retest it yet. Had the pleasure of moving the whole setup yesterday afternoon. I'll try to get some results tonight after work.

Thanks again

When I have time I will look more closely but my first impression is that the LED Library is leaking memory. I suspect the problem stems from the library author(s) failure to free an object instance created in the various constructors (an Animation).

In the meantime, I suggest adding some code to occasionally print the amount of free memory available.

So, funny story now. The system as it was was functional enough to run the reef which it was doing actually just moments ago (I'm mopping up water still now) when suddenly all the bad luck I've been able to dodge all my life reached up and dragged me to hell. My acrylic tank cracked down the side on one of the corners and pretty much drained the entire contents onto the floor and onto my desktop... So now, I'm not even sure if my circuitry for the aquarium works anymore, on top of that I don't even know if my gaming rig works anymore, an I get to explain to facilities in the morning why my apartment smells like the ocean.

All that being said, I'm sure I'll be coming back to this dilemna in the future, but for now, I have a lot of repairs to do and hopefully a lot of salvaging.

Thanks for all the help though, it's definitely given me some things to look into when I get back into the code. I'll probably even try to make my own LED library that caters more to my needs.