Go Down

Topic: Correct array type and SDFat saving. (Read 1 time) previous topic - next topic

cyclegadget


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: [Select]
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};
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

el_supremo

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

cyclegadget


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.
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

el_supremo

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

PaulS

Quote
I may need to learn how to create ASCII strings from my values.

Why? The SD library already knows how.

cyclegadget


  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: [Select]
#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");
}
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

PaulS

Quote
Here is a minimal sketch to show the only way I understand to save my values to SDcard.

Code: [Select]
  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: [Select]
// log file
SdFile file;


According to SdFile.h:
Code: [Select]
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.

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview