Advertisement
AlexShu

[Float Only] Saving & Loading Settings on SD Card Arduino

Mar 20th, 2016
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.47 KB | None | 0 0
  1. /*
  2.   Saving & Loading Settings on SD Card with Arduino by Alex Shu
  3.   http://overskill.alexshu.com/saving-loading-settings-on-sd-card-with-arduino/
  4.  
  5.   SD card connections:
  6.  ** MOSI - pin 11
  7.  ** MISO - pin 12
  8.  ** CLK - pin 13
  9.  ** CS - pin 4
  10.  
  11.  This Is an altered version by request for float only.
  12.  */
  13.  
  14.  #include <SPI.h>
  15.  #include <SD.h>
  16.  
  17.  File myFile;
  18.  float kiloWatts = 0.001;
  19.  const int decimal_point = 3;
  20.  
  21.  
  22.  void setup()
  23.  {
  24.  // Open serial communications and wait for port to open:
  25.  Serial.begin(9600);
  26.  while (!Serial) {
  27.  ; // wait for serial port to connect. Needed for Leonardo only
  28.  }
  29.  Serial.print("Initializing SD card...");
  30.  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  31.  // Note that even if it's not used as the CS pin, the hardware SS pin
  32.  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  33.  // or the SD library functions will not work.
  34.  pinMode(10, OUTPUT);
  35.  if (!SD.begin(4)) {
  36.  Serial.println("initialization failed!");
  37.  return;
  38.  }
  39.  Serial.println("initialization done.");
  40.  readSDSettings();
  41.  
  42.  /*
  43.  //Debuuging Printing
  44.  Serial.println("In RAM Memory");
  45.  Serial.print("kiloWatts=");
  46.  Serial.println(kiloWatts,decimal_point);
  47.  
  48.  */
  49.  }
  50.  
  51.  void loop()
  52.  {
  53.     // ##########################################################################
  54.     // ##########   SAVE YOUR PARAMETERS HERE - call writeSDSettings();  ########
  55.     // ##########################################################################
  56.  
  57.  
  58.         // writeSDSettings();
  59.  
  60.  }
  61.  
  62.  void readSDSettings(){
  63.  char character;
  64.  String settingName;
  65.  String settingValue;
  66.  myFile = SD.open("kwcounter.txt");
  67.  if (myFile) {
  68.  while (myFile.available()) {
  69.  character = myFile.read();
  70.  while((myFile.available()) && (character != '[')){
  71.  character = myFile.read();
  72.  }
  73.  character = myFile.read();
  74.  while((myFile.available()) && (character != '=')){
  75.  settingName = settingName + character;
  76.  character = myFile.read();
  77.  }
  78.  character = myFile.read();
  79.  while((myFile.available()) && (character != ']')){
  80.  settingValue = settingValue + character;
  81.  character = myFile.read();
  82.  }
  83.  if(character == ']'){
  84.  
  85.  /*
  86.  //Debuuging Printing
  87.  Serial.print("Name:");
  88.  Serial.println(settingName);
  89.  Serial.print("Value :");
  90.  Serial.println(settingValue);
  91.  */
  92.  
  93.  // Apply the value to the parameter
  94.  applySetting(settingName,settingValue);
  95.  // Reset Strings
  96.  settingName = "";
  97.  settingValue = "";
  98.  }
  99.  }
  100.  // close the file:
  101.  myFile.close();
  102.  } else {
  103.  // if the file didn't open, print an error:
  104.  Serial.println("error opening kwcounter.txt");
  105.  }
  106.  }
  107.  
  108.  /* Apply the value to the parameter by searching for the parameter name
  109.  toFloat(string); for Float
  110.  */
  111.  void applySetting(String settingName, String settingValue) {
  112.  if(settingName == "kiloWatts") {
  113.  kiloWatts=toFloat(settingValue);
  114.  }
  115.  }
  116.  
  117.  // converting string to Float
  118.  float toFloat(String settingValue){
  119.  char floatbuf[settingValue.length()+1];
  120.  settingValue.toCharArray(floatbuf, sizeof(floatbuf));
  121.  float f = atof(floatbuf);
  122.  return f;
  123.  }
  124.  
  125.  // Writes A Configuration file
  126.  void writeSDSettings() {
  127.  // Delete the old One
  128.  SD.remove("kwcounter.txt");
  129.  // Create new one
  130.  myFile = SD.open("kwcounter.txt", FILE_WRITE);
  131.  // writing in the file works just like regular print()/println() function
  132.  myFile.print("[");
  133.  myFile.print("kiloWatts=");
  134.  myFile.print(kiloWatts,decimal_point);
  135.  myFile.println("]");
  136.  // close the file:
  137.  myFile.close();
  138.  //Serial.println("Writing done.");
  139.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement