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);
}
}