Long story short I'm a plc programmer (ladder logic, function block) just learning arduino interface and structured text( C, C++ coding). I read the forum rules and hope I post everything correct. sorry if I made a mistake. Anyways I'm setting up a datalogger for some analog devices and using a flow meter to calculate flow and totalize it and send it to a sd card.
I am using a atlas scientific ez flow totalizer and a random flow meter that uses pulses. The analog devices our a transducer, voltage monitor and a amp reading. So my problem is I don't know how to properly write the code and I would like some pointers to make my code more efficient. Also when I incorporate my flow text into program my data log file keeps having in error pop up.
I would appreciate any pointers people have to keep me from scratching my head, also if you see any long term data logging problems. I'm hoping to incorporate a 2nd flow meter at some point. I also have a data log problem, when it goes from to day 9 to day 10 of month the 0 in the ten is causing my log files to not file chronologically.
Thanks for any input.
float value1 = 5;
float value2;
float sensorPin = A0;
float analogPin0 = 0;
float sensorPin1 = A1;
float analogPin1 = 0;
float analog0AVG = 0;
float analog1AVG = 0;
float analog2AVG = 0;
float sensorPin2 = A2;
float analogPin2 = 0;
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10;
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_PCF8523 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
unsigned long LOG_INTERVAL = 15000;
unsigned long previousMillis = 0;
#include <SoftwareSerial.h> //we have to include the SoftwareSerial library, or else we can't use it.
#define rx 2 //define what pin rx is going to be.
#define tx 3 //define what pin tx is going to be.
SoftwareSerial myserial(rx, tx); //define how the soft serial port is going to work.
char sensordata[30]; //we make a 48 byte character array to hold incoming data from the Flow Meter Totalizer.
char sensordatajunk[30];
byte received_from_computer=0; //we need to know how many characters have been received.
byte sensor_bytes_received=0; //we need to know how many characters have been received.
byte string_received=0; //used to identify when we have received a string from the Flow Meter Totalizer.
float f;
char *flotot;
void setup() {
while (!Serial) {
delay(1); // for Leonardo/Micro/Zero
}
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.initialized()) {
Serial.println("RTC is NOT running!");
// 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(2019, 2, 26, 14, 26, 0));
}
// put your setup code here, to run once:
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
delay(1000);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
myserial.begin(9600); //enable the software serial port
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= LOG_INTERVAL) {
previousMillis = currentMillis;
// create a filename
DateTime now;
now = rtc.now();
int YR = now.year();
String YRs = String(YR, DEC);
int MO = now.month();
String MOs = String(MO, DEC);
int DY = now.day();
String DYs = String(DY, DEC);
String DATE = YRs + MOs + DYs;
String filename = DATE + ".txt";
String dataString = "";
dataString += String(now.year(), DEC); dataString += " / ";
dataString += String(now.month(), DEC); dataString += " / ";
dataString += String(now.day(), DEC); dataString += " ( ";
dataString += String(now.hour(), DEC); dataString += " - ";
dataString += String(now.minute(), DEC); dataString += " . ";
dataString += String(now.second(), DEC); dataString += " ) ";
dataString += String(analog0AVG); dataString += "= Volts, ";
dataString += String(analog1AVG); dataString += "= Amps, ";
dataString += String(analog2AVG); dataString += "= Level ";
File dataFile = SD.open(filename, FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
}
else {
Serial.println("error opening datalog.txt");
}
{
value2 = value1 ++;
analogPin0 = analogRead(sensorPin);
analogPin1 = analogRead(sensorPin1);
analogPin2 = analogRead(sensorPin2);
analog0AVG = analogPin0 / 1024 * 150;
analog1AVG = analogPin1 / 1024 * 20;
analog2AVG = analogPin2 / 1024 * 34.605;
Serial.print("Level=");
Serial.print(analog2AVG);
Serial.print("\tFlow Total=");
Serial.print(f);
Serial.print("\tAmps=");
Serial.print(analog1AVG);
Serial.print("\tVolts=");
Serial.println(analog0AVG);
}
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}
flow();
delay(200);
}
void flow(){//reads all flows, both instantaneous and totals
myserial.print("R");
myserial.print("\r");
delay(200);
if (myserial.available() > 0) { //If data has been transmitted from an Atlas Scientific device
sensor_bytes_received = myserial.readBytesUntil(13, sensordata, 30); //we read the data sent from the Atlas Scientific device until we see a <CR>. We also count how many character have been received
sensordata[sensor_bytes_received] = 0;
flotot = strtok(sensordata, ",");
f=atof(flotot);
delay(10);
if (myserial.available() > 0) { //If data has been transmitted from an Atlas Scientific device
int junk = myserial.readBytesUntil(13, sensordatajunk, 30);}
delay(10);
}
}