mr.ToddL1962
#include <SPI.h>
#include "SdFat.h"
#include "RTClib.h"
#include "HX711.h"//XFW-HX711 amplifier 80Hz
#define calibration_factor -7090.0
int LOADCELL_DOUT_PIN = 7;
int LOADCELL_SCK_PIN = 6;
int LOADCELL_DOUT_PIN_1 = 5;
int LOADCELL_SCK_PIN_1 = 4;
int LOADCELL_DOUT_PIN_2 = 3;
int LOADCELL_SCK_PIN_2 = 2;
HX711 scale;
HX711 scale1;
HX711 scale2;
int navoser;
int sensorPin = A0;
int relay1 = 8;
int relay2 = 9;
unsigned long previousMillis = 0;
long OnTime = 15000;
long OffTime = 500;
int relay2State = LOW;
boolean alreadyRun = false;
int sensorValue = 0;
float voltage;
RTC_DS1307 RTC;//using RTClib
DateTime now;
bool enableDataLogging = false;
String receivedChar;
boolean newData = false;
const uint8_t chipSelect = 10;
const uint32_t SAMPLE_INTERVAL_MS = 1;
#define FILE_BASE_NAME "Result"
#define error(msg) sd.errorHalt(F(msg))
//------------------------------------------------------------------------------
SdFat sd;
SdFile file;// Log file.
uint32_t logTime;
//==============================================================================
int Date_count = 1;
int Time_count = 1;
int Load_Cell1 = 1;
int Load_Cell2 = 1;
int Load_Cell3 = 1;
//------------------------------------------------------------------------------
void load_cell () {
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("t3.txt=");
Serial.write(0x22);
Serial.print(scale.get_units(), 1);
Serial.write(0x22);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("t4.txt=");
Serial.write(0x22);
Serial.print(scale1.get_units(), 1);
Serial.write(0x22);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("t5.txt=");
Serial.write(0x22);
Serial.print(scale2.get_units(), 1);
Serial.write(0x22);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
navoser = (scale.get_units() + scale1.get_units() + scale2.get_units() / 3);
Serial.print("t8.txt=");
Serial.write(0x22);
Serial.print(navoser);
Serial.write(0x22);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
/*
Serial.print("Reading: ");
Serial.print(scale.get_units(), 1);
// Serial.print(" kg");
Serial.print("\t");
Serial.print(scale1.get_units(), 1);
Serial.print("\t");
Serial.print(scale2.get_units(), 1);
Serial.print(" kg");
Serial.println();
*/
}
void setup() {
pinMode(sensorPin, INPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
Serial.begin(9600);//
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale1.begin(LOADCELL_DOUT_PIN_1, LOADCELL_SCK_PIN_1);
scale2.begin(LOADCELL_DOUT_PIN_2, LOADCELL_SCK_PIN_2);
scale.set_scale(calibration_factor);
scale.tare();
scale1.set_scale(calibration_factor);
scale1.tare();
scale2.set_scale(calibration_factor);
scale2.tare();
RTC.begin();
}
void creat_file() {
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[13] = FILE_BASE_NAME "00.csv";
if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
sd.initErrorHalt();
}
// Find an unused file name.
if (BASE_NAME_SIZE > 7) {
error("FILE_BASE_NAME long");
}
while (sd.exists(fileName)) {
if (fileName[BASE_NAME_SIZE + 1] != '9') {
fileName[BASE_NAME_SIZE + 1]++;
} else if (fileName[BASE_NAME_SIZE] != '9') {
fileName[BASE_NAME_SIZE + 1] = '0';
fileName[BASE_NAME_SIZE]++;
} else {
error("filename not created");
}
}
if (!file.open(fileName, O_WRONLY | O_CREAT | O_EXCL)) {
error("file.open");
}
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("page 4");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("g1.txt=");
Serial.write(0x22);
Serial.print(fileName);
Serial.write(0x22);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
//Serial.print(F("Recording: "));
//Serial.println(fileName);
//Serial.println(F("enter any character to stop"));
writeHeader();
}
//------------------------------------------------------------------------------
void loop() {
recvOneChar();
useNewData();
if (enableDataLogging) {
logTime += SAMPLE_INTERVAL_MS;
logData();
if (!file.sync() || file.getWriteError()) {
error("error write");
}
} else {
load_cell();
}
}
void recvOneChar()
{
sensorValue = analogRead(sensorPin);
voltage = sensorValue * (5.0 / 1023.0);
if (Serial.available() > 0 || voltage > 2 ) {
receivedChar = Serial.readStringUntil('#');
newData = true;
}
}//recvOneChar
void useNewData()
{
if (newData == true)
{
//Serial.print("This just in ... ");
Serial.println(receivedChar);
if (receivedChar.indexOf("2") > -1 || voltage > 2 )
{
creat_file();
enableDataLogging = true;
previousMillis = 0; // <----------------
alreadyRun = false; // <----------------
//Serial.println("Data logging enabled");
//digitalWrite(LED_BUILTIN, HIGH);
}
if (receivedChar.indexOf("5") > -1)
{
enableDataLogging = false;
//Serial.println(" Data logging disabled");
//digitalWrite(LED_BUILTIN, LOW);
if (file) file.close();
//SysCall::halt();
}
newData = false;
}
}//useNewData
void writeHeader() {
file.print(F("time(us)"));
for (int c = 0; c < Load_Cell1; c++) {
file.print(F(",load1"));
//file.print(c, DEC);
} for (int d = 0; d < Load_Cell2; d++) {
file.print(F(",load2"));
//file.print(d, DEC);
} for (int e = 0; e < Load_Cell3; e++) {
file.print(F(",load3"));
//file.print(e, DEC);
}
for (int a = 0; a < Date_count; a++) {
now = RTC.now();
file.print(F(",date:"));
file.print(now.day(), DEC);
file.print('/');
file.print(now.month(), DEC);
file.print('/');
file.print(now.year(), DEC);
} for (int b = 0; b < Time_count; b++) {
file.print(F(",time(hour-minute-second)"));
}
file.println();
}
//------------------------------------------------------------------------------
void logData() {
file.print(logTime);
for (int c = 0; c < Load_Cell1; c++) {
file.write(',');
file.print(scale.get_units());
} for (int d = 0; d < Load_Cell2; d++) {
file.write(',');
file.print(scale1.get_units());
} for (int e = 0; e < Load_Cell3; e++) {
file.write(',');
file.print(scale2.get_units());//file.print(scale2.get_units(),DEC);
}
for (int a = 0; a < Date_count; a++) {
file.write(',');
//file.print("test");
} for (int b = 0; b < Time_count; b++) {
file.write(',');
file.print(now.hour(), DEC);
file.print(":");
file.print(now.minute(), DEC);
file.print(":");
file.print(now.second(), DEC);
file.print(":");
file.print(micros(), DEC);
}
file.println();
relay ();
}
//==============================================================================
void relay () {
if (alreadyRun == false) {
unsigned long currentMillis = millis();
if ((relay2State == LOW) && (currentMillis - previousMillis >= OnTime) ) {
relay2State = HIGH; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(relay2, relay2State); // Update the actual LED
}
else if ((relay2State == HIGH) && (currentMillis - previousMillis >= OffTime) ) {
relay2State = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(relay2, relay2State); // Update the actual LED
alreadyRun = true;
}
}
}
I refreshed the code like this, there is some change, but it doesn't work exactly as I want. The code works in this way, when the arduino is energized for the first time and after making the first analog reading; it does not wait for 15 seconds, it waits only for 5 seconds, and it closes the relay after remaining open for the specified time. the other function continues to work.
But if I start the loop again without disconnecting the power from the Arduino, this time the relay becomes "HIGH" without waiting, stays on for the specified time and then turns off. Again, other functions continue to work.