Trouble with Trigger/ADC

I'm trying to make a function that when called, waits for a trigger signal to change from 0 to 1 on a digital input, then delays 5 seconds, takes an ADC and displays the result to the LCD. Right now, it waits for the trigger singnal, but then once the trigger level goes from 0 to 1, the LCD very quickly flashes 'Taking Reading...' and then it seems that it crashes because the LCD is cleared and I can't get anything else to happen. Here is the code I have for the function so far:

void doReading(){
  noInterrupts();
  lcd.clear();
  lcd.print("Waiting...");
  //int triggered = 0;
  while(trigger){
    trigger = digitalRead(triggerPin);
    delay(100); 
  }
  //probably have to add a delay
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.print("Taking Reading..");
  delay(5000);
  curReading = analogRead(sensorPin);
  lcd.clear();
  getDateDs1307();
  lcd.setCursor(0,0);
  lcd.print("Reading: ");
  lcd.print(curReading);
  delay(2000);
  //writeToEeprom();
  exitFunct = 1;
  interrupts();
  while(exitFunct);
}

it seems that it crashes

Does it crash or not? Put some Serial.prints in to see exactly where the code is going.

exitFunct = 1;
while(exitFunct);

Waste of time, just let the function return.

Why disable interrupts?

getDateDs1307();

What's this for, there's no obvious use of any results.


Rob

Graynomad:
exitFunct = 1;
while(exitFunct);

Waste of time, just let the function return.

I suspect that is the point exactly. ?

That while loop was to hold the reading display on the LCD before returning to the loop() function. exitFunct is an int variable that is toggled in an interrupt that would end that while loop supposedly. I'm currently adding some serial.prints to see if it really is crashing. I'll just have to temporarily disable some Xbee stuff. If I don't get anywhere and no one else has ideas I can post my whole program it isn't terribly long.
Also, getDateDs1307() gets the date from an I2C device and stores it in global variables since there is day, month, year, hour, etc that I want in all separate variables so they are easier to store in EEPROM and transmit over XBee.

Any variables used inside interrupts need to be declared volatile, otherwise the compiler is likely to optimize out code that it thinks doesn't accomplish anything.

From the documentation for attachInterupt() :

Inside the attached function, delay() won't work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.

Alright, I made the volatile now. I am not using the delay() call inside an interrupt though. The interrupt merely sets up some variables for the if statements in the loop() so that functions that I want get called. delay() is still not functioning though and it blows right by the delay(5000) which from my understanding should be a 5 second delay. Thanks for all the input so far!

EDIT: Here is the current code for my analog reading method. It prints out 'Taking reading' and then it never does anything again no mater how long I wait. I've tried the commented out method as well as the simple delay that is in now.

void doReading(){
  lcd.clear();
  lcd.print("Waiting...");
  Serial.print("Waiting");
  //int triggered = 0;
  while(digitalRead(triggerPin) != 1);
  //probably have to add a delay
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.print("Taking Reading..");
  Serial.print("Taking reading");
  //volatile unsigned long waitUntil = millis() + 5000;
  //while(millis() < waitUntil);
  delay(5000);
  Serial.print("Delay finished");
  curReading = analogRead(sensorPin);
  Serial.print("Reading complete");
  lcd.clear();
  //getDateDs1307();
  Serial.print("Got date");
  lcd.setCursor(0,0);
  lcd.print("Reading: ");
  lcd.print(curReading);
  Serial.print("Reading: ");
  Serial.print(curReading);
  //delay(2000);
  //writeToEeprom();
}

In the complete code above, it looks like doReading() is only called from okAction() which is only called as an interrupt routine.

Ha obviously. Guess I overlooked that thought. Thanks for the help! Delay works perfect now and no crashing.