Go Down

Topic: runtime error... memory overload? (Read 1 time) previous topic - next topic

TempleClause

my programm keeps crashing and I don't have a clue why. Does anyone spot an error in this code?
Thanks a lot

Code: [Select]
void runTimeLapse(){
  long elapsedTimeUntilNow = millis();

  int totalPhotos = ceil(runFor / (timeBetween + triggerFor));
  int photosTaken = 0;
  boolean trigger = false;
  int elapsedTime = 0;
  long startOfNextPhoto = 0;
  int photoLength = timeBetween + triggerFor;

  while((millis() - elapsedTimeUntilNow) / 1000 < runFor){
    elapsedTime = (millis() - elapsedTimeUntilNow) / 1000;
    startOfNextPhoto = photosTaken * photoLength;
    if(elapsedTime >= startOfNextPhoto && elapsedTime < startOfNextPhoto + triggerFor){
      trigger = true;
      printRemainingTime(0,1);
      digitalWrite(triggerPin,HIGH);
      lcd.setCursor(15,1);
      lcd.print("X");
    }
    else{
      if(trigger){
        photosTaken++;
        printPhotosTaken(photosTaken, totalPhotos);
      }
      digitalWrite(triggerPin,LOW);
      lcd.setCursor(15,1);
      lcd.print(" ");
      trigger = false;
      printRemainingTime(startOfNextPhoto - elapsedTime, 1);
    }
    printRemainingTime(runFor - ((millis() - elapsedTimeUntilNow) / 1000), 0);
  }
}

KeithRB

Well, for one thing, elapsed timeuntil now should be *unsigned* long to match millis() output and prevent a sudden switch to a negative number.

LarryD

I was under the impression you should use, unsigned long
Quote
long elapsedTimeUntilNow = millis();

Sorry, I can't help any more than this.
The way you have it in your schematic isn't the same as how you have it wired up!

TempleClause

gonna give this a try, thanks for the fast answers

Jimmy60

I see a bunch of mixing of types. I also don't see all the code. All variables that I use in calculations involving millis() I declare as unsigned long. Any constants I use in such calculations get UL at the end.

I see some division by 1000 to compare to runFor. If runFor was multiplied by 1000 then this division wouldn't be needed. I'm guessing runFor represents seconds. It would be better to have it represent millis().

That's some stuff I see but I'm just a hack. Some smarter than me guys will no doubt find some other issues.

Go Up