Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Saving & Loading Settings on SD Card with Arduino by Alex Shu
- http://overskill.alexshu.com/saving-loading-settings-on-sd-card-with-arduino/
- SD card connections:
- ** MOSI - pin 11
- ** MISO - pin 12
- ** CLK - pin 13
- ** CS - pin 4
- This Is an altered version by request for float only.
- */
- #include <SPI.h>
- #include <SD.h>
- File myFile;
- float kiloWatts = 0.001;
- const int decimal_point = 3;
- void setup()
- {
- // Open serial communications and wait for port to open:
- Serial.begin(9600);
- while (!Serial) {
- ; // wait for serial port to connect. Needed for Leonardo only
- }
- Serial.print("Initializing SD card...");
- // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
- // Note that even if it's not used as the CS pin, the hardware SS pin
- // (10 on most Arduino boards, 53 on the Mega) must be left as an output
- // or the SD library functions will not work.
- pinMode(10, OUTPUT);
- if (!SD.begin(4)) {
- Serial.println("initialization failed!");
- return;
- }
- Serial.println("initialization done.");
- readSDSettings();
- /*
- //Debuuging Printing
- Serial.println("In RAM Memory");
- Serial.print("kiloWatts=");
- Serial.println(kiloWatts,decimal_point);
- */
- }
- void loop()
- {
- // ##########################################################################
- // ########## SAVE YOUR PARAMETERS HERE - call writeSDSettings(); ########
- // ##########################################################################
- // writeSDSettings();
- }
- void readSDSettings(){
- char character;
- String settingName;
- String settingValue;
- myFile = SD.open("kwcounter.txt");
- if (myFile) {
- while (myFile.available()) {
- character = myFile.read();
- while((myFile.available()) && (character != '[')){
- character = myFile.read();
- }
- character = myFile.read();
- while((myFile.available()) && (character != '=')){
- settingName = settingName + character;
- character = myFile.read();
- }
- character = myFile.read();
- while((myFile.available()) && (character != ']')){
- settingValue = settingValue + character;
- character = myFile.read();
- }
- if(character == ']'){
- /*
- //Debuuging Printing
- Serial.print("Name:");
- Serial.println(settingName);
- Serial.print("Value :");
- Serial.println(settingValue);
- */
- // Apply the value to the parameter
- applySetting(settingName,settingValue);
- // Reset Strings
- settingName = "";
- settingValue = "";
- }
- }
- // close the file:
- myFile.close();
- } else {
- // if the file didn't open, print an error:
- Serial.println("error opening kwcounter.txt");
- }
- }
- /* Apply the value to the parameter by searching for the parameter name
- toFloat(string); for Float
- */
- void applySetting(String settingName, String settingValue) {
- if(settingName == "kiloWatts") {
- kiloWatts=toFloat(settingValue);
- }
- }
- // converting string to Float
- float toFloat(String settingValue){
- char floatbuf[settingValue.length()+1];
- settingValue.toCharArray(floatbuf, sizeof(floatbuf));
- float f = atof(floatbuf);
- return f;
- }
- // Writes A Configuration file
- void writeSDSettings() {
- // Delete the old One
- SD.remove("kwcounter.txt");
- // Create new one
- myFile = SD.open("kwcounter.txt", FILE_WRITE);
- // writing in the file works just like regular print()/println() function
- myFile.print("[");
- myFile.print("kiloWatts=");
- myFile.print(kiloWatts,decimal_point);
- myFile.println("]");
- // close the file:
- myFile.close();
- //Serial.println("Writing done.");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement