Hi all,
User needs to control data sampling rate and needs to have control on how long program will run. I have used one configuration file where I give length of program running.This file being saved in SD card. But I cannot do the same for sampling rate/data collection rate.In my code, I am calling sampling rate/data collection rate as LOG_INTERVAL. I want to save LOG_INTERVAL value in a.txt file and call it. How can I do it effectively?
The code is given below. Can anyone please help me?
//TIME IS RIGHT INSIDE CSV FILE, MAX VALUE RIGHT, DISPLAY RIGHT WITH THIS PROGRAM AS OF 10/11/2011
//check font here: https://github.com/olikraus/u8g2/wiki/fntlist8x8
#include <Arduino.h>
#include <U8x8lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#include <SD.h>
#include "RTClib.h"
#include "HX711.h" //You must have this library in your arduino library folder
//---------------------------------------------------------------------------
#define LOG_INTERVAL 500 //Milliseconds between entries (reduce to take more/faster data)
#define SYNC_INTERVAL 10 // millis between calls to flush() - to write data to the card
#define ECHO_TO_SERIAL 1 //Echo data to serial port, Easy method to turn serial printing on and off. Set to 0 if you don't want to print to the Serial Monitor
unsigned long currentmillis=0;
unsigned long prevmillis=0;
uint32_t syncTime = 0; // time of last sync()
uint32_t start_time;
uint32_t currentTime;
int counter = 0;
float x;
//---------------------------------------------------------------------------
RTC_DS1307 RTC; //define the Real Time Clock object
const int chipSelect = 10; //SD chip select;for the data logging shield, we use digital pin 10 for the SD cs line
char timestamp[30];
char filename[30];
const int pushbutton = 8;
const int done_light = 9;
float maxi = 0;
float val1 = 0;
float one_min_val=0;
/***All the variables for voltage reading function***/
//the logging file
File bhoomiFile;
//-------------------Hx711 pins-----------------------------------------------
#define DOUT 3
#define CLK 2
HX711 scale;
float calibration_factor =120396.000;
//--------------------DISPLAY--------------------------------------------------
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);
//------------------------SETUP--------------------------------------------
//-------------------------------------------------------------------------
void setup(){
scale.begin(DOUT, CLK);
Serial.begin(9600); //Initialize the serial communication
u8x8.clear();
u8x8.begin();
Wire.begin(); //Wire.begin: Initiate the Wire library and join the I2C bus as a master or slave.
scale.set_scale(calibration_factor); //Calibration Factor obtained from first sketch
scale.tare(); //Reset the scale to 0
pinMode(pushbutton, INPUT); //Set pushbutton as Input
pinMode(done_light, OUTPUT); //Set the LED as an output
digitalWrite(done_light, LOW); //Make sure the LED is off when we start the program
SD_INIT();
createFile();
//-----------------connect to RTC----------------------
if (!RTC.begin()){ //RTC.begin initializes the internal RTC. It needs to be called before any other RTC library methods
bhoomiFile.println("RTC failed");
}
// RTC.adjust(DateTime(F(__DATE__), F(__TIME__))); //----uncomment this command if you need to adjust the time with RTC running----//
//if (! RTC.isrunning()) //----uncomment this command if you need to adjust the time when RTC is not running----//
// {
// Serial.println("RTC is NOT running, let's set the time!");
// // When time needs to be set on a new device, or after a power loss, the following line sets the RTC to the date & time this sketch was compiled
// RTC.adjust(DateTime(F(__DATE__), F(__TIME__))); //This line sets the RTC with an explicit date & time, for example to set
//RTC.adjust(DateTime(2014, 1, 21, 3, 0, 0)); January 21, 2014 at 3am you would call:
// }
//bhoomiFile.println("millisecond,Time (MM/DD/YYYY HH:MM:SS), weight (g), 1 min weight, Max weight(g)"); //If RTC initializes successfully...set this as a header in the file
start_time = millis(); //The very next step after this will be the data recording
//delay(x1);
}
//-------------------------------LOOP---------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
void loop(){
File dataFile = SD.open("config.txt");
float x = dataFile.parseFloat();
// File dataFile1 = SD.open("config1.txt");
// int LOG_INTERVAL = dataFile1.parseFloat();
for (start_time; (millis() - start_time) < (x * 60 * 1000L); ) //Below: As long as current time - start time < required duration, keep recording the data
//To have record time in Minutes: (DURATION * 60 * 60 * 1000L)
Logging();
u8x8.clear();
while (1) {
#if ECHO_TO_SERIAL
Serial.println("ALL DONE"); //Once the set amount of time has passed, display message saying so
#endif //ECHO_TO_SERIAL
digitalWrite(done_light, HIGH);
//-------DISPLAY Initial VALUE--------
u8x8.setFont(u8x8_font_8x13B_1x2_f); // big font
u8x8.setCursor(0, 32);
u8x8.print(" g begin "); //Unit in gram
u8x8.print(maxi, 3); //read upto 4 decimal point
//-------DISPLAY END VALUE--------
u8x8.setFont(u8x8_font_8x13B_1x2_f); // big font
u8x8.setCursor(1, 2);
u8x8.print(" g end "); //Unit in gram
u8x8.print(val1, 4); //read upto 4 decimal point
//-------DISPLAY TOTAL TIME --------
//Unit in gram
u8x8.setCursor(1, 4);
u8x8.print(" min ");
u8x8.print(x,2); //time in hour
//-------DISPLAY END VALUE--------
//u8x8.setFont(u8x8_font_8x13B_1x2_f);
u8x8.setCursor(2, 6);
u8x8.print(filename);
}
}
//------------------------------------FUNCTIONS-------------------------------------------
//------------------------------------------------------------------------------------
void error(char *str){
Serial.print("error: ");
Serial.println(str);
while (1);
}
//-----------------------FUNCTION SD CARD INITIALIZATION---------------------------------------
//------------------------------------------------------------------------------------
void SD_INIT(){
pinMode(chipSelect, OUTPUT);
//Check if the card is present and can be initialized:
if (!SD.begin(chipSelect)) { //SD.begin initializes the SD library and card; Returns 1 on success, 0 on failure
u8x8.setFont(u8x8_font_courR18_2x3_r);
u8x8.setCursor(3, 19);
u8x8.println("No SD");
return;
}
}
//-----------------------FUNCTION FILE CREATION---------------------------------------
//------------------------------------------------------------------------------------
void createFile(){
strcpy(filename, "00.CSV");
for (uint8_t i = 0; i < 100; i++) { //Make a new file every time the Arduino starts up, Goes from 00 to 199
filename[0] = '0' + ((i / 10) % 10);
filename[1] = '0' + ((i / 1) % 10);
// filename[3] = '0' + (i % 10);
if (!SD.exists(filename)) { //SD.exists() tests whether a file or directory exists on the SD card
bhoomiFile = SD.open(filename, FILE_WRITE); //SD.open() opens a file on the SD card. If the file is opened for writing,
//it will be created if it doesn't already exist (but the directory containing it must already exist).
//FILE_WRITE enables read and write access to the file
bhoomiFile.println("Time(MM/DD/YYYY HH:MM:SS),millisecond, Mass (g)");
break; // leave the loop!
}
}
if (!bhoomiFile){ //If file couldn't be opened/created
error("couldn't create file");
}
//----------- DISPLAY FILE NAME -----
u8x8.setFont(u8x8_font_8x13B_1x2_f);
u8x8.setCursor(2, 22);
u8x8.print(filename);
delay(100);
}
//-----------------------FUNCTION DO THE THING---------------------------------------
//------------------------------------------------------------------------------------
void Logging() { /*uncomment lower IF statement if you want to set up time for your RTC. IF the RTC battery is being removed,
it will set the time to 1/1/00 like this. But after uncommenting and compiling,complilation time will be current time and clock will start acting*/
// if (! RTC.isrunning())
// {
// Serial.println("RTC is NOT running, let's set the time!");
// // When time needs to be set on a new device, or after a power loss, the
// // following line sets the RTC to the date & time this sketch was compiled
// RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
// // This line sets the RTC with an explicit date & time, for example to set
// // January 21, 2014 at 3am you would call:
// // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
// }
// delay((LOG_INTERVAL - 1) - (millis() % LOG_INTERVAL));
delay(LOG_INTERVAL);
DateTime now = RTC.now();
bhoomiFile.println(); //WITHOUT THIS INSTRUCTION,CSV DATA WILL BE SAVED ROWWISE,, NOT COLUMNWISE
//--------LOG DATE& TIME IN ANOTHER COLUMN IN CSV FILE--------
bhoomiFile.print(" "); //This space is important. If you remove this, the seconds will not be recorded in the log file
bhoomiFile.print(now.month(), DEC);
bhoomiFile.print("/");
bhoomiFile.print(now.day(), DEC);
bhoomiFile.print("/");
bhoomiFile.print(now.year(), DEC);
bhoomiFile.print(" ");
bhoomiFile.print(now.hour(), DEC);
bhoomiFile.print(":");
bhoomiFile.print(now.minute(), DEC);
bhoomiFile.print(":");
bhoomiFile.print(now.second(), DEC);
bhoomiFile.print("");
bhoomiFile.print(millis(), DEC);
bhoomiFile.print("");
//--------LOG ALL MILISECOND IN ONE COLUMN IN CSV FILE--------
//bhoomiFile.print("");
bhoomiFile.print(" , ");
bhoomiFile.print(millis(), DEC);
bhoomiFile.print("");
//---------- Log LOAD CELL Current reading in CSV FILE and DISPLAY---------------------------
float currentReading = scale.get_units();
currentTime=millis();
bhoomiFile.print(", "); //Unit in gram
bhoomiFile.print(currentReading * 1, 4); //read upto 4 decimal
u8x8.setFont(u8x8_font_courR18_2x3_r); // big font
u8x8.setCursor(90, 32);
u8x8.print(" g "); //Unit in gram
u8x8.print(scale.get_units() * 1, 3); //read upto 4 decimal point
//---------- Log LOAD CELL MAXIMUM reading in CSV FILE and DISPLAY ---------------------------
if (currentReading < maxi) {
maxi = maxi;
}
else if (currentReading >= maxi) {
maxi = currentReading;
}
// bhoomiFile.print(", "); //Unit in gram
// bhoomiFile.print(maxi, 4); //read upto 4 decimal
u8x8.setFont(u8x8_font_8x13B_1x2_f);//small font
u8x8.setCursor(0, 3);
u8x8.print(" g begin "); //Unit in gram
u8x8.print(maxi, 3); //read upto 4 decimal point
//---------- Log LOAD CELL reading at the end of experiment in CSV FILE ---------------------------
val1=0;
if (currentTime >x * 60 * 1000L ) {
val1=scale.get_units();
}
else{
}
if (val1 < one_min_val) {
one_min_val = one_min_val;
}
else if (val1 >= one_min_val) {
one_min_val = val1;
}
// bhoomiFile.print(", "); //Unit in gram
// bhoomiFile.print(val1, 4); //read upto 4 decimal
//--------------CLOSE CSV FILE BY FLUSHING---------------------------------------
if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();
bhoomiFile.flush(); //flush() ensures that any bytes written to the file are physically saved to the SD card
//When you use file.write(), it doesn't write to the card until you flush() or close().
//Whenever you open a file, be sure to close it to save your data.
}