Advertisement
AlexShu

Saving & Loading Settings on SD Card with Arduino

Apr 9th, 2015
19,765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.28 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.  
  12.  #include <SPI.h>
  13.  #include <SD.h>
  14.  
  15.  File myFile;
  16.  int exINT = 15;
  17.  float exFloat = 1.12345;
  18.  boolean exBoolean = true;
  19.  long exLong = 2123456789;
  20.  
  21.  void setup()
  22.  {
  23.  // Open serial communications and wait for port to open:
  24.  Serial.begin(9600);
  25.  while (!Serial) {
  26.  ; // wait for serial port to connect. Needed for Leonardo only
  27.  }
  28.  Serial.print("Initializing SD card...");
  29.  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  30.  // Note that even if it's not used as the CS pin, the hardware SS pin
  31.  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  32.  // or the SD library functions will not work.
  33.  pinMode(10, OUTPUT);
  34.  if (!SD.begin(4)) {
  35.  Serial.println("initialization failed!");
  36.  return;
  37.  }
  38.  Serial.println("initialization done.");
  39.  writeSDSettings();
  40.  readSDSettings();
  41.  
  42.  /*
  43.  //Debuuging Printing
  44.  Serial.println("In RAM Memory");
  45.  Serial.print("exINT=");
  46.  Serial.println(exINT);
  47.  Serial.print("exFloat=");
  48.  Serial.println(exFloat,10);
  49.  Serial.print("exBoolean=");
  50.  Serial.println(exBoolean);
  51.  Serial.print("exLong=");
  52.  Serial.println(exLong);
  53.  */
  54.  }
  55.  
  56.  void loop()
  57.  {
  58.  }
  59.  
  60.  void readSDSettings(){
  61.  char character;
  62.  String settingName;
  63.  String settingValue;
  64.  myFile = SD.open("settings.txt");
  65.  if (myFile) {
  66.  while (myFile.available()) {
  67.  character = myFile.read();
  68.  while((myFile.available()) && (character != '[')){
  69.  character = myFile.read();
  70.  }
  71.  character = myFile.read();
  72.  while((myFile.available()) && (character != '=')){
  73.  settingName = settingName + character;
  74.  character = myFile.read();
  75.  }
  76.  character = myFile.read();
  77.  while((myFile.available()) && (character != ']')){
  78.  settingValue = settingValue + character;
  79.  character = myFile.read();
  80.  }
  81.  if(character == ']'){
  82.  
  83.  /*
  84.  //Debuuging Printing
  85.  Serial.print("Name:");
  86.  Serial.println(settingName);
  87.  Serial.print("Value :");
  88.  Serial.println(settingValue);
  89.  */
  90.  
  91.  // Apply the value to the parameter
  92.  applySetting(settingName,settingValue);
  93.  // Reset Strings
  94.  settingName = "";
  95.  settingValue = "";
  96.  }
  97.  }
  98.  // close the file:
  99.  myFile.close();
  100.  } else {
  101.  // if the file didn't open, print an error:
  102.  Serial.println("error opening settings.txt");
  103.  }
  104.  }
  105.  
  106.  /* Apply the value to the parameter by searching for the parameter name
  107.  Using String.toInt(); for Integers
  108.  toFloat(string); for Float
  109.  toBoolean(string); for Boolean
  110.  toLong(string); for Long
  111.  */
  112.  void applySetting(String settingName, String settingValue) {
  113.  if(settingName == "exINT") {
  114.  exINT=settingValue.toInt();
  115.  }
  116.  if(settingName == "exFloat") {
  117.  exFloat=toFloat(settingValue);
  118.  }
  119.  if(settingName == "exBoolean") {
  120.  exBoolean=toBoolean(settingValue);
  121.  }
  122.  if(settingName == "exLong") {
  123.  exLong=toLong(settingValue);
  124.  }
  125.  }
  126.  
  127.  // converting string to Float
  128.  float toFloat(String settingValue){
  129.  char floatbuf[settingValue.length()+1];
  130.  settingValue.toCharArray(floatbuf, sizeof(floatbuf));
  131.  float f = atof(floatbuf);
  132.  return f;
  133.  }
  134.  
  135.  long toLong(String settingValue){
  136.  char longbuf[settingValue.length()+1];
  137.  settingValue.toCharArray(longbuf, sizeof(longbuf));
  138.  long l = atol(longbuf);
  139.  return l;
  140.  }
  141.  
  142.  // Converting String to integer and then to boolean
  143.  // 1 = true
  144.  // 0 = false
  145.  boolean toBoolean(String settingValue) {
  146.  if(settingValue.toInt()==1){
  147.  return true;
  148.  } else {
  149.  return false;
  150.  }
  151.  }
  152.  
  153.  // Writes A Configuration file
  154.  void writeSDSettings() {
  155.  // Delete the old One
  156.  SD.remove("settings.txt");
  157.  // Create new one
  158.  myFile = SD.open("settings.txt", FILE_WRITE);
  159.  // writing in the file works just like regular print()/println() function
  160.  myFile.print("[");
  161.  myFile.print("exINT=");
  162.  myFile.print(exINT);
  163.  myFile.println("]");
  164.  myFile.print("[");
  165.  myFile.print("exFloat=");
  166.  myFile.print(exFloat,5);
  167.  myFile.println("]");
  168.  myFile.print("[");
  169.  myFile.print("exBoolean=");
  170.  myFile.print(exBoolean);
  171.  myFile.println("]");
  172.  myFile.print("[");
  173.  myFile.print("exLong=");
  174.  myFile.print(exLong);
  175.  myFile.println("]");
  176.  // close the file:
  177.  myFile.close();
  178.  //Serial.println("Writing done.");
  179.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement