I'm confused. I'm building an intervalometer for my camera but the loop() function isn't looping. I added in the first and last lines for debugging. It prints "the end" signifying the first loop has completed but never prints "the beginning" meaning that loop() does not restart. loop() works properly on a smaller sample sketch so I don't think the problem is hardware or setup related. Using an Arduino Uno R3. Anyone know what might be going on?
void loop() {
lcd.setCursor(0,1); lcd.print("the beginning");
// Get user input
int exposureCount = exposureCountSelect(); // exposure count
int exposureTime = exposureSelect(); // exposure time in seconds
int intervalTime = intervalSelect(exposureTime); // delay time in seconds
if( userWait(exposureTime, exposureCount, intervalTime, "Press to start. ") != 0 ) { return; } // wait for user to start
// Time to take photos
unsigned long startTime = millis();
unsigned long endTime = ((exposureCount*intervalTime)+startTime);
for( int i=1; i <= exposureCount; i=i+1 ) {
if( takePhoto(exposureCount, exposureTime, intervalTime, i, startTime, endTime) != 0 ) { return; }
}
userWait(exposureTime, exposureCount, intervalTime, "Frames complete!"); // Wait for user to acknowledge completion
lcd.clear(); lcd.print("the end");
lcd.setCursor(0,1); lcd.print(freeMemory()); delay(3000);
}
I was able to narrow it down somewhat. When I commented takePhoto() out of loop() then it looped properly. Probably still some bugs in takePhoto() but I don't see anything that would cause loop() to break. I tried commenting out parts of takePhoto() to narrow it down. It doesn't seem like there's any single line causing a problem, but when I add any 2 or 3 of the lines in together then loop() stops working. freeMemory() says I have over ~1500 bytes available both inside takePhoto() and at the end. The cameraSnap() subfunction works properly in another test sketch.
int takePhoto(int exposureCount, int exposureTime, int intervalTime, int currentCount, unsigned long startTime, unsigned long endTime) {
char string1[3];
// Write to LCD
lcd.clear();
lcd.setCursor(0,0); lcd.print("Frame ");
lcd.setCursor(6,0); sprintf(string1,"%3d",currentCount); lcd.print(string1);
lcd.setCursor(9,0); lcd.print("/");
lcd.setCursor(10,0); sprintf(string1,"%3d",exposureCount); lcd.print(string1);
lcd.setCursor(4,1); lcd.print("minutes left.");
// If we're in bulb mode, two IR clicks are required. Otherwise just one.
analogWrite(redLed, backlightLevel);
unsigned long initialTime = millis();
cameraSnap();
while( millis() < ((exposureTime*1000ul)+initialTime) ) {
lcd.setCursor(0,1); sprintf(string1,"%3d",(((endTime-millis())/60000)+1)); lcd.print(string1);
if( readButton() == 2 ) { if( cancelSelect() != 0 ) { break; } }
}
if ( exposureTime != 0 && exposureTime != 1800 ) { cameraSnap(); }
digitalWrite(redLed, LOW);
while( millis() < ((intervalTime*1000ul)+initialTime) ) {
if( currentCount == exposureCount ) { break; };
lcd.setCursor(0,1); sprintf(string1,"%3d",(((endTime-millis())/60000)+1)); lcd.print(string1);
if( readButton() == 2 ) { if( cancelSelect() != 0 ) { return 1; } }
}
lcd.clear(); lcd.print("inside takePhoto");
lcd.setCursor(0,1); lcd.print(freeMemory()); delay(3000);
return 0;
}