//read out 5 slider values and store them on the SD card
//read out the data from SD card as int or string
//this might be useful if you want to save data before arduino is shut down
//and reload data at new start up
#include <SD.h>
#include <SPI.h>
#define chipSelect 10
#define saveButton 2 //yellow
#define loadButton 3 //green
#define deleteButton 4 //blue
#define printVariablesButton 5 //gray
#define MAX_MESSAGE_LENGTH 12
#define NUMBER_OF_PARAMETERS 5
#define slider1 A0 //green
#define slider2 A1 //braun
#define slider3 A2 //orange
#define slider4 A3 //purple
#define slider5 A4 //white
int parameter[NUMBER_OF_PARAMETERS];
void initializeSDcard(){ //function for initializion of the SD card
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized.");
}
void setup(){
pinMode(saveButton, INPUT_PULLUP); //set pins to input and add software pullup
pinMode(loadButton, INPUT_PULLUP);
pinMode(deleteButton, INPUT_PULLUP);
pinMode(printVariablesButton, INPUT_PULLUP);
Serial.begin(9600);
while (!Serial) {delay(10);}// wait for serial port to connect.
initializeSDcard(); //initialze the SD card
}
void saveDataToSD(int parameter_pos, int value){ //function to save data to sd
deleteDataFromSD(); //before save new data to sd card delete old file
String dataString = "";
parameter[parameter_pos]=value;
for(int i = 0; i<NUMBER_OF_PARAMETERS; i++){
dataString = String(parameter[i]);
File dataFile = SD.open("datalog.txt", FILE_WRITE); // open the file named datalog.txt on the sd card
if (dataFile) { // if the file is available, write the contents of datastring to it
dataFile.println(dataString);
dataFile.close();
Serial.println("data saved!");
}else { // if the file isn't open, pop up an error:
Serial.println("error opening datalog.txt");
}
}
}
void loadDataFromSD(){ //function to read out sd card as string and print
Serial.println("load data");
File dataFile = SD.open("datalog.txt"); //open up datalog2.txt and then print all of its contents
if(dataFile) {
Serial.println("datalog:");
while (dataFile.available()) {
Serial.write(dataFile.read());
}
dataFile.close(); // close the file:
}else{
Serial.println("error opening datalog.txt"); // if the file didn't open, print an error:
}
}
void deleteDataFromSD(){ //function to delete the file datalog.txt
SD.remove("datalog.txt");
Serial.println("data deleted");
}
int readoutSDAsInt(int i){ //function to readout sd card and return data at line i as int
int parameter_pos = 0;
File dataFile = SD.open("datalog.txt");
if(dataFile){
while (dataFile.available()){
static char message[MAX_MESSAGE_LENGTH];//create a place to hold the incoming message
static unsigned int message_pos = 0;
char inByte = dataFile.read();//read the next available byte in the serial receive buffer
if(inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1)){//check not terminating character
message[message_pos] = inByte;//add the incoming byte to our message
message_pos++;
}else{
message[message_pos] = '\0';//add nul charakter to string
parameter[parameter_pos] = atoi(message);
message_pos = 0;//reset for the next message
parameter_pos++;
}
}
dataFile.close();
}else{
Serial.println("error opening datalog.txt"); // if the file didn't open, print an error:
}
return parameter[i];
}
void loop(){
delay(100);
if(digitalRead(saveButton) == LOW) { //if the save button is low, read out all sliders and save to sd card
saveDataToSD(0,analogRead(slider1));
saveDataToSD(1,analogRead(slider2));
saveDataToSD(2,analogRead(slider3));
saveDataToSD(3,analogRead(slider4));
saveDataToSD(4,analogRead(slider5));
Serial.println("saved all sliders");
}
if(digitalRead(loadButton) == LOW) {loadDataFromSD();}
if(digitalRead(deleteButton)==LOW) {deleteDataFromSD();}
if(digitalRead(printVariablesButton)==LOW){
Serial.println("Readout >parameters[]<:");
for(int i = 0; i<NUMBER_OF_PARAMETERS; i++){
Serial.print("Slider ");
Serial.print(i+1);
Serial.print(": ");
Serial.println(readoutSDAsInt(i));
}
}
}