Analog Inputs Causing Program to Stop

I'm experiencing a problem with the analog inputs in that certain values are causing the program to stop. The program is the Datalogger at http://arduino.cc/en/Tutorial/Datalogger and I've wired it as shown in the schematic on that page. It works find except when the pot at A0 is adjusted to 0 or the pot at A1 is at 1000 or higher. Electrically, this would represent 0 volts and 5 volts, respectively. From what I've read, the high impedance of the analog inputs don't need input resistors and operation should not be affected by these values.

Other programs using these inputs have not exhibited this problem which would imply that it's a software issue. Not sure what's going on here but I didn't see anything in the code that would do it. Any help would be appreciated.

OK, I figured it out. The program uses a "for" loop for creating a string. I simply added a space after the comma.

From: dataString += ",";
To: dataString += ", ";

Removing the space causes the problem to reappear every time.
I'm new at programming so would appreciate if someone could explain why this works.

OK, I figured it out. The program uses a "for" loop for creating a string. I simply added a space after the comma.

From: dataString += ",";
To: dataString += ", ";

Removing the space causes the problem to reappear every time.
I'm new at programming so would appreciate if someone could explain why this works.

Post all of your code, and we'll have a look. Putting all the data into one String object before writing to the file is completely unnecessary, by the way. Writing to the file (buffer) one value at a time is more efficient.

Here is the code. It is working now, just not sure why it requires a space after the comma in statement
dataString += ", ";

#include <SD.h>

  // make a string for assembling the data to log:
  String dataString = 0;
  
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;
//digitalWrite(4,HIGH);//disables SD chip

void setup()
{
 // Open serial communications and wait for port to open:
 // start the SPI library:


  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
digitalWrite(10,HIGH); // disables w5100 
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{

  String dataString = 0;
  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ", "; 
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
  delay(500);
}

Moderator edit: code tags

lawren5:
Here is the code. It is working now, just not sure why it requires a space after the comma in statement

That does sound odd, but since you haven't mentioned what problem it causes I couldn't guess why it's happening.

The problem is described in the first post of this topic.

certain values are causing the program to stop

Programs don't stop you know. They can't. They may be doing something other than what you expect.

It works find except when the pot at A0 is adjusted to 0 or the pot at A1 is at 1000 or higher.

You might know what "it works" means, but to us that has no meaning. "It works" by .... sending data? Flashing an LED? Outputting some numbers rather than other ones?

My apology for not being specific. I'm not an experienced programmer as you've probably guessed by now.
Anyway, when certain values appear at the analog inputs, the program stops writing data to the SD card. Returning the input values to within the normally working range does not restart the writing process.

The String class is notorious for fragmenting memory. Your extra space on the face of it should not cause an issue, but perhaps it is altering the way the memory fragments. I suggest changing the code to avoid using String.

Thanks, Nick. I hate to change anything since the program is working fine with the space there. If I avoid using strings as you suggest, does that mean I should write just one value at a time?

PaulS suggests using a buffer although I'm not sure how to go about doing that at this point.

lawren5:
The problem is described in the first post of this topic.

It wasn't actually, but I see you've described it now. Nick Gammon's suggestion seems pretty likely to me. To avoid using String you can simply write your values directly to the file instead of appending them to a String and then writing the String to the file.

Exactly. Change:

dataFile.println(dataString);

... to print the items (one by one if necessary, you can use print rather than println).

Thanks PeterH and Nick. I appreciate your support.
I'm going to stick with the string as long as it's working because it makes it easy to drop the data into an Excel spreadsheet.
Regards,
Lawren5

The suggested change would not affect that at all.

I think I get it now. I rewrote the program without using the STRING command but still writes to the SD card in the same format.
Thanks for pointing this out to me, Nick.

/*
  THIS PROGRAM WRITES DATA TO AN SD CARD WITHOUT USING
  THE STRING COMMAND.
 */

#include <SD.h>
// On the Ethernet Shield, CS is pin 4. 
const int chipSelect = 4;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
digitalWrite(10,HIGH); // disables w5100 
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{
 File dataFile = SD.open("datalog.txt", FILE_WRITE);
 if (dataFile) {
  // read three sensors 
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
      dataFile.print(sensor) ;
        Serial.print(sensor);
        if (analogPin < 2) {  
              dataFile.print(", ") ;
        Serial.print(", ");  
        }    
      else { 
  
       dataFile.println();
        Serial.println();  
          dataFile.close();  
      }       
    }
 }
 
         else {
    Serial.println("error opening datalog.txt");

  }
  delay(500);
}

Moderator edit: [code] ... [/code] tags added. (Nick Gammon)