Pages: [1]   Go Down
Author Topic: Trouble with Trigger/ADC  (Read 838 times)
0 Members and 1 Guest are viewing this topic.
Milwaukee, WI
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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);
}
Logged

Kyle

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8493
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
« Last Edit: March 08, 2011, 07:26:14 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

 exitFunct = 1;
  while(exitFunct);

Waste of time, just let the function return.

I suspect that is the point exactly. ?
Logged

Milwaukee, WI
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: March 08, 2011, 08:22:29 pm by nncs20 » Logged

Kyle

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From the documentation for attachInterupt() :
Quote
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.
Logged

Milwaukee, WI
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
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();
}
« Last Edit: March 09, 2011, 09:45:34 pm by nncs20 » Logged

Kyle

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Milwaukee, WI
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Kyle

Pages: [1]   Go Up
Jump to: