DataLogger Example Error

Moderators: lily.li, violet, salmanfarisvp

Post Reply
mdupree
Pre-kindergarten
Pre-kindergarten
Posts: 2
Joined: Wed Mar 11, 2015 4:01 am

DataLogger Example Error

Post by mdupree » Wed Mar 11, 2015 4:38 am

There is a small error in the DataLogger Example.
The A/D Channel are A0 to A2 not 0 to 2.
Here is the original code

/*
SD card datalogger

This example shows how to log data from three analog sensors
to an SD card using the SD library.

created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe


port to LinkIt ONE
by Loovee
2014-10-12

This example code is in the public domain.

*/

#include <LTask.h>
#include <LFlash.h>
#include <LSD.h>
#include <LStorage.h>


#define Drv LFlash // use Internal 10M Flash
// #define Drv LSD // use SD card



void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);

while(!Serial.available()); // input any thing to start


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);

// see if the card is present and can be initialized:
LTask.begin();
Drv.begin();
Serial.println("card initialized.");
}

void loop()
{
// make a string for assembling the data to log:
String dataString = "";

// 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.
LFile dataFile = Drv.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");
}
}


this section:
// 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 += ",";
}
}
needs to be:
// read three sensors and append to the string:
for (int analogPin = A0; analogPin < A2+1; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < A2) {
dataString += ",";
}
}

I used A2+1 in the event A3 is not defined.
The code does run, how ever it does not read the correct voltage, if the pins have an input,
For my test I used a 1000 ohm resistor between the +5v pin and A0 and a 1000 ohm resistor between the 3.3 volts and A2;

Maybe this will help another beginner.
Mike

User avatar
loovee
Elementary-3
Elementary-3
Posts: 352
Joined: Fri Nov 02, 2012 9:15 am

Re: DataLogger Example Error

Post by loovee » Tue Mar 17, 2015 10:57 am

Thanks mike, this is helphul. :)

Post Reply