Hi Everyone. I hope someone could help me with reading a csv line into some variables. This has been driving me mad for about 2 weeks with no joy, so I thought i would seeks some help from the forum.
The below code is working on a Arduino Mega. Using Pin 10 for Chipselect with SPI SDCARD. All working great. What i am trying to achieve is plain and simple im sure for most coders. Im not a great coder at all and why im stuck.
-
All i need to do is if int weekSchedule = eg. 7, then i need to load the SCHEDULE.csv file and read line 7 data into the array and then add the data to variables. currently the code is not working this way and is instead given a number eg. 23 in the csv file so it knows to read in 23 values which currently is only loading line 1. I would rather this read in the whole line and not have to declare the 23 in the csv file at all.
-
Now at the moment i have 2 strings
String desiredECTotal and String desiredPHTotal and i would like these to be floats 1.8 and 5.7. not sure how to code this.
currently im updating the data from the array values to strings:
desiredECTotal = String(values[8]) + "." + String(values[9]);
desiredPHTotal = String(values[10]) + "." + String(values[11]);
This requires 4 values 8,9,10,11 as the DOT in the csv eg. 1.8,5.7 is thinking the DOT is a delimiter and causing it to be 2 values!
Now i see a new problem going to happen. if we read in a full line eg 23,1,18,27,0,60,0,0,0,1.8,5.7,27,100,7,24,20,11,13,1000,250,1,24 // < problem here in that the csv has two float values 1.8,5.7, and i dont want these as strings i need these as floats. also what happens if instead of 1.8, 5.7, some one entered 2, 5.7, my string array values would then be 3 and not 4! help!
CODE:
#include "SD.h"
#include"SPI.h"
const int SDCARD_CSpin = 10; // the CS pin for the SDCARD set to 53 on mega
// VARIABLES FOR THE SCHEDULE.CSV FILE TO LOAD TO
int weekSchedule = 7; // if this is 7 then i need to only read and load in line 7 of the schedule.csv
int week; // if weekSchedule = 7 for eg. i need to load week 7 which is line 7 in the csv
int lightingCycle;
int nutrientAquaVegaAB;
int nutrientAquaFloresAB;
int nutrientRhizotonic;
int nutrientCanazym;
int nutrientPK1314;
int nutrientBoost;
String desiredECTotal; // < need these to be floats not strings!
String desiredPHTotal; // < need these to be floats not strings!
int desiredroomTemperature;
int desiredroomHumidity;
int lightingONTime;
int lightingOFFTime;
int desiredwaterTemp;
int wateringONTime;
int wateringOFFTime;
int defaultNutrientLevel;
int defaultPHLevel;
int ventilationONTime;
int ventilationOFFTime;
// VARIABLES FOR THE SDCARD READER
File file; // the file name variable
String dataString; // holds the data for the csv to be written to the SD card
int fieldIndex = 0;
void setup() {
Serial.begin (9600); // Initialize the serial port for debugging via the console
pinMode(SDCARD_CSpin, OUTPUT); // initialize the SDCARD CS pin
if (SD.begin(SDCARD_CSpin)) {
Serial.println("SD CARD IS READY!");
} else {
Serial.println("NO SDCARD FOUND! - PLEASE INSERT SDCARD AND REBOOT THE SYSTEM");
return;
}
file = SD.open("SCHEDULE.csv");
if (file) { // i have a number 23 in the csv that this below code looks for to know the length of the array. i would rather this just read in the line eg line 7 if weekSchedule = 7
char ch = file.read(); // Read the first entry - tens
int NoF = 10 * (ch-'0'); // Change ASCII digit to a tens digit
ch = file.read(); // Read the second entry
NoF = NoF + ch-'0'; // Add the ones digit to the tens digit
ch = file.read(); // read the next figure which will be a comma, but ignore it.
unsigned long values[NoF]; // This is the array to hold the values.
if (file.available()) {
for (fieldIndex = 0; fieldIndex < NoF; fieldIndex ++) {
values[fieldIndex]=file.parseInt(); //get a numerical value
}
Serial.println("");
Serial.print(fieldIndex);
Serial.print(" FIELDS RECEIVED FROM SCHEDULE.CSV FILE: ");
for(int i = 0; i < fieldIndex; i++) {
Serial.print(values[i]);
}
fieldIndex = 0; //reset the indexer
}
file.close();
Serial.println("");
Serial.println("LOAD RECEIVED ARRAY DATA INTO THE VARIABLES");
week = values[0];
lightingCycle = values[1];
nutrientAquaVegaAB = values[2];
nutrientAquaFloresAB = values[3];
nutrientRhizotonic = values[4];
nutrientCanazym = values[5];
nutrientPK1314 = values[6];
nutrientBoost = values[7];
desiredECTotal = String(values[8]) + "." + String(values[9]); // < problem here in that the csv has two flaots 1.8,5.7, and i dont want these as strings i need these as floats. also what happens if instead of 1.8, 5.7, some one entered 2, 5.7, my string array values would then be 3 and not 4! help!
desiredPHTotal = String(values[10]) + "." + String(values[11]);
desiredroomTemperature = values[12];
desiredroomHumidity = values[13];
lightingONTime = values[14];
lightingOFFTime = values[15];
desiredwaterTemp = values[16];
wateringONTime = values[17];
wateringOFFTime = values[18];
defaultNutrientLevel = values[19];
defaultPHLevel = values[20];
ventilationONTime = values[21];
ventilationOFFTime = values[22];
}
Serial.println("");
Serial.print("LOADED SETTINGS FOR WEEK: = ");
Serial.println(week);
Serial.print("Lighting Cycle (Hours): = ");
Serial.println(lightingCycle);
Serial.print("Nutrient Aqua Vega Dosage (ml): = ");
Serial.println(nutrientAquaVegaAB);
Serial.print("Nutrient Aqua Flores Dosage (ml): = ");
Serial.println(nutrientAquaFloresAB);
Serial.print("Nutrient Rhizotonic Dosage (ml): = ");
Serial.println(nutrientRhizotonic);
Serial.print("Nutrient Canazym Dosage (ml): = ");
Serial.println(nutrientCanazym);
Serial.print("Nutrient PK 13/14 Dosage (ml): = ");
Serial.println(nutrientPK1314);
Serial.print("Nutrient Boost Dosage (ml): = ");
Serial.println(nutrientBoost);
Serial.print("Desired EC Total: = ");
Serial.println(desiredECTotal);
Serial.print("Desired PH Total: = ");
Serial.println(desiredPHTotal);
Serial.print("Desired Room Temperature: = ");
Serial.println(desiredroomTemperature);
Serial.print("Desired Room Humidity: = ");
Serial.println(desiredroomHumidity);
Serial.print("Lighting ON Time: = ");
Serial.println(lightingONTime);
Serial.print("Lighting OFF Time: = ");
Serial.println(lightingOFFTime);
Serial.print("Desired Water Tempurature: = ");
Serial.println(desiredwaterTemp);
Serial.print("Watering ON Time: = ");
Serial.println(wateringONTime);
Serial.print("Watering OFF Time: = ");
Serial.println(wateringOFFTime);
Serial.print("Default Nutrient Level: = ");
Serial.println(defaultNutrientLevel);
Serial.print("Default PH Level: = ");
Serial.println(defaultPHLevel);
Serial.print("Ventilation ON Time: = ");
Serial.println(ventilationONTime);
Serial.print("Ventilation OFF Time: = ");
Serial.println(ventilationOFFTime);
Serial.println("SCHEDULE DATA UPDATED OK");
}
void loop() {
// put your main code here, to run repeatedly:
}