Pages: [1]   Go Down
Author Topic: Analog Inputs Causing Program to Stop  (Read 1058 times)
0 Members and 1 Guest are viewing this topic.
Washington State
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Washington State
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49237
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Washington State
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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
« Last Edit: July 05, 2012, 02:47:11 pm by AWOL » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

Washington State
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18764
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote
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?
Logged


Washington State
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18764
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Washington State
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18764
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Exactly. Change:

Code:
dataFile.println(dataString);

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


Washington State
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18764
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The suggested change would not affect that at all.
Logged


Washington State
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
/*
  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)
« Last Edit: July 06, 2012, 02:46:58 am by Nick Gammon » Logged

Pages: [1]   Go Up
Jump to: