Show Posts
Pages: [1] 2 3
1  Using Arduino / Programming Questions / Re: Program stalls while in infinite loop on: September 04, 2014, 09:03:39 pm
Thanks for all the input.  I found out the culprit was the S300 sensor I was using; I have used it in the past but only as an analog output and not via I2C.  I converted everything to the 'I2c.h' library but the script was still stalling.

@Nick Gammon, I used the I2c library you list on you webpage for communication with a timeout, and still had the program stall.  I followed the syntax in the example sketch, but noticed that in the library .cpp file there is a function definition for "timeout(...)".  At a quick read it didn't occur to me if there is a default timeout in the library, or if you need to explicitly define a timeout period (which I did not and I speculate may be the reason the code is still hanging up...).

Thanks again!
2  Using Arduino / Programming Questions / Re: Program stalls while in infinite loop on: August 29, 2014, 10:22:21 pm
Scratch the pull-up resistor bit, saw the shots under your I2C section.  Good stuff!
3  Using Arduino / Programming Questions / Re: Program stalls while in infinite loop on: August 29, 2014, 10:13:59 pm
Good to know!  I'll try using the Wire library you mention which uses a time out.  I too have never had a hangup with I2C, but there is a first time for everything.  I'll see if I can put the circuit on a scope, but it is pretty well riddled with decoupling capacitors, so I don't think this is an issue.  This is the first time i've used the CO2 sensor with I2C, so perhaps there is something going on there, and according to the spec sheet (http://tccelt.co.kr/2012/kor/pdf/s-300/DS_S-300_v2.2.pdf) it has internal pull up resistors, so I didn't include any on the board.  This may also be a culprit. 
Is there any harm in additional pull-up resistors?  I've seen devices with anything from 1.7k to 2.2k, so I don't know what a nominal value would be (probably depends on the device impedance, right?).


At any rate, i've got some good stuff to try here.  Thanks.

Also a last thought... this is the first time that i've used the DS1307 and Time library (previously I used the RTClib library with Arduino 022) -- I was thinking perhaps there is something weird going on with the real time clock as well.  I changed the code to only read from the real-time clock once at the beginning of the sketch, so I don't think that is the problem, but perhaps something else going on with my time-keeping?
4  Using Arduino / Programming Questions / Re: Program stalls while in infinite loop on: August 29, 2014, 09:55:30 pm
Thanks for the thorough read-through; I appreciate it.  I'll incorporate the changes and give it a go.
From a functional stand-point, it seems that of the things you identified, the soft_reset and the error_handling for the SHT_function, would be the two things that might cause the code to fail.  I've used both of these pretty extensively, but perhaps they are just unpredictable and are the culprit in this instance.  I also ported some of this from some work in the arduino022 environment, which did not have read/write as the functions for transmitting via I2C, so perhaps there were some other functional changes to the Wire library that have changed and would affect error communication/handling?

From how the I2C library is written, what would you expect when the Wire.beginTransmission and Wire.requestFrom functions encounter an error?  Would the processor get hung up if a connection isn't established during the Wire.beginTransmission(...) or Wire.requestFrom(...)call?

Thanks again!
5  Using Arduino / Programming Questions / Re: Program stalls while in infinite loop on: August 29, 2014, 08:51:49 pm
Great, thanks!
I have attached the entirety of the sketch.

Quote
You may be running out of memory. Try changing such lines to be:

Code:
        Serial.print(F("Elapsed Time: "));
Aren't the strings in the Serial.print function loaded into the stack when the code is compiled and run for the first loop (in which case the code would have crashed during the first void loop(){} run)?

I also compiled and ran a case with all of the "Serial" lines commented out, and the code still crashed : (
6  Using Arduino / Programming Questions / Program stalls while in infinite loop on: August 29, 2014, 06:31:37 pm
Hi all,
  I have a simple control system put together, and i've been trying to figure out why it has been crashing for the past week to no avail.  I was hoping someone might be able to lend a hand.

Some overview of the hardware:
two pumps; one small one controlled with an npn transistor (pin 45 using PWM) and another with a mechanical relay (pin 48).
three valves all controlled with an npn (pins 37,31,35)
SD card with CS on 44
DS1307 real time clock
bmp085 pressure sensor
sht25 humidity/temp sensor
6 analog output sensors (pins A10-A15)
6 small heaters with npn transistors (PWM pins 4,5,6,8,9,10)
mcp3424 ADC with address pins hard-wired Ground/Float
two leds (pins 26,28)
CO2 sensor using I2C address 0x31
controller is a Mega ADK

The unit works by sampling the sensors and printing the data to an SD card as well as the serial port.  The loop is set up such that the unit will sample for a "sample time" then switch some valves then sample for a "catalyst time", then go back and start over, indefinitely.  The unit will also restart whenever a negative pressure below "PRESSURE_TOLERANCE" is observed.

The setup works great and will run for a few cycles, but will stop working after anywhere from 10 min to and hr or so.  I couldn't find anything happening that correlates with the time that it dies.

I tried to comment out all of the SD card writing lines, so that it only streamed to the serial port, and it still crashed.
I tried to comment out all of the Serial port commands, and it still crashed.
I commented out both, so it would just loop through the delays and flash the leds, and it still crashed.

I hope someone has some ideas or sees something that I don't, and could help me out.  The code is too long for the forum to allow me to post (I can PM it if you want), but here is the main loop.
Thanks a million!

Code:
 void loop()
  {
    BMP085.refresh();
    ambientPressure=BMP085.pressure;
    startTime=now();
    elapsedTime=now()-startTime;
    
    while( (elapsedTime< (SAMPLE_TIME*60+CATALYST_TIME*60)) && abs(BMP085.pressure-ambientPressure)<PRESSURE_TOLERANCE )  
      {
        if(! SD.exists(current_file))
      {
        soft_reset();
      }
      
      else
      {
        logfile=SD.open(current_file, FILE_WRITE);
      }
      
        if(elapsedTime <= SAMPLE_TIME*60)  //chose flow direction
        {
          pathFlag=CatalystFlow(0);
          digitalWrite(red_led,LOW);
          Serial.print("Normal Flow  ");
        }
        if((elapsedTime>SAMPLE_TIME*60))
        {
         pathFlag=CatalystFlow(1);
         Serial.print("Catalyst Flow  ");
         digitalWrite(red_led,HIGH);
        }
        
        currentTime=now();
        elapsedTime=currentTime-startTime;
        Serial.print("Elapsed Time: ");
        Serial.println(elapsedTime);
        
        analogWrite(SAMPLE_PUMP_PIN,SAMPLE_PUMP_FLOW);
        
        digitalWrite(green_led,HIGH);  //flash green light when sampling
        delay(50);
        digitalWrite(green_led,LOW);
        Serial.print("Sampling  ");
        time_t time=now();
        BMP085.refresh();
        get_SHT2x();
        on_board_value[0]=on_board.GetValue(1);
        on_board_value[1]=on_board.GetValue(2);  //18bit adc values
        CO2=getS300CO2();
        
      //  try
       // {
        
         if(logfile)
         {
          logfile.print(model); //model name
          logfile.print(comma);
          logfile.print((year(currentTime))); //current year
          logfile.print(slash);
          logfile.print(month(currentTime)); //current month
          logfile.print(slash);
          logfile.print(day(currentTime)); //current day
          logfile.print(comma);
          logfile.print(hour(currentTime)); //curent hr
          logfile.print(colon);
          logfile.print(minute(currentTime)); //current minute
          logfile.print(colon);
          logfile.print(second(currentTime)); //current second
          logfile.print(comma);
          logfile.print(currentTime); //current unixtime; time since midnight 1/1/1970 (seconds)
          logfile.print(comma);
          logfile.print(CO2); //s300 co2 reading 12bit
          logfile.print(comma);
          logfile.print(on_board_value[0]); //alphaCOB4 aux
          logfile.print(comma);
          logfile.print(on_board_value[1]);  //alphaCOB4 sig
          logfile.print(comma);
          logfile.print(temperature_SHT); //sht temp
          logfile.print(comma);
          logfile.print(humidity_SHT);  //sht Rh
          logfile.print(comma);
          logfile.print(BMP085.temperature);  //bmp temp
          logfile.print(comma);
          logfile.print(BMP085.pressure);  //bmp pressure
          
          for(int j=0;j<=5;j++)
          {
           logfile.print(comma);
           logfile.print(analogRead(Sensor[j]));
          }
          
          for(int j=0;j<=5;j++)
          {
           logfile.print(comma);
           logfile.print((PWMval[j]));
          }
          logfile.print(comma);
          logfile.print(pathFlag);
          logfile.println();
          logfile.flush();
          logfile.close();
         }
         else
         {
           Serial.println("Cannot open logfile");
           digitalWrite(red_led,HIGH);
           delay(200);
           digitalWrite(red_led,LOW);
           SD.begin(CS_SD);
         }
          
      //  }
      //  catch(...)
      //  {
      //  }
        
        //PRINT TO SERIAL MONITOR
        Serial.print("deltaP: ");
        Serial.println(ambientPressure-BMP085.pressure);
        Serial.print(model); //model name
        Serial.print(comma);
        Serial.print((year(currentTime))); //current year
        Serial.print(slash);
        Serial.print(month(currentTime)); //current month
        Serial.print(slash);
        Serial.print(day(currentTime)); //current day
        Serial.print(comma);
        Serial.print(hour(currentTime)); //curent hr
        Serial.print(colon);
        Serial.print(minute(currentTime)); //current minute
        Serial.print(colon);
        Serial.print(second(currentTime)); //current second
        Serial.print(comma);
        Serial.print(currentTime); //current unixtime; time since midnight 1/1/1970 (seconds)
        Serial.print(comma);
        Serial.print(CO2); //s300 co2 reading 12bit
        Serial.print(comma);
        Serial.print(on_board_value[0]); //alphaCOB4 aux
        Serial.print(comma);
        Serial.print(on_board_value[1]);  //alphaCOB4 sig
        Serial.print(comma);
        Serial.print(temperature_SHT); //sht temp
        Serial.print(comma);
        Serial.print(humidity_SHT);  //sht Rh
        Serial.print(comma);
        Serial.print(BMP085.temperature);  //bmp temp
        Serial.print(comma);
        Serial.print(BMP085.pressure);  //bmp pressure
        
        for(int j=0;j<=5;j++)
        {
         Serial.print(comma);
         Serial.print(analogRead(Sensor[j]));
        }
        
        for(int j=0;j<=5;j++)
        {
         Serial.print(comma);
         Serial.print((PWMval[j]));
        }
        
        Serial.print(comma);
        Serial.print(pathFlag);  
        Serial.println();
        
        delay(SAMPLE_RATE*1000);    
    }
    
    analogWrite(SAMPLE_PUMP_PIN,0);
    Evac();
    delay(1000);
    analogWrite(SAMPLE_PUMP_PIN,SAMPLE_PUMP_FLOW);
  }
 
 void soft_reset() //software reset; bring script to beggining of reset loop; internal watchdog on atmega
  {
    asm volatile ("  jmp 0");
  }
                  
 void get_SHT2x()
{
  Wire.beginTransmission(SHT2x_address);
  Wire.write(temp_command);
  check1=Wire.endTransmission();
  
  Wire.requestFrom(SHT2x_address, 3);
  
  TEMP_byte1=Wire.read();
  TEMP_byte2=Wire.read();
  TEMP_byte3=Wire.read();
  
  Wire.beginTransmission(SHT2x_address);
  Wire.write(hum_command);
  check2=Wire.endTransmission();
  
  Wire.requestFrom(SHT2x_address,3);
  HUM_byte1=Wire.read();
  HUM_byte2=Wire.read();
  HUM_byte3=Wire.read();
  
  humidity_board= ( (HUM_byte1 << 8)|(HUM_byte2) & mask );
  temperature_board= ( (TEMP_byte1 << 8)|(TEMP_byte2) & mask );
  
  humidity_SHT= ((125*(float)humidity_board)/(65536)) - 6.00;
  temperature_SHT= ((175.72*(float)temperature_board)/(65536)) - 46.85;
}

float getS300CO2()
{
 int i=1;
  long reading;
  float CO2val;
  Wire.beginTransmission(0x31);
  Wire.write(0x52);
  Wire.endTransmission();
  Wire.requestFrom(0x31,7);
  while(Wire.available())
  {
    byte val=Wire.read();
    if(i==2)
    {
      reading=val;
      reading=reading<<8;
    }
    if(i==3)
    {
      reading=reading|val;
    }
    i=i+1;
  }
  //CO2val=reading/4096.0*2000.0;
  CO2val=reading;
  return CO2val;
}

void Evac()
{
    digitalWrite(INPUT_VALVE_PIN,HIGH);
    delay(500);
    digitalWrite(EVAC_PIN,HIGH);
    delay(EVAC_TIME);
    digitalWrite(INPUT_VALVE_PIN,LOW);
    digitalWrite(EVAC_PIN,LOW);
    delay(2000);
}

int CatalystFlow(int flag)
{
 if (flag == 0)
 {
  digitalWrite(UPPER_VALVE_PIN,LOW);
  digitalWrite(LOWER_VALVE_PIN,LOW);
 }
 else if (flag == 1)
 {
  digitalWrite(UPPER_VALVE_PIN,HIGH);
  digitalWrite(LOWER_VALVE_PIN,HIGH);
 }
 return flag;
}
7  Using Arduino / Microcontrollers / Re: Arduino UNO microcontroller type? on: June 04, 2012, 11:49:39 am
Ah, that's what I had suspected... thanks!
8  Using Arduino / Microcontrollers / Arduino UNO microcontroller type? on: June 03, 2012, 08:55:37 am
Hi all,
  I was talking a look at my arduino uno, and it seems that there are in fact two atmel chips on the board?  There is the large chip, which I assume is the main microcontroller, but there is also a small, surface-mount chip (mega 18U2 I think).  What does this second chip do??  Does it contribute to the main chip's function? 
Thanks!
9  Using Arduino / Programming Questions / Re: Arduino script and SD card library? on: March 13, 2012, 09:53:16 pm
Great! Thanks for all the feedback.  Keeping an eye on available memory is certainly a good motivator to code better.  I've cleaned up alot of the slop and I can now run the code with a good bit of extra RAM (I used the memoryfree.h library for this).  I didn't get around to doing it, but if I expand the code some more and memory once again becomes an issue, then I will probably store most of the long strings (like the file header, etc...) in the program memory, and just poll it when needed.  using #if, #end if is certainly a good idea as well; no use in having the extra stuff floating around if ECHO_DEBUG is false...

Two questions still as to how I could do the following:
(1)
Quote
Defining a compile time variable to determine if the code is generated or not.
The arduino GUI will usually tell you when the code compiles incorrectly, but with my running-out-of-RAM problem, the GUI would say everything would compile correctly, but still not work.  Is 'defining a compile time variable' a way to check for deeper errors?

(2)
Quote
You can look at the assembler output for the two cases.
  Are there any good programs that will help to look at the assembler output/machine code, and show you where stuff is coming from/going?  I can imagine such a tool would be very useful in understanding the guts of a compiler, or coding in general.

Thanks again for all the tips!
-Nick
10  Using Arduino / Programming Questions / Re: Arduino script and SD card library? on: March 09, 2012, 09:36:50 am
Ah, okay... I see how the pointer will keep pointing to a permanent place in memory regardless of the local function etc...

Why then would the following string not be deleted when the void loop() exits and returns:
Code:
void loop()
{
Serial.print("Hello World");
}

It seems like from what you're saying (in previous comments), the Serial.print function will create a "hello world' string, and even after it has printed, and loop has exited and returned to the beginning, the "hello world" string is still in memory...  If the program calls Serial.print("Hello World") a second time, will it create another "hello world" string in RAM, or go back to the first one, or is the string "hello world" indeed deleted and recreated in memory each iteration?  I'm just confused about how the Serial.print() function operates when you give it a string directly.  So would there be a difference between the above code, and the following code concerning how the 'string' array is saved (or deleted from memory).
Code:
void loop()
{
char to_print[]="Hello World";
Serial.print(to_print);
}

Is this code treated differently (in as far as memory) than if I pass "Hello World" directly to Serial.print()?  Would this code clear the character array to_print[] from memory after the loop() function has finished executing?
11  Using Arduino / Programming Questions / Re: Arduino script and SD card library? on: March 08, 2012, 05:46:52 pm
Why is it that locally declared strings are not wiped from RAM, whereas locally declared ints, etc... are cleared?  (This also applies to arrays, right?  so character arrays, i.e. strings are not cleared, whereas integer arrays locally declared are cleared after the function has exited?). Is this just a facet of the C programming language?

Thanks,
-Nick
12  Using Arduino / Programming Questions / Re: Arduino script and SD card library? on: March 08, 2012, 12:48:25 pm
Thank you!  I didn't quite get your first response; you say that local variables persist
Quote
for the life of the function call
.  Is that to say that the RAM to which local variables are stored is freed up when the function has completed (I assume so otherwise risk memory leaks...).  If this is true, I would assume it also applies to the main 'void loop()'; for example:
Code:
int first_var;

void setup()
{
}

void loop()
{
function1();

function2();

Serial.print("Hello World");

function3();
}

Correct me if i'm wrong, but I believe the following is true:
1. loop begins, string "Hello World" does not exist in RAM memory
2. function1() and function2() are executed, "Hello World" still not occupying RAM
3. THEN the string HELLO WORLD get created and enters RAM
4. function3() executes, HELLO WORLD still in RAM
5. loop returns, all variables (including HELLO WORLD) wiped out, and function begins again

If this is not the case, and local variables exist after their parent functions finish, then perhaps my code would benefit from dynamic allocation and 'delete *pointer' to clear up memory that would be used by a function later down the line?  I've never worried much about memory, but this seems like a good opportunity to learn about memory allocation and become a better programmer!
13  Using Arduino / Programming Questions / Re: Arduino script and SD card library? on: March 08, 2012, 09:32:10 am
To lay out a few questions for succinctly:

1. when a variable is declared locally in a function, is it written from flash to RAM, used, then wiped out afterwards
2. do global variables reside in RAM whenever the program is running
3. all variables must be written in flash somewhere, right?  RAM does not persist when the power is off, so I assume that at bootup variables are written from flash to RAM
4.  When you use a function like Serial.print("Hello World"); is the string "Hello World" created as a global variable string that persists in RAM?  Or is it created when the function is called, then wiped out of RAM after it is used?
14  Using Arduino / Programming Questions / Re: Arduino script and SD card library? on: March 08, 2012, 09:19:50 am
Okay, great!  I've been reading a little bit about variable memory allocation, etc... and it isn't clear to me exactly how variables stored in flash are manipulated.  If I use F(), and the string/integer/whatever is constant, then the Serial.print(F("...")) will just write the string from flash memory.  If, however, it is a variable, and I use the progmem functions, then doesn't the variable (stored in Flash) have to be 'loaded' in to RAM to be manipulated, then stored back in flash after manipulation?  Is this how the controller/flash program/machine code carries out the process? (or does a progmem stored variable never "leave" the flash memory?).

I only ask because, if that is the case, then the amount of RAM being used will vary depending on the process and which variables are being read/written from flash to RAM.  If so, then wouldn't your RAM needs change throughout a program execution?  I also haven't looked in to the 'freemem' functions yet, but will they tell you how much RAM is being used THROUGHOUT a program execution (so you can inspect the points in the code when max RAM is being used, and make sure the max is not being exceeded?)

If anybody has a link to some literature on how variables/data are treated (passed from flash to RAM, manipulated etc...) on a low down level, i'd really appreciate to read some more.  It's hard to find some of the fundamentals well laid out on the web; still trying to wrap my head around the whole machine level process.
Thanks alot, -Nick
15  Using Arduino / Programming Questions / Re: Arduino script and SD card library? on: March 07, 2012, 10:57:15 pm
Ah... it appears that any variable will be in SRAM as it is manipulated throughout the program execution?  Correct?  So perhaps I could also save some SRAM by using #define and const on some of my constants?
Pages: [1] 2 3