I am trying to log multiple variables in a csv. file using an sd card. I have the code to write the 2 different variables (plantCounter and wheelCounter) and they onto the sd card and show in excel just like I want them, but I am trying to make the arduino solve an equation such as (plantSensor/wheelSensor) so I do not have to do it in the csv. file after the fact. When I try to add this it jumbles up all my numbers in excel. So I want 3 variables in my csv. file; plantCounter, wheelCounter, and (plantCounter/wheelCounter). Is there a way to fix this? Sorry my code is sloppy, I am really new to coding.
#include <SD.h>
int plantSensor = 2; //Plant Sensor
int wheelSensor = 3; //Wheel Sensor
int plantCounter = 0; //Plant Sensor Counter
int wheelCounter = 1; //Wheel Sensor Counter
int statePlant;
int laststatePlant = HIGH;
int stateWheel;
int laststateWheel = HIGH;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
int refreshrate = 10;
const int CS_PIN = 10;
const int POW_PIN = 8;
void setup() {
Serial.begin(9600);
Serial.println("Initializing Card");
pinMode(CS_PIN, OUTPUT);
pinMode(POW_PIN, OUTPUT);
digitalWrite(POW_PIN, HIGH);
if (!SD.begin(CS_PIN)) {
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
File commandFile = SD.open("speed.txt");
if (commandFile) {
Serial.println("Reading Command File");
while(commandFile.available()) {
refreshrate = commandFile.parseInt();
}
Serial.print("Refresh Rate = ");
Serial.print(refreshrate);
Serial.println("ms");
commandFile.close();
}
else {
Serial.println("Could not read command file.");
return;
}
}
void loop()
{
//Plant counting loop
int statePlant = digitalRead(plantSensor);
if ( statePlant != laststatePlant)
{
lastDebounceTime = millis();
laststatePlant = statePlant;
plantCounter=plantCounter+1;
File dataFile =SD.open("log.csv",FILE_WRITE);
if(dataFile)
dataFile.print(plantCounter); //Logging plants to SD card
dataFile.print(",");
dataFile.print((wheelCounter-1)/2); //Logging wheel slots to SD card
dataFile.println(".0");
dataFile.close();
Serial.print(plantCounter); //Serial Print plants
Serial.print(",");
Serial.println((wheelCounter-1)/2); //Serial print wheel slots
}
//Wheel hole counting loop
int stateWheel = digitalRead(wheelSensor);
if ( stateWheel !=laststateWheel)
{
laststateWheel = stateWheel;
wheelCounter = wheelCounter+1;
}
}