I am experiencing strange behavior in my code. Basically I have a sketch that monitors the current drawn by two motors and opens a relay in the STOP circuit of a piece of equipment. The basic sketch works fine but I decided to provide some feature updates, namely data logging with timestamps. I added the RTC routines and verified they work, then added the datalogging routines without the RTC and verified that it worked. I then added all routines to the sketch and that is when the problem manifested itself. I am using a 2 line 16 character LCD display being driven via IC2 and the Adafruit datalogging shield for Arduino Uno. What I am experiencing is a software flag named data_logging_flag is being reset to LOW whenever I try to create a file on the SD card. This occurs in the void start_logging() function. If I comment out the for loop in that function that creates the file the flag doesn't get reset. Looking for enlightenment. Attached is my code and screenshots of serial.print troubleshooting. The screenshots show what happens while running sketch and pressing down button to start logging.
[code]
// Current_meter_final_v4
// include the library code:
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
// const int rs = A3(ORANGE), en = 9(GRN/WHT), d4 = 5(YELLOW), d5 = 6(GREEN), d6 = 7(BLUE), d7 = 8(WHITE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
// these constants won't change. But you can change the size of
// your LCD using them:
const int numRows = 2;
const int numCols = 16;
// setup buttons
const int upButton = 5; //BLUE was 1, 3
const int downButton = 6; // YELLOW was 0, 2
const int leftButton = 7; //GREEN was 2, 4
const int rightButton = 8; //RED was 3, 5
const int enterButton = 9; //WHITE was 4, 6
// setup digits for setpoint adjustment
int digitOne[] = {0, 1};
int digitTwo[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int digitThree[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int digitFour[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int column[] = {11, 12, 13, 14};
int x = 0;
int y = 11;
int z = 0;
int w = 0;
int v = 0;
// declare and initialize overcurrent flags
bool overCurrent_1_flag = LOW;
bool overCurrent_2_flag = LOW;
// declare and initialize data logging flag
bool data_logging_flag;
// declare and initialize setpoints
float setPoint_1 = 9.5;
float setPoint_2 = 9.5;
// declare and initialize AC Current values and analog input pins
float ACCurrentValue1 = 0.0;
float ACCurrentValue2 = 0.0;
const int AC1Pin = A1; //set arduino signal read pin
const int AC2Pin = A2;
const int chipSelect = 4;
#define ACTectionRange 20; //set Non-invasive AC Current Sensor tection range (5A,10A,20A)
// VREF: Analog reference
// For Arduino UNO, Leonardo and mega2560, etc. change VREF to 5
// For Arduino Zero, Due, MKR Family, ESP32, etc. 3V3 controllers, change VREF to 3.3
#define VREF 5.0
// the logging file
File logfile;
RTC_PCF8523 RTC; // define the Real Time Clock object
DateTime now;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin();
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(leftButton, INPUT_PULLUP);
pinMode(rightButton, INPUT_PULLUP);
pinMode(enterButton, INPUT_PULLUP);
pinMode(A0, OUTPUT);
Serial.begin(115200);
}
void loop() {
lcd.clear();
ACCurrentValue1 = readACCurrentValue_1(); //read AC Current Value
ACCurrentValue2 = readACCurrentValue_2(); //read AC Current Value
Serial.print("Main Loop");
Serial.println(data_logging_flag);
if (setPoint_1 == 0) {
setpoint_1_adjust();
}
if (setPoint_2 == 0) {
setpoint_2_adjust();
}
if (digitalRead(leftButton) == LOW) {
setpoint_1_adjust();
}
else if (digitalRead(rightButton) == LOW) {
setpoint_2_adjust();
}
else if (digitalRead(downButton) == LOW) {
start_logging();
// Serial.println(data_logging_flag);
}
else if (digitalRead(upButton) == LOW) {
stop_logging();
// Serial.println(data_logging_flag);
}
lcd.home();
lcd.setCursor(0, 0);
lcd.print(" AMPS VFD1 TRIP");
lcd.setCursor(0, 1);
lcd.print(ACCurrentValue1);
lcd.print(" ");
lcd.print(setPoint_1);
delay(500);
lcd.setCursor(0, 0);
lcd.print(" AMPS VFD2 TRIP");
lcd.setCursor(0, 1);
lcd.print(ACCurrentValue2);
lcd.print(" ");
lcd.print(setPoint_2);
delay(500);
lcd.home();
lcd.setCursor(0, 0);
lcd.print("< = ADJUST VFD1");
lcd.setCursor(0, 1);
lcd.print("> = ADJUST VFD2");
// delay(250);
delay(500);
lcd.home();
lcd.setCursor(0, 0);
lcd.print("^ = START LOGGING");
lcd.setCursor(0, 1);
lcd.print("v = STOP LOGGING");
delay(500);
if (ACCurrentValue1 >= setPoint_1) {
overCurrent_1_flag = HIGH;
ACCurrentValue1 = ACCurrentValue1;
overCurrent_detected_1();
}
else if (ACCurrentValue2 >= setPoint_2) {
overCurrent_2_flag = HIGH;
ACCurrentValue2 = ACCurrentValue2;
overCurrent_detected_2();
}
lcd.clear();
lcd.noBlink();
if (data_logging_flag == HIGH) {
Log_Data();
}
// Serial.println(data_logging_flag);
}
float readACCurrentValue_1()
{
float ACCurrentValue_1 = 0;
float peakVoltage = 0;
float voltageVirtualValue = 0; //Vrms
// average peakVoltage over 20 readings
for (int i = 0; i < 20; i++)
{
peakVoltage += analogRead(AC1Pin) * 0.318; //read peak voltage
delay(1);
// Serial.println(i);
}
voltageVirtualValue = peakVoltage / 20;
/*The circuit is amplified by 2 times, so it is divided by 2.*/
voltageVirtualValue = ((voltageVirtualValue / 1024) * VREF ) / 2;
ACCurrentValue_1 = voltageVirtualValue * ACTectionRange;
return ACCurrentValue_1;
}
float readACCurrentValue_2()
{
float ACCurrentValue_2 = 0;
float peakVoltage = 0;
float voltageVirtualValue = 0; //Vrms
// average peakVoltage over 20 readings
for (int i = 0; i < 20; i++)
{
peakVoltage += analogRead(AC2Pin) * 0.318; //read peak voltage
delay(1);
// Serial.println(i);
}
voltageVirtualValue = peakVoltage / 20;
/*The circuit is amplified by 2 times, so it is divided by 2.*/
voltageVirtualValue = ((voltageVirtualValue / 1024) * VREF ) / 2;
ACCurrentValue_2 = voltageVirtualValue * ACTectionRange;
return ACCurrentValue_2;
}
void up_selected() {
//Serial.println(x);
if (column[y] == 11) {
x = x + 1;
if (x >= 2) {
x = 0;
} else {
x = x;
}
}
else if (column[y] == 12) {
x = x + 1;
if (x >= 10) {
x = 0;
} else {
x = x;
}
}
else if (column[y] == 13) {
x = x + 1;
if (x >= 10) {
x = 0;
} else {
x = x;
}
}
else if (column[y] == 14) {
x = x + 1;
if (x >= 10) {
x = 0;
} else {
x = x;
}
}
}
void down_selected() {
x = x - 1;
if (x <= 0) {
x = 0;
} else {
x = x;
}
}
void left_selected() {
y = y - 1;
if (y <= 0) {
y = 0;
} else {
y = y;
}
}
void right_selected() {
y = y + 1;
if (y >= 4) {
y = 0;
} else {
y = y;
}
}
void enter_selected() {
lcd.clear();
lcd.print("ENTER BUTTON");
delay(1000);
}
int digitOne_up() {
x = x + 1;
if (x >= 2) {
x = 0;
} else {
x = x;
return x;
}
}
int digitOne_down() {
x = x - 1;
if (x <= 0) {
x = 0;
} else {
x = x;
return x;
}
}
int digitTwo_up() {
w = w + 1;
if (w >= 10) {
w = 0;
} else {
w = w;
return w;
}
}
int digitTwo_down() {
w = w - 1;
if (w <= 0) {
w = 0;
} else {
w = w;
return w;
}
}
int digitThree_up() {
z = z + 1;
if (z >= 10) {
z = 0;
} else {
z = z;
return z;
}
}
int digitThree_down() {
z = z - 1;
if (z <= 0) {
z = 0;
} else {
z = z;
return z;
}
}
int digitFour_up() {
v = v + 1;
if (v >= 10) {
v = 0;
} else {
v = v;
return v;
}
}
int digitFour_down() {
v = v - 1;
if (v <= 0) {
v = 0;
} else {
v = v;
return v;
}
}
void setpoint_1_adjust() {
delay(250);
lcd.clear();
y = 11;
while (digitalRead(enterButton) == HIGH) {
lcd.home();
lcd.setCursor(0, 0);
lcd.print("OVERCURRENT ADJ");
lcd.setCursor(0, 1);
lcd.print("SETPOINT 1:");
lcd.setCursor(column[y], 1);
lcd.blink();
if (digitalRead(upButton) == LOW) {
if (column[y] == 11) {
// up_selected();
digitOne_up();
}
}
if (digitalRead(upButton) == LOW) {
if (column[y] == 12) {
// up_selected();
digitTwo_up();
}
}
if (digitalRead(upButton) == LOW) {
if (column[y] == 13) {
// up_selected();
digitThree_up();
}
}
if (digitalRead(upButton) == LOW) {
if (column[y] == 14) {
// up_selected();
digitFour_up();
}
}
if (digitalRead(downButton) == LOW) {
if (column[y] == 11) {
digitOne_down();
}
}
if (digitalRead(downButton) == LOW) {
if (column[y] == 12) {
digitTwo_down();
}
}
if (digitalRead(downButton) == LOW) {
if (column[y] == 13) {
digitThree_down();
}
}
if (digitalRead(downButton) == LOW) {
if (column[y] == 14) {
digitFour_down();
}
}
else if (digitalRead(leftButton) == LOW) {
left_selected();
}
else if (digitalRead(rightButton) == LOW) {
right_selected();
}
if (column[y] == 11) {
lcd.setCursor(column[y], 1);
lcd.print(digitOne[x]);
// delay(500);
lcd.setCursor(column[y], 1);
}
else if (column[y] == 12) {
lcd.setCursor(column[y], 1);
lcd.print(digitTwo[w]);
lcd.setCursor(column[y], 1);
}
else if (column[y] == 13) {
lcd.setCursor(column[y], 1);
lcd.print(digitThree[z]);
lcd.setCursor(column[y], 1);
}
else if (column[y] == 14) {
lcd.setCursor(column[y], 1);
lcd.print(digitFour[v]);
lcd.setCursor(column[y], 1);
}
delay(250);
lcd.noBlink();
}
setPoint_1 = (((digitOne[x]) * 1000.00) + ((digitTwo[w]) * 100.00) + ((digitThree[z]) * 10.00) + ((digitFour[v]) * 1.00)) / 100.00;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("SAVING....");
lcd.setCursor(3, 1);
lcd.print("SETPOINT 1");
delay(250);
y = 11;
}
void setpoint_2_adjust() {
delay(250);
lcd.clear();
y = 11;
while (digitalRead(enterButton) == HIGH) {
lcd.home();
lcd.setCursor(0, 0);
lcd.print("OVERCURRENT ADJ");
lcd.setCursor(0, 1);
lcd.print("SETPOINT 2:");
lcd.setCursor(column[y], 1);
lcd.blink();
if (digitalRead(upButton) == LOW) {
if (column[y] == 11) {
// up_selected();
digitOne_up();
}
}
if (digitalRead(upButton) == LOW) {
if (column[y] == 12) {
// up_selected();
digitTwo_up();
}
}
if (digitalRead(upButton) == LOW) {
if (column[y] == 13) {
// up_selected();
digitThree_up();
}
}
if (digitalRead(upButton) == LOW) {
if (column[y] == 14) {
// up_selected();
digitFour_up();
}
}
if (digitalRead(downButton) == LOW) {
if (column[y] == 11) {
digitOne_down();
}
}
if (digitalRead(downButton) == LOW) {
if (column[y] == 12) {
digitTwo_down();
}
}
if (digitalRead(downButton) == LOW) {
if (column[y] == 13) {
digitThree_down();
}
}
if (digitalRead(downButton) == LOW) {
if (column[y] == 14) {
digitFour_down();
}
}
else if (digitalRead(leftButton) == LOW) {
left_selected();
}
else if (digitalRead(rightButton) == LOW) {
right_selected();
}
if (column[y] == 11) {
lcd.setCursor(column[y], 1);
lcd.print(digitOne[x]);
lcd.setCursor(column[y], 1);
}
else if (column[y] == 12) {
lcd.setCursor(column[y], 1);
lcd.print(digitTwo[w]);
lcd.setCursor(column[y], 1);
}
else if (column[y] == 13) {
lcd.setCursor(column[y], 1);
lcd.print(digitThree[z]);
lcd.setCursor(column[y], 1);
}
else if (column[y] == 14) {
lcd.setCursor(column[y], 1);
lcd.print(digitFour[v]);
lcd.setCursor(column[y], 1);
}
delay(250);
lcd.noBlink();
}
setPoint_2 = (((digitOne[x]) * 1000.00) + ((digitTwo[w]) * 100.00) + ((digitThree[z]) * 10.00) + ((digitFour[v]) * 1.00)) / 100.00;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("SAVING....");
lcd.setCursor(3, 1);
lcd.print("SETPOINT 2");
delay(250);
y = 11;
}
void overCurrent_detected_1() {
lcd.clear();
lcd.blink();
while (overCurrent_1_flag) {
lcd.setCursor(0, 0);
lcd.print("OVERCURRENT VFD1");
lcd.setCursor(0, 1);
lcd.print("TRIP ");
lcd.print(setPoint_1);
lcd.print(" ");
lcd.print(ACCurrentValue1);
digitalWrite(A0, HIGH);
delay(1000);
if (digitalRead(enterButton) == LOW) {
setpoint_1_adjust();
overCurrent_1_flag = LOW;
digitalWrite(A0, LOW);
}
}
lcd.noBlink();
lcd.clear();
}
void overCurrent_detected_2() {
lcd.clear();
lcd.blink();
while (overCurrent_2_flag) {
lcd.setCursor(0, 0);
lcd.print("OVERCURRENT VFD2");
lcd.setCursor(0, 1);
lcd.print("TRIP ");
lcd.print(setPoint_2);
lcd.print(" ");
lcd.print(ACCurrentValue2);
digitalWrite(A0, HIGH);
delay(1000);
if (digitalRead(enterButton) == LOW) {
setpoint_2_adjust();
overCurrent_2_flag = LOW;
digitalWrite(A0, LOW);
}
}
lcd.noBlink();
lcd.clear();
}
void start_logging() {
data_logging_flag = HIGH;
Serial.println("logging started");
// Serial.println("millis,datetime,VFD1 Current,VFD2 Current");
// create a new file
char filename[] = "LOGGER00.CSV";
/* 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!
}
} */
logfile.println("millis,datetime,VFD1 Current,VFD2 Current");
Serial.println("millis,datetime,VFD1 Current,VFD2 Current");
Serial.println(data_logging_flag);
data_logging_flag = HIGH;
Serial.print("Start_logging");
Serial.println(data_logging_flag);
}
void stop_logging() {
Serial.println("Logging Stopped");
data_logging_flag = LOW;
/* Serial.print("Stop_logging");
Serial.println(data_logging_flag); */
logfile.flush();
}
void Log_Data() {
uint32_t m = millis(); //declare variable for timestamp purposes
// DateTime now;
logfile.print(m); // milliseconds since start
logfile.print(", ");
now = RTC.now();
// log time
logfile.print('"');
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(" ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
logfile.print('"');
logfile.print(", ");
logfile.print(ACCurrentValue1);
logfile.print(", ");
logfile.print(ACCurrentValue2);
logfile.println();
// logfile.flush();
Serial.print(m); // milliseconds since start
Serial.print(", ");
Serial.print('"');
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.print('"');
Serial.print(", ");
Serial.print(ACCurrentValue1);
Serial.print(", ");
Serial.print(ACCurrentValue2);
Serial.println();
Serial.print("Logging Data");
Serial.println(data_logging_flag);
}
[/code]
Preformatted text
[code]