Pages: [1]   Go Down
Author Topic: Correct array type and SDFat saving.  (Read 1000 times)
0 Members and 1 Guest are viewing this topic.
Greenville, IL
Offline Offline
Edison Member
*
Karma: 11
Posts: 1310
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 I have a two questions. I have at least 2 data types that I would like to put into an array then save them to a SD card.

  First question, how should I define my array called "Datavalues" to store the floats and ints. I decided to go with float but, I don't want to waste space if something else would be better. I will have 25 values to store when I get it all figured out.
 
 Second question can I save to an SD card using a "for" loop with my array?  I am worried that I might have a problem with data types for SD card saving. I don't know if everything has to be a char, int, byte, etc.  Also, if it matters, I would like to save to my card in CSV format.

Code:
int RPM = 999;                //variable for RPM reading
int Throttle = 0;             //Throttle postion
float Battery  = 0.00;        //battery voltage
int Gear = 0;                 //gear position
int Advance = 0;              //ignition advance
float AF = 0.00;              //air fuel ratio
int IAP = 0;                  //value for map
int CLT = 0;                  //Coolant temp
int stp = 0;                  //secondary throttle postiton
float Datavalue[10] = {RPM, Throttle, Battery, Gear, Advance, AF, IAP, CLT, stp};
Logged


Offline Offline
Edison Member
*
Karma: 35
Posts: 1429
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I would like to save to my card in CSV format.
That means that you have to output all the values as ASCII strings with commas between them so it doesn't matter whether the variables are char, int or float. They'll all end up as strings anyway.

Pete
Logged

Where are the Nick Gammons of yesteryear?

Greenville, IL
Offline Offline
Edison Member
*
Karma: 11
Posts: 1310
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I would like to save to my card in CSV format.
That means that you have to output all the values as ASCII strings with commas between them so it doesn't matter whether the variables are char, int or float. They'll all end up as strings anyway.

Pete

 That does simplify the second question, thank you for that!

 Did I choose the right type for my array to contain the values of int and float?   I may need to learn how to create ASCII strings from my values. I will start that tomorrow.
Logged


Offline Offline
Edison Member
*
Karma: 35
Posts: 1429
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, I somehow didn't see that last line with the array.
You don't need the Datavalue array at all, and the way you've written it wouldn't work anyway.

Pete
Logged

Where are the Nick Gammons of yesteryear?

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

Quote
I may need to learn how to create ASCII strings from my values.
Why? The SD library already knows how.
Logged

Greenville, IL
Offline Offline
Edison Member
*
Karma: 11
Posts: 1310
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


  Here is a minimal sketch to show the only way I understand to save my values to SDcard. For this example, I am saving the defined values rather than showing the 500 lines of code it takes to update the values. Keep in mind I will have 25 changing values, (rather than 9) in my final sketch.

Is there a better way to write to the card? I would like to use a for loop instead of the redundant write and print statements.

Code:
#include <SdFat.h>

// file system
SdFat sd;

// log file
SdFile file;

// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))
///////////////////////variables////////////////////////////
int RPM = 999;                //variable for RPM reading
int Throttle = 0;             //Throttle postion
float Battery  = 0.00;        //battery voltage
int Gear = 0;                 //gear position
int Advance = 0;              //ignition advance
float AF = 0.00;              //air fuel ratio
int IAP = 0;                  //value for map
int CLT = 0;                  //Coolant temp
int stp = 0;                  //secondary throttle postiton
//------------------------------------------------------------------------------
void setup(void) {
  Serial.begin(9600);

  // breadboards.  use SPI_FULL_SPEED for better performance.
  if (!sd.init(SPI_FULL_SPEED)) sd.initErrorHalt();

  // create a new file
  char name[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    name[6] = i/10 + '0';
    name[7] = i%10 + '0';
    if (file.open(name, O_CREAT | O_EXCL | O_WRITE)) break;
  }

  if (!file.isOpen()) error ("file.create");

  // write header
  file.writeError = 0;
  file.print("millis");
  file.println(); 

  if (file.writeError || !file.sync()) {
    error("write header failed");
  }
}
//------------------------------------------------------------------------------
void loop(void) {
  uint32_t m;

  m = millis();

  // log time
  file.print(m); 

  // add sensor data
  file.write(',');
  file.print(RPM);
  file.write(',');
  file.print(Throttle);
  file.write(',');
  file.print(Battery);
  file.write(',');
  file.print(Gear);
  file.write(',');
  file.print(Advance);
  file.write(',');
  file.print(AF);
  file.write(',');
  file.print(IAP);
  file.write(',');
  file.print(CLT);
  file.write(',');
  file.print(stp);

  file.println(); 

  if (file.writeError) error("write data failed");

  if (!file.sync()) error("sync failed");
}
Logged


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

Quote
Here is a minimal sketch to show the only way I understand to save my values to SDcard.
Code:
  file.print("millis");
  file.println(); 
So, we need to trace backwards to see what this is doing. file is an instance of the SdFile class, as shown here:
Code:
// log file
SdFile file;

According to SdFile.h:
Code:
class SdFile : public SdBaseFile, public Print {
This means that the SdFile class derives from the Print class, which is the same class that Serial and a number of other classes derives from.

What it means is that SdFile knows how to convert ints, floats, longs, and a number of other types to strings, for writing to the file.

Quote
I would like to use a for loop instead of the redundant write and print statements.
Why? Put all that code in a function, and call the function. Copying all the values to an array so that you can iterate over the array would require nearly the same number of statements. If the variables were all of the same type, that might make sense. Since they are not, it does not.
Logged

Pages: [1]   Go Up
Jump to: