Main goal is to read data from sensor and save these data to USB. I am using USB module called CH376.
Code 1 only reads data and Code 2 is used to save data to USB flash drive. Both codes work fine individually. I just can't find a way to save all the data from Code 1 to USB drive. Can someone please help?
But I have usb module called CH376. You can look it up. I'm able to use code 2 and it works fine with arduino uno.
I don't mind switching arduino (I also have arduino mega and arduino due). The main question is how to combine my code. This is the main problem that I am trying to solve. Do you think you can help?
Here you go. I have not tested it but it does compile with only a couple of warnings where the author of the Ch376msc library forgot to put 'const' on their char* function arguments.
// Number of sensors:
// Pressure sensor (x3) +
// Turbine Flow (x2) +
// TDS sensor (x2) +
// Temperature sensor (x1)
//******************************************************************************************************
//Arduino pins being used:
//Pressure sensor:>>>>>>>>>> A0, A1, A2 (Analog)
//TDS sensor:>>>>>>>>>>>>>>> A3, A4 (Analog)
//Turbine flowmeter:>>>>>>>> 2, 3 (digital)
//Temperature senor:>>>>>>>> 9 (Digital)
// 6 and 7: SoftwareSerial
//SD card: >>>>>>>>>>>>>>>>> 4 (Digital)
//**********************************************************************************************************
//Libraries included
#include <GravityTDS.h> // Library for the TDS sensors
#include <OneWire.h> // Library to establish communication with sensors
#include <DallasTemperature.h> //Library for the temperature sensor
#include <Ch376msc.h>
#include <SoftwareSerial.h>
const char * MyFileName = "Data25.txt"; // Gives my file a name.
//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 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 pressureValue1a = 0; // Object for pressure transducer 1
float pressureValue2a = 0; // Object for pressure transducer 2
float pressureValue3a = 0; // Object for pressure transducer 3
//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
/*------------------------------------------------------------------------------------------------------------------
Author: György Kovács |
Created: 28 Mar 2019 |
Description: Basic usage of CH376 with software serial |
Thanks for the idea to Scott C , https://arduinobasics.blogspot.com/2015/05/ch376s-usb-readwrite-module.html |
------------------------------------------------------------------------------------------------------------------
*/
//Important! First create a soft serial object, after create a Ch376 object
SoftwareSerial mySerial(7, 6); // RX, TX pins on arduino
Ch376msc flashdrive(mySerial); // Ch376 object with software serial
unsigned long totSect = 0;
unsigned long freeSect = 0;
byte percentg = 0;
byte tmpCommand; //used to store data coming from serial port
boolean readMore;
static char helpString[] = {"h:Print this help\n\n1:Create\n2:Append\n3:Read\n4:Read date/time\n"
"5:Modify date/time\n6:Delete\n7:List dir\n8:Print free space"
"\n9:Open/Create folder(s)/subfolder(s)"
};
//Print information
void printInfo(const char info[])
{
int infoLength = strlen(info);
if (infoLength > 40)
{
infoLength = 40;
}
Serial.print(F("\n\n"));
for (int a = 0; a < infoLength; a++)
{
Serial.print('*');
}
Serial.println();
Serial.println(info);
for (int a = 0; a < infoLength; a++)
{
Serial.print('*');
}
Serial.print(F("\n\n"));
}
void setup()
{
Serial.begin(115200); //This starts serial communicatiion, so that the arduino can send out commands thourhg USB connection
pinMode(10, OUTPUT); // Pin 10 reserved for output
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) TF1Frequency(Hz) TF2Frequency(Hz) TdsSensorValue1(ppm) TdsSensorValue2(ppm) TemperatureInCelcius(C) TemperatureInFarenheit(F)");
mySerial.begin(9600);// Important! First initialize soft serial object and after Ch376
flashdrive.init();
printInfo(helpString);
}
void loop()
{
if (flashdrive.checkIntMessage())
{
if (flashdrive.getDeviceStatus())
{
Serial.println(F("Flash drive attached!"));
}
else
{
Serial.println(F("Flash drive detached!"));
delay(5000);
return;
}
}
//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
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);
}
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);
String fileLine;
fileLine += String(pressureValue1a, 1);
fileLine += String(" ");
fileLine += String(pressureValue2a, 1); //Pressure sensor
fileLine += String(" ");
fileLine += String(pressureValue3a, 1); //Pressure sensor
fileLine += String(" ");
fileLine += String(frequency1); // Flow turbine (Hertz)
fileLine += String(" ");
fileLine += String(frequency2); // Flow turbine (Hertz)
fileLine += String(" ");
fileLine += String(tdsValue1, 1); //TDS sensor (uS/cm)// The 1 just means "no number after the decimal"
fileLine += String(" ");
//fileLine += String(tdsValue2,1);//TDS sensor (uS/cm)
fileLine += String(" ");
fileLine += String(Celsius);// Temperature sensor
fileLine += String(" ");
fileLine += String(Fahrenheit);//Temperature sensor
fileLine += '\n';
// Display on Serial Monitor
Serial.print(fileLine);
// Store on flashdrive
flashdrive.setFileName(MyFileName);
if (flashdrive.openFile() == ANSW_USB_INT_SUCCESS) //open the file
{
flashdrive.moveCursor(CURSOREND); //if the file exist, move the "virtual" cursor at end of the file
flashdrive.writeFile(fileLine.c_str(), fileLine.length());
flashdrive.closeFile();
}
else
Serial.println("Failed to open file");
delay(1000);
}
Code 2 by itself works by sending a command. For example if I use the serial monitor and enter 52, data will be saved to the created file.
I am trying to find a way to not use commands. I want to automatically start saving data when the test is running and the USB flash drive is attached.
I am working on it also. If I aslo find a solution for this. I will post for someone in the future who may be running through the same issue.
Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.
Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.
Repeated duplicate posting could result in a temporary or permanent ban from the forum.
Could you take a few moments to Learn How To Use The Forum
It will help you get the best out of the forum in the future.
I did the best I could without having the hardware to test with. From the error message you can tell that the flashdrive.openFile() is NOT returning ANSW_USB_INT_SUCCESS. You could print out the value that it DOES return instead and look that up in the Ch376msc library sources. That might tell you WHY it was unable to open the file. Maybe the file has to already exist.
const uint8_t ANSW_RET_SUCCESS = 0x51; //Operation successful
const uint8_t ANSW_USB_INT_SUCCESS = 0x14; //Operation successful, no further data
const uint8_t ANSW_USB_INT_CONNECT = 0x15; //New USB device connected
const uint8_t ANSW_USB_INT_DISCONNECT = 0x16;//USB device unplugged!
const uint8_t ANSW_USB_INT_USB_READY = 0x18; //Device is ready
const uint8_t ANSW_USB_INT_DISK_READ = 0x1d; //Disk read operation
const uint8_t ANSW_USB_INT_DISK_WRITE = 0x1e;//Disk write operation
const uint8_t ANSW_RET_ABORT = 0x5F; //Operation failure
const uint8_t ANSW_USB_INT_DISK_ERR = 0x1f; //USB storage device error
const uint8_t ANSW_USB_INT_BUF_OVER = 0x17; //Buffer overflow
const uint8_t ANSW_ERR_OPEN_DIR = 0x41; //Tried to open a directory with FILE_OPEN
const uint8_t ANSW_ERR_MISS_FILE = 0x42; //File not found
const uint8_t ANSW_ERR_FOUND_NAME = 0x43;
const uint8_t ANSW_ERR_DISK_DISCON = 0x82; //Disk disconnected
const uint8_t ANSW_ERR_LARGE_SECTOR = 0x84; //Sector size is not 512 bytes
const uint8_t ANSW_ERR_TYPE_ERROR = 0x92; //Invalid partition type, reformat drive
const uint8_t ANSW_ERR_BPB_ERROR = 0xa1; //Partition not formatted
const uint8_t ANSW_ERR_DISK_FULL = 0xb1; //Disk full
const uint8_t ANSW_ERR_FDT_OVER = 0xb2; //Directory full
const uint8_t ANSW_ERR_FILE_CLOSE = 0xb4; //Attempted operation on closed file
////////////////////////////////////////////