Can someone please tell me what I am doing wrong?

My code is being successfully uploaded to the arduino, but I am not getting the expected results.
I am collecting data from sensors and I want to save the data to a SD card and create new file every 120000 milliseconds. For example, LOGGER01.txt, LOGGER02.txt, etc... every 120000 seconds.
When I look to see if I have files saved on the SD CARD, the SD card is empty. Can someone please help me or at least tell me what I am doing wrong?
I am using arduino uno and usb host shield.
Here is my code:

code to mix 1 and timer.ino (12.2 KB)

Can someone please just tell me what I am doing wrong. I just want to get done with this project

post the code here so that we can read it without downloading stuff

Help us help you.

Without looking at your code, I can tell you did NOT close the SD file before you started a new file. There may be other errors, as well.

File logfile = SD.open("filename",FILE_WRITE);
What file are you trying to open here? Looks like you probably meant to pass in filename as a variable rather than a string but I don't think the variable filename is in scope here so I'm not sure?

I am using a USB shield and All I did was mounting it on top of the arduino uno.
It is not a simulator code.


Here is the code:

//Number of sesnors: Pressure sensor (x3) + Turbine Flow (x2) + TDS sensor (x2) + Temperature sensor (x1)
//******************************************************************************************************

//Arduino pins being used:
//Pressure sensor:>>>>>>>>>> A0, A1, A2, A12, A13 (Analog)
//TDS sensor:>>>>>>>>>>>>>>> A3, A4 (Analog)
//Turbine flowmeter:>>>>>>>> 2,3 (digital)
//Temperature senor:>>>>>>>> 9 (Digital)
//SD card: >>>>>>>>>>>>>>>>> 4 (Digital)
//**********************************************************************************************************

//Libraries included
unsigned long start, finished, elapsed; // Timer

#include <SPI.h> //SD card library
#include <SD.h> //SD card library
#include "Wire.h" //allows communication over i2c
#include <GravityTDS.h> // Library for the TDS sensors
#include <EEPROM.h> //
#include <OneWire.h> // Library to establish communication with sensors
#include <DallasTemperature.h> //Library for the temperature sensor

//SD card
//String MyFileName = "Data28.txt"; // Gives my file a name.
const int chipSelect = 4;// SD card pin on arduino
File logfile; // Creates an object for SDcard

//Pressure sensor >> Analog pin: A0, A1, A2
const int pressureInput1 = A0; //selects the analog input pin for the pressure transducer 1
const int pressureInput2 = A1; //selects the analog input pin for the pressure transducer 2
const int pressureInput3 = A2; //selects the analog input pin for the pressure transducer 3
//const int pressureInput4 = A12; //selects the analog input pin for the pressure transducer 4
//onst int pressureInput5 = A13; //selects the analog input pin for the pressure transducer 5
const int pressureZero = 100; //analog reading of pressure transducer at 0psi
const int pressureMax = 650.0; //analog reading of pressure transducer at 100psi
const int pressuretransducermaxPSI = 100; //psi value of transducer being used
float pressureValue1 = 0; //variable to store the value coming from the pressure transducer 1
float pressureValue2 = 0; //variable to store the value coming from the pressure transducer 2
float pressureValue3 = 0; //variable to store the value coming from the pressure transducer 3
float pressureValue4 = 0; //variable to store the value coming from the pressure transducer 4
float pressureValue5 = 0; //variable to store the value coming from the pressure transducer 5
float pressureValue1a= 0; // Object for pressure transducer 1
float pressureValue2a= 0;// Object for pressure transducer 2
float pressureValue3a= 0;// Object for pressure transducer 3
float pressureValue4a= 0;// Object for pressure transducer 4
float pressureValue5a= 0;// Object for pressure transducer 5

//Turbine Flowmeter >> Digital pin:2, 3
int Htime1; //Stores high pulse (signal) for Turbine Flowmeter 1
int Ltime1; //Stores low pulse (signal) for Turbine Flowmeter 1
float Ttime1; // Stores total time of a cycle or period for sensor 1
float frequency1; //stores frequency for sensor 1

int Htime2; //Stores high pulse (signal) for Turbine Flowmeter 2
int Ltime2; //Stores low pulse (signal) for Turbine Flowmeter 2
float Ttime2; // Stores total time of a cycle or period for sensor 2
float frequency2; //stores frequency for sensor 2

//TDS Sensor >> Analog pin: A3, A4
#define TdsSensorPin1 A3 // Sets TDS sensor 1 to analog pin A3
#define TdsSensorPin2 A4 //Sets TDS sensor 1 to analog pin A4
GravityTDS gravityTds1; //Creates an object for TDS sensor 1
GravityTDS gravityTds2;//Object is created for TDS sensor 2
float temperature = 25; // Sets a fixed temperature (25C) and TDSvalue as floating number.
float tdsValue1 = 0; // Initializes data storage for TDS sensor 1
float tdsValue2 = 0; //Initializes data storage for TDS sensor 2

//Temperature sensor (DS18B20)
#define ONE_WIRE_BUS 9 // Temperature will be using digital pin 9
OneWire oneWire(ONE_WIRE_BUS);// Establishes communication with temperature sensor
DallasTemperature sensors(&oneWire);
float Celsius = 0; // Initializes the temperature in celsius as floating data type and 0 is the initial value
float Fahrenheit = 0; // Initializes the temperature in fahrenheit as floating data type and 0 is the initial value

void setup()

{Serial.begin(9600);//This starts serial communicatiion, so that the arduino can send out commands thourhg USB connection
pinMode(53,OUTPUT);
pinMode(10,OUTPUT);

//logfile.println("Pressure1(psi) Pressure2(psi) Pressure3(psi) Pressure4(psi) Pressure5(psi) TF1Frequency(Hz) TF2Frequency(Hz) TdsSensorValue1(ppm) TdsSensorValue2(ppm) TemperatureInCelcius(C) TemperatureInFarenheit(F)");// writing headers in the text file saved in the SD card.*/

pinMode(2,INPUT);//Turbine folow 1 using Digital input #2
pinMode(3,INPUT);//Turbine folow 2 using Digital input #3

gravityTds1.setPin(TdsSensorPin1); //Creates data storage for TDS sensor 1
gravityTds1.setAref(5.0); //reference voltage on ADC, default 5.0V on Arduino UNO
gravityTds1.setAdcRange(1024); //1024 for 10bit ADC;4096 for 12bit ADC
gravityTds1.begin(); //Establishes communication with TDS sensor 1

gravityTds2.setPin(TdsSensorPin2); //Creates data storage for TDS sensor 2
gravityTds2.setAref(5.0); //reference voltage on ADC, default 5.0V on Arduino UNO
gravityTds2.setAdcRange(1024); //1024 for 10bit ADC;4096 for 12bit ADC
gravityTds2.begin(); //Establishes communication with TDS sensor 2
Serial.println("Pressure1(psi) Pressure2(psi) Pressure3(psi) Pressure4(psi) Pressure4(psi) TF1Frequency(Hz) TF2Frequency(Hz) TdsSensorValue1(ppm) TdsSensorValue2(ppm) TemperatureInCelcius(C) TemperatureInFarenheit(F)");
}

void loop()
{
float s, ms;
int m=0;
unsigned long over;
elapsed = finished;
elapsed = elapsed % 120000;
s = int(elapsed / 1000);
if(s == 60)
{
m = m++;

}

finished = millis();

//Pressure sensor
pressureValue1 = analogRead(pressureInput1); //reads value from input pin A0 and assigns to variable
pressureValue1a = ((pressureValue1-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi for pressure sensor 1
pressureValue2 = analogRead(pressureInput2); //reads value from input pin A1 and assigns to variable
pressureValue2a = ((pressureValue2-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi for pressure sensor 2
pressureValue3 = analogRead(pressureInput3); //reads value from input pin A2 and assigns to variable
pressureValue3a = ((pressureValue3-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi for pressure sensor 3
pressureValue4 = analogRead(pressureInput3); //reads value from input pin A12 and assigns to variable
pressureValue4a = ((pressureValue4-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi for pressure sensor 4
pressureValue5 = analogRead(pressureInput3); //reads value from input pin A13 and assigns to variable
pressureValue5a = ((pressureValue5-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi for pressure sensor 5

if (pressureValue1a<0){
pressureValue1a = 0;
}
else if (pressureValue1a>0){
pressureValue1a = ((pressureValue1-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi
}
if (pressureValue2a<0){
pressureValue2a = 0;
}
else if (pressureValue2a>0){
pressureValue2a = ((pressureValue2-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi
}
if (pressureValue3a<0){
pressureValue3a = 0;
}
else if (pressureValue3a>0){
pressureValue3a = ((pressureValue3-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero);}

if (pressureValue4a<0){
pressureValue4a = 0;
}
else if (pressureValue4a>0){
pressureValue4a = ((pressureValue4-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero);}

if (pressureValue5a<0){
pressureValue5a = 0;
}
else if (pressureValue5a>0){
pressureValue5a = ((pressureValue5-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero);}
delay(100);

//Turbine flow
Htime1=pulseIn(2,HIGH); //reads high signal (pulse) from digital pin 2 for turbine flow 1
Ltime1=pulseIn(2,LOW); //reads low signal (pulse) from digital pin 2 for turbine flow 1
Ttime1 = Htime1+Ltime1; //Sum of the time for both high and low pulse
frequency1=1000000/Ttime1; //Calculates frequency

Htime2=pulseIn(3,HIGH); //reads high signal (pulse) from digital pin 3 for turbine flow 2
Ltime2=pulseIn(3,LOW); //reads low signal (pulse) from digital pin 3 for turbine flow 2
Ttime2 = Htime2+Ltime2; ////Sum of the time for both high and low pulse
frequency2=1000000/Ttime2; //Calculates frequency
delay(100);

//TDS sensor
gravityTds1.setTemperature(temperature); // sets the temperature and executes temperature compensation
gravityTds1.update(); //samples and calculates
tdsValue1 = gravityTds1.getTdsValue(); // Stores data for TDS sensor 1 in TDSvalue1
//tdsInSiemens1 = tdsValue1*(1.56); // Conversion from ppm to uS/cm

gravityTds2.setTemperature(temperature); // sets the temperature and executes temperature compensation
gravityTds2.update(); //samples and calculates
tdsValue2 = gravityTds2.getTdsValue(); // Stores data for TDS sensor 1 in TDSvalue2
//tdsInSiemens2 = tdsValue2*(1.56); //Conversion from ppm to uS/cm
delay(100);

//Temperature sensor
sensors.requestTemperatures();//Gets temperature
Celsius = sensors.getTempCByIndex(0);// Gets temperature in celcius
Fahrenheit = sensors.toFahrenheit(Celsius); // Converts celsius to Fahrenheit
delay(100);

//Serial monitor display
Serial.print( pressureValue1a, 1); //Pressure sensor
Serial.print(" ");
Serial.print(pressureValue2a, 1); //Pressure sensor
Serial.print(" ");
Serial.print(pressureValue3a, 1); //Pressure sensor
Serial.print(" ");
Serial.print(pressureValue4a, 1); //Pressure sensor
Serial.print(" ");
Serial.print(pressureValue5a, 1); //Pressure sensor
Serial.print(" ");
Serial.print(frequency1); // Flow turbine (Hertz)
Serial.print(" ");
Serial.print(frequency2); // Flow turbine (Hertz)
Serial.print(" ");
Serial.print(tdsValue1,1);//TDS sensor (us/cm)
Serial.print(" ");
Serial.print(tdsValue2,1);//TDS sensor (uS/cm)
Serial.print(" ");
Serial.print(Celsius);// Temperature
Serial.print(" ");
Serial.println(Fahrenheit);

// create new file
if (s== 117){
char filename[] = "LOGGER00.txt";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
}

if (s==117){

logfile.println("Pressure1(psi) Pressure2(psi) Pressure3(psi) Pressure4(psi) Pressure4(psi) TF1Frequency(Hz) TF2Frequency(Hz) TdsSensorValue1(ppm) TdsSensorValue2(ppm) TemperatureInCelcius(C) TemperatureInFarenheit(F)");
//SD card saving
File logfile = SD.open("filename",FILE_WRITE); //open MyData.txt on the sd card, logfile is an opeject.
if (logfile){ //Only do these things things below if data file opened successfully
logfile.print(pressureValue1a, 1);
logfile.print(" ");
logfile.print(pressureValue2a, 1); //Pressure sensor
logfile.print(" ");
logfile.print(pressureValue3a, 1); //Pressure sensor
logfile.print(" ");
logfile.print(pressureValue4a, 1); //Pressure sensor
logfile.print(" ");
logfile.print(pressureValue5a, 1); //Pressure sensor
logfile.print(" ");
logfile.print(frequency1); // Flow turbine (Hertz)
logfile.print(" ");
logfile.print(frequency2); // Flow turbine (Hertz)
logfile.print(" ");
logfile.print(tdsValue1,1);//TDS sensor (uS/cm)// The 1 just means "no number after the decimal"
logfile.print(" ");
logfile.print(tdsValue2,1);//TDS sensor (uS/cm)
logfile.print(" ");
logfile.print(Celsius);// Temperature sensor
logfile.print(" ");
logfile.println(Fahrenheit);//Temperature sensor
delay(1000);
logfile.close();
}
else
logfile.println();
delay(1000);
}
}

Please edit your post to add code tags ("</>" editor button).

You have been on the forum for many days, created several topics, but still have not read the welcome message with the forum guidelines.
As you are ignoring the using of the code tags. I think it would be right if until you correct your messages - no one will answer you

Learn about arrays. They would save you quite a lot of code... (and superfluous comments)

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly

please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It’s barely readable as it stands. (also make sure you indented the code in the IDE before copying, that’s done by pressing ctrlT on a PC or cmdT on a Mac)


Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

I've deleted a few off topic comments and their replies. Yes, I know they were not rude at all, and nor were the related comments, but they also were not at all relevant to the subject.

Cool. Are you a moderator of this forum?
I like the moderation for a "moderate language" used (but not for deleting posts and "related comments" by a "censor").
Who are you?
Who does decide what "not at all relevant to the subject" is?
Sorry, I am confused.

Moderators have the shield icon next to their username. They act when an user clicks the "Flag" icon and alerts them to a problem. You can do it if you see something that is well out of a technical scope (sex, religion, politics are the three topics generally not discussed in public, plus angry posts, etc.) All users should behave courteously and understand that there are many noobs, and many where English is not their first language. If you wouldn't say it in front of your mother, don't say it here! LOL

BTW I am just another user.