// here i have simple code which reads file from SD card.
// but now i want process different data.
// for example : age45
// so i want create a variable or function named "age" and read file from SD and pass age to that variable or function (like age[]).
// so whenever we read age we update value of age let say previous value is 0 before opening file than as it read file and updates value of age as it changes
// i hope you understand what i'm trying to say.
/** #include <SD.h>
#include <SPI.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
float number = readNumber();
processNumber(number);
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
float readNumber() {
String line = myFile.readStringUntil('\n');
line.trim();
return line.toFloat();
}
void processNumber(float number) {
Serial.println(number);
// Add functionality to process the number as needed
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
} **/
/** #include <SD.h>
#include <SPI.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
String data = readData();
processData(data);
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
String readData() {
return myFile.readStringUntil('\n');
}
void processData(String data) {
data.trim();
Serial.print("Raw data: ");
Serial.println(data);
// Process as float
if (isFloat(data)) {
float number = data.toFloat();
Serial.print("Float: ");
Serial.println(number);
}
// Process as integer
else if (isInteger(data)) {
int number = data.toInt();
Serial.print("Integer: ");
Serial.println(number);
}
// Process as char
else if (data.length() == 1) {
char character = data.charAt(0);
Serial.print("Char: ");
Serial.println(character);
}
// Process as string
else {
Serial.print("String: ");
Serial.println(data);
}
}
bool isFloat(String data) {
char* end;
data.toFloat();
return end != data.c_str() && *end == '\0';
}
bool isInteger(String data) {
char* end;
data.toInt();
return end != data.c_str() && *end == '\0';
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
} **/
/** #include <SD.h>
#include <SPI.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
int age = 0; // Initialize the age variable
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
processLine();
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
void processLine() {
String line = myFile.readStringUntil('\n');
line.trim();
// Check if the line starts with "age"
if (line.startsWith("age")) {
// Extract the number following "age"
String numberStr = line.substring(3);
int number = numberStr.toInt();
age = number; // Update the age variable
Serial.print("Updated age to: ");
Serial.println(age);
} else {
Serial.print("Unknown data: ");
Serial.println(line);
}
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
} **/
/** #include <SD.h>
#include <SPI.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
int age = 0; // Initialize the age variable
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
processLine();
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
void processLine() {
String line = myFile.readStringUntil('\n');
line.trim();
int index = 0;
while ((index = line.indexOf("age", index)) != -1) {
// Skip past "age"
index += 3;
// Extract the number following "age"
String numberStr = "";
while (index < line.length() && isDigit(line[index])) {
numberStr += line[index];
index++;
}
if (numberStr.length() > 0) {
int number = numberStr.toInt();
age = number; // Update the age variable
Serial.print("Updated age to: ");
Serial.println(age);
}
}
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
} **/
// this is good but i have issues with lines below it's not working for that :
// the age of jim is 35 and kelly is 42
// age 6
// age 10
/** #include <SD.h>
#include <SPI.h>
#include <Regexp.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
int age = 0; // Initialize the age variable
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
processLine();
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
void processLine() {
String line = myFile.readStringUntil('\n');
line.trim();
// Regular expression to match "age" followed by any digits
const char* pattern = "age\\s*(\\d+)";
MatchState ms;
ms.Target(line.c_str());
char result = ms.Match(pattern);
while (result == REGEXP_MATCHED) {
char ageStr[10];
ms.GetCapture(ageStr, 0); // Get the matched group
int number = String(ageStr).toInt();
age = number; // Update the age variable
Serial.print("Updated age to: ");
Serial.println(age);
result = ms.Match(pattern, ms.MatchStart + 1); // Move to the next match
}
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
} **/
/** sorry this apparoach didn't work see below output :
SD Card initialized successfully!
Reading TEST.txt:
File closed.
All tasks completed. **/
/** #include <SD.h>
#include <SPI.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
int age = 0; // Initialize the age variable
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
processLine();
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
void processLine() {
String line = myFile.readStringUntil('\n');
line.trim();
int index = 0;
while ((index = line.indexOf("age", index)) != -1) {
// Move past "age" and any following spaces
index += 3;
while (index < line.length() && isSpace(line[index])) {
index++;
}
// Extract the number following "age"
String numberStr = "";
while (index < line.length() && isDigit(line[index])) {
numberStr += line[index];
index++;
}
if (numberStr.length() > 0) {
int number = numberStr.toInt();
age = number; // Update the age variable
Serial.print("Updated age to: ");
Serial.println(age);
}
}
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
} **/
/** thank you it worked for rest but not for below line do you have any solution for that.
the age of jim is 35 and kelly is 42 **/
/** #include <SD.h>
#include <SPI.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
int age = 0; // Initialize the age variable
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
processLine();
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
void processLine() {
String line = myFile.readStringUntil('\n');
line.trim();
int index = 0;
while ((index = line.indexOf("age", index)) != -1) {
// Move past "age"
index += 3;
// Skip non-digit characters until a digit is found
while (index < line.length() && !isDigit(line[index])) {
index++;
}
// Extract the number following "age"
String numberStr = "";
while (index < line.length() && isDigit(line[index])) {
numberStr += line[index];
index++;
}
if (numberStr.length() > 0) {
int number = numberStr.toInt();
age = number; // Update the age variable
Serial.print("Updated age to: ");
Serial.println(age);
}
}
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
} **/
/** #include <SD.h>
#include <SPI.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
float age = 0.0; // Initialize the age variable
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
processLine();
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
void processLine() {
String line = myFile.readStringUntil('\n');
line.trim();
int index = 0;
while ((index = line.indexOf("age", index)) != -1) {
// Move past "age"
index += 3;
// Skip non-digit and non-minus-sign characters until a digit or minus sign is found
while (index < line.length() && !isDigit(line[index]) && line[index] != '-') {
index++;
}
// Extract the number following "age"
String numberStr = "";
bool isNegative = false;
if (index < line.length() && line[index] == '-') {
isNegative = true;
numberStr += line[index];
index++;
}
while (index < line.length() && (isDigit(line[index]) || line[index] == '.')) {
numberStr += line[index];
index++;
}
if (numberStr.length() > 0) {
float number = numberStr.toFloat();
age = number; // Update the age variable
Serial.print("Updated age to: ");
Serial.println(age);
}
}
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
} **/
/** #include <SD.h>
#include <SPI.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
float age = 0.0; // Initialize the age variable
float height = 0.0; // Initialize the height variable
float weight = 0.0; // Initialize the weight variable
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
processLine();
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
void processLine() {
String line = myFile.readStringUntil('\n');
line.trim();
processKeyword(line, "age", age);
processKeyword(line, "height", height);
processKeyword(line, "weight", weight);
}
void processKeyword(String& line, const String& keyword, float& variable) {
int index = 0;
while ((index = line.indexOf(keyword, index)) != -1) {
// Move past the keyword
index += keyword.length();
// Skip non-digit and non-minus-sign characters until a digit or minus sign is found
while (index < line.length() && !isDigit(line[index]) && line[index] != '-') {
index++;
}
// Extract the number following the keyword
String numberStr = "";
if (index < line.length() && line[index] == '-') {
numberStr += line[index];
index++;
}
while (index < line.length() && (isDigit(line[index]) || line[index] == '.')) {
numberStr += line[index];
index++;
}
if (numberStr.length() > 0) {
variable = numberStr.toFloat(); // Update the variable
Serial.print("Updated ");
Serial.print(keyword);
Serial.print(" to: ");
Serial.println(variable);
}
}
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
} **/
/** #include <SD.h>
#include <SPI.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
float age = 0.0; // Initialize the age variable
float height = 0.0; // Initialize the height variable
float weight = 0.0; // Initialize the weight variable
bool ageUpdated = false; // Flag to track if age is updated
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
processLine();
if (ageUpdated) {
printAgeCategory(static_cast<int>(age)); // Print age category after update
ageUpdated = false; // Reset the flag
}
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
void processLine() {
String line = myFile.readStringUntil('\n');
line.trim();
processKeyword(line, "age", age, ageUpdated);
processKeyword(line, "height", height, ageUpdated);
processKeyword(line, "weight", weight, ageUpdated);
}
void processKeyword(String& line, const String& keyword, float& variable, bool& updatedFlag) {
int index = 0;
while ((index = line.indexOf(keyword, index)) != -1) {
// Move past the keyword
index += keyword.length();
// Skip non-digit and non-minus-sign characters until a digit or minus sign is found
while (index < line.length() && !isDigit(line[index]) && line[index] != '-') {
index++;
}
// Extract the number following the keyword
String numberStr = "";
if (index < line.length() && line[index] == '-') {
numberStr += line[index];
index++;
}
while (index < line.length() && (isDigit(line[index]) || line[index] == '.')) {
numberStr += line[index];
index++;
}
if (numberStr.length() > 0) {
float newValue = numberStr.toFloat();
if (newValue != variable) { // Check if the value has changed
variable = newValue; // Update the variable
updatedFlag = true; // Set the flag
//Serial.print("Updated ");
//Serial.print(keyword);
//Serial.print(" to: ");
//Serial.println(variable);
}
}
}
}
void printAgeCategory(int age) {
switch (age) {
case -42:
Serial.println("Mid-aged");
break;
case 20:
Serial.println("Young");
break;
case 6:
Serial.println("Child");
break;
default:
break;
}
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
} **/
/** #include <SD.h>
#include <SPI.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
float age = 0.0; // Initialize the age variable
float height = 0.0; // Initialize the height variable
float weight = 0.0; // Initialize the weight variable
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
processLine();
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
void processLine() {
String line = myFile.readStringUntil('\n');
line.trim();
processKeyword(line, "age", age);
processKeyword(line, "height", height);
processKeyword(line, "weight", weight);
}
void processKeyword(String& line, const String& keyword, float& variable) {
int index = 0;
while ((index = line.indexOf(keyword, index)) != -1) {
// Move past the keyword
index += keyword.length();
// Skip non-digit and non-minus-sign characters until a digit or minus sign is found
while (index < line.length() && !isDigit(line[index]) && line[index] != '-') {
index++;
}
// Extract the number following the keyword
String numberStr = "";
if (index < line.length() && line[index] == '-') {
numberStr += line[index];
index++;
}
while (index < line.length() && (isDigit(line[index]) || line[index] == '.')) {
numberStr += line[index];
index++;
}
if (numberStr.length() > 0) {
float newValue = numberStr.toFloat();
if (newValue != variable) { // Check if the value has changed
variable = newValue; // Update the variable
//Serial.print("Updated ");
//Serial.print(keyword);
//Serial.print(" to: ");
//Serial.println(variable);
if (keyword == "age") {
printAgeCategory(static_cast<int>(variable)); // Print age category immediately after update
}
}
}
}
}
void printAgeCategory(int age) {
switch (age) {
case 42:
Serial.println("Mid-aged");
break;
case 20:
Serial.println("Young");
break;
case 6:
Serial.println("Child");
break;
default:
break;
}
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
} **/
/** #include <SD.h>
#include <SPI.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
float age = 0.0; // Initialize the age variable
float height = 0.0; // Initialize the height variable
float weight = 0.0; // Initialize the weight variable
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
processLine();
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
void processLine() {
String line = myFile.readStringUntil('\n');
line.trim();
processKeyword(line, "age", age);
processKeyword(line, "height", height);
processKeyword(line, "weight", weight);
}
bool isValidKeyword(String& line, int index, const String& keyword) {
// Ensure that the character following the keyword is a space or a digit
int endIdx = index + keyword.length();
if (endIdx < line.length() && (isDigit(line[endIdx]) || isSpace(line[endIdx]))) {
return true;
}
return false;
}
void processKeyword(String& line, const String& keyword, float& variable) {
int index = 0;
while ((index = line.indexOf(keyword, index)) != -1) {
// Check if the found keyword is valid
if (!isValidKeyword(line, index, keyword)) {
index += keyword.length();
continue;
}
// Move past the keyword
index += keyword.length();
// Skip non-digit and non-minus-sign characters until a digit or minus sign is found
while (index < line.length() && !isDigit(line[index]) && line[index] != '-') {
index++;
}
// Extract the number following the keyword
String numberStr = "";
if (index < line.length() && line[index] == '-') {
numberStr += line[index];
index++;
}
while (index < line.length() && (isDigit(line[index]) || line[index] == '.')) {
numberStr += line[index];
index++;
}
if (numberStr.length() > 0) {
float newValue = numberStr.toFloat();
if (newValue != variable) { // Check if the value has changed
variable = newValue; // Update the variable
Serial.print("Updated ");
Serial.print(keyword);
Serial.print(" to: ");
Serial.println(variable);
if (keyword == "age") {
printAgeCategory(variable); // Print age category immediately after update
}
}
}
}
}
void printAgeCategory(float age) {
Serial.print("Age category: ");
Serial.println(age);
if (age == 42.0) {
Serial.println("Mid-aged");
} else if (age == 20.0) {
Serial.println("Young");
} else if (age == 6.0) {
Serial.println("Child");
} else {
Serial.println("Age category not recognized");
}
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
} **/
/** #include <SD.h>
#include <SPI.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
float age = 0.0; // Initialize the age variable
float height = 0.0; // Initialize the height variable
float weight = 0.0; // Initialize the weight variable
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
processLine();
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
void processLine() {
String line = myFile.readStringUntil('\n');
line.trim();
processKeyword(line, "age", age);
processKeyword(line, "height", height);
processKeyword(line, "weight", weight);
}
bool isValidKeyword(String& line, int index, const String& keyword) {
// Ensure that the character following the keyword is a space or a digit
int endIdx = index + keyword.length();
if (endIdx < line.length() && (isDigit(line[endIdx]) || isSpace(line[endIdx]))) {
return true;
}
return false;
}
void processKeyword(String& line, const String& keyword, float& variable) {
int index = 0;
while ((index = line.indexOf(keyword, index)) != -1) {
// Check if the found keyword is valid
if (!isValidKeyword(line, index, keyword)) {
index += keyword.length();
continue;
}
// Move past the keyword
index += keyword.length();
// Skip non-digit and non-minus-sign characters until a digit or minus sign is found
while (index < line.length() && !isDigit(line[index]) && line[index] != '-') {
index++;
}
// Extract the number following the keyword
String numberStr = "";
if (index < line.length() && line[index] == '-') {
numberStr += line[index];
index++;
}
while (index < line.length() && (isDigit(line[index]) || line[index] == '.')) {
numberStr += line[index];
index++;
}
if (numberStr.length() > 0) {
float newValue = numberStr.toFloat();
if (newValue != variable) { // Check if the value has changed
variable = newValue; // Update the variable
//Serial.print("Updated ");
//Serial.print(keyword);
//Serial.print(" to: ");
//Serial.println(variable);
if ((keyword == "age") || (keyword == "height") || (keyword == "weight")) {
printAgeCategory(age, height, weight); // Print age category immediately after update
}
}
}
}
}
void printAgeCategory(float age, float height, float weight) {
Serial.print(" Age: ");
Serial.print(age);
Serial.print(" Height: ");
Serial.print(height);
Serial.print(" Weight: ");
Serial.println(weight);
if (age == 42.0) {
Serial.println("Mid-aged");
} else if (age == 20.0) {
Serial.println("Young");
} else if (age == 6.0) {
Serial.println("Child");
} else {
Serial.println("Age category not recognized");
}
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
} **/
/** #include <SD.h>
#include <SPI.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
float age = 0.0; // Initialize the age variable
float height = 0.0; // Initialize the height variable
float weight = 0.0; // Initialize the weight variable
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
processLine();
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
void processLine() {
String line = myFile.readStringUntil('\n');
line.trim();
processKeyword(line, "age", age);
processKeyword(line, "height", height);
processKeyword(line, "weight", weight);
}
bool isValidKeyword(String& line, int index, const String& keyword) {
// Ensure that the character following the keyword is a space, a minus, or a digit
int endIdx = index + keyword.length();
if (endIdx < line.length() && (isDigit(line[endIdx]) || isSpace(line[endIdx]) || line[endIdx] == '-')) {
return true;
}
return false;
}
void processKeyword(String& line, const String& keyword, float& variable) {
int index = 0;
while ((index = line.indexOf(keyword, index)) != -1) {
// Check if the found keyword is valid
if (!isValidKeyword(line, index, keyword)) {
index += keyword.length();
continue;
}
// Move past the keyword
index += keyword.length();
// Skip non-digit and non-minus-sign characters until a digit or minus sign is found
while (index < line.length() && !isDigit(line[index]) && line[index] != '-') {
index++;
}
// Extract the number following the keyword
String numberStr = "";
if (index < line.length() && line[index] == '-') {
numberStr += line[index];
index++;
}
while (index < line.length() && (isDigit(line[index]) || line[index] == '.')) {
numberStr += line[index];
index++;
}
if (numberStr.length() > 0) {
float newValue = numberStr.toFloat();
if (newValue != variable) { // Check if the value has changed
variable = newValue; // Update the variable
//Serial.print("Updated ");
//Serial.print(keyword);
//Serial.print(" to: ");
//Serial.println(variable);
if (keyword == "age") {
printAgeCategory(age, height, weight); // Print age category immediately after update
}
}
}
}
}
void printAgeCategory(float age, float height, float weight) {
Serial.print(" Age: ");
Serial.print(age);
Serial.print(" Height: ");
Serial.print(height);
Serial.print(" Weight: ");
Serial.println(weight);
if (age == 42.0) {
Serial.println("Mid-aged");
} else if (age == 20.0) {
Serial.println("Young");
} else if (age == 6.0) {
Serial.println("Child");
} else {
Serial.println("Age category not recognized");
}
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
} **/
/** #include <SD.h>
#include <SPI.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
float age = 0.0; // Initialize the age variable
float height = 0.0; // Initialize the height variable
float weight = 0.0; // Initialize the weight variable
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
processLine();
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
void processLine() {
String line = myFile.readStringUntil('\n');
line.trim();
processKeyword(line, "age", age);
processKeyword(line, "height", height);
processKeyword(line, "weight", weight);
}
bool isWholeWord(String& line, int index, const String& keyword) {
int endIdx = index + keyword.length();
bool validStart = (index == 0 || !isalnum(line[index - 1]));
bool validEnd = (endIdx == line.length() || !isalnum(line[endIdx]));
return validStart && validEnd;
}
void processKeyword(String& line, const String& keyword, float& variable) {
int index = 0;
while ((index = line.indexOf(keyword, index)) != -1) {
// Check if the found keyword is a whole word
if (!isWholeWord(line, index, keyword)) {
index += keyword.length();
continue;
}
// Move past the keyword
index += keyword.length();
// Skip non-digit and non-minus-sign characters until a digit or minus sign is found
while (index < line.length() && !isDigit(line[index]) && line[index] != '-') {
index++;
}
// Extract the number following the keyword
String numberStr = "";
if (index < line.length() && line[index] == '-') {
numberStr += line[index];
index++;
}
while (index < line.length() && (isDigit(line[index]) || line[index] == '.')) {
numberStr += line[index];
index++;
}
if (numberStr.length() > 0) {
float newValue = numberStr.toFloat();
if (newValue != variable) { // Check if the value has changed
variable = newValue; // Update the variable
//Serial.print("Updated ");
//Serial.print(keyword);
//Serial.print(" to: ");
//Serial.println(variable);
if ((keyword == "age") || (keyword == "height") || (keyword == "weight")) {
printAgeCategory(age, height, weight); // Print age category immediately after update
}
}
}
}
}
void printAgeCategory(float age, float height, float weight) {
Serial.print(" Age: ");
Serial.print(age);
Serial.print(" Height: ");
Serial.print(height);
Serial.print(" Weight: ");
Serial.println(weight);
if (age == 42.0) {
Serial.println("Mid-aged");
} else if (age == 20.0) {
Serial.println("Young");
} else if (age == 6.0) {
Serial.println("Child");
} else {
Serial.println("Age category not recognized");
}
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
} **/
/** #include <SD.h>
#include <SPI.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
float age = 0.0; // Initialize the age variable
float height = 0.0; // Initialize the height variable
float weight = 0.0; // Initialize the weight variable
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
processLine();
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
void processLine() {
String line = myFile.readStringUntil('\n');
line.trim();
processKeyword(line, "age", age);
processKeyword(line, "height", height);
processKeyword(line, "weight", weight);
}
bool isWholeWord(String& line, int index, const String& keyword) {
int endIdx = index + keyword.length();
bool validStart = (index == 0 || !isalnum(line[index - 1]));
bool validEnd = (endIdx == line.length() || !isalnum(line[endIdx]) || isDigit(line[endIdx]));
return validStart && validEnd;
}
void processKeyword(String& line, const String& keyword, float& variable) {
int index = 0;
while ((index = line.indexOf(keyword, index)) != -1) {
// Check if the found keyword is a whole word
if (!isWholeWord(line, index, keyword)) {
index += keyword.length();
continue;
}
// Move past the keyword
index += keyword.length();
// Skip non-digit and non-minus-sign characters until a digit or minus sign is found
while (index < line.length() && !isDigit(line[index]) && line[index] != '-') {
index++;
}
// Extract the number following the keyword
String numberStr = "";
if (index < line.length() && line[index] == '-') {
numberStr += line[index];
index++;
}
while (index < line.length() && (isDigit(line[index]) || line[index] == '.')) {
numberStr += line[index];
index++;
}
if (numberStr.length() > 0) {
float newValue = numberStr.toFloat();
if (newValue != variable) { // Check if the value has changed
variable = newValue; // Update the variable
//Serial.print("Updated ");
//Serial.print(keyword);
//Serial.print(" to: ");
//Serial.println(variable);
if ((keyword == "age") || (keyword == "height") || (keyword == "weight")) {
printAgeCategory(age, height, weight); // Print age category immediately after update
}
}
}
}
}
void printAgeCategory(float age, float height, float weight) {
Serial.print(" Age: ");
Serial.print(age);
Serial.print(" Height: ");
Serial.print(height);
Serial.print(" Weight: ");
Serial.println(weight);
if (age == 42.0) {
Serial.println("Mid-aged");
} else if (age == 20.0) {
Serial.println("Young");
} else if (age == 6.0) {
Serial.println("Child");
} else {
Serial.println("Age category not recognized");
}
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
} **/
#include <SD.h>
#include <SPI.h>
File myFile;
const int sdPin = 2; // SD card pin
const char* filename = "TEST.txt"; // Default filename
int maxRetries = 3; // Maximum number of retries for SD card and file operations
bool sdInitialized = false;
bool fileOpened = false;
float age = 0.0; // Initialize the age variable
float height = 0.0; // Initialize the height variable
float weight = 0.0; // Initialize the weight variable
float previousAge = 0.0; // Previous value of age
float previousHeight = 0.0; // Previous value of height
float previousWeight = 0.0; // Previous value of weight
float previousAgeDiff = 0.0; // Previous difference in age
float previousHeightDiff = 0.0; // Previous difference in height
float previousWeightDiff = 0.0; // Previous difference in weight
void setup() {
Serial.begin(9600);
pinMode(sdPin, OUTPUT);
initializeSDCard();
if (sdInitialized) {
openFile(filename);
}
}
void loop() {
if (fileOpened) {
while (myFile.available()) {
processLine();
printDifferences();
//printAgeCategory();
delay(2000); // Delay of 2 seconds after processing each line
}
closeFile();
fileOpened = false;
Serial.println("All tasks completed.");
while (true) {} // Stop further executions
}
}
void initializeSDCard() {
int attempt = 0;
while (attempt < maxRetries && !sdInitialized) {
if (!SD.begin(sdPin)) {
Serial.println("SD Card initialization failed! Retrying...");
delay(500); // Wait before retrying
attempt++;
} else {
Serial.println("SD Card initialized successfully!");
sdInitialized = true;
}
}
}
void openFile(const char* filename) {
int attempt = 0;
while (attempt < maxRetries && !fileOpened) {
if (SD.exists(filename)) {
myFile = SD.open(filename);
if (myFile) {
Serial.print("Reading ");
Serial.print(filename);
Serial.println(":");
fileOpened = true;
} else {
Serial.print("Error opening ");
Serial.println(filename);
delay(500);
attempt++;
}
} else {
Serial.print(filename);
Serial.println(" does not exist.");
break;
}
}
}
void processLine() {
String line = myFile.readStringUntil('\n');
line.trim();
processKeyword(line, "age", age, previousAge);
processKeyword(line, "height", height, previousHeight);
processKeyword(line, "weight", weight, previousWeight);
//printDifferences();
delay(2000); // Delay of 2 seconds after processing each line
}
bool isWholeWord(String& line, int index, const String& keyword) {
int endIdx = index + keyword.length();
bool validStart = (index == 0 || !isalnum(line[index - 1]));
bool validEnd = (endIdx == line.length() || !isalnum(line[endIdx]) || isDigit(line[endIdx]));
return validStart && validEnd;
}
void processKeyword(String& line, const String& keyword, float& variable, float& previousVariable) {
int index = 0;
while ((index = line.indexOf(keyword, index)) != -1) {
// Check if the found keyword is a whole word
if (!isWholeWord(line, index, keyword)) {
index += keyword.length();
continue;
}
// Move past the keyword
index += keyword.length();
// Skip non-digit and non-minus-sign characters until a digit or minus sign is found
while (index < line.length() && !isDigit(line[index]) && line[index] != '-') {
index++;
}
// Extract the number following the keyword
String numberStr = "";
if (index < line.length() && line[index] == '-') {
numberStr += line[index];
index++;
}
while (index < line.length() && (isDigit(line[index]) || line[index] == '.')) {
numberStr += line[index];
index++;
}
if (numberStr.length() > 0) {
float newValue = numberStr.toFloat();
if (newValue != variable) { // Check if the value has changed
previousVariable = variable; // Store previous value
variable = newValue; // Update the variable
//Serial.print("Updated ");
//Serial.print(keyword);
//Serial.print(" to: ");
//Serial.println(variable);
//printDifferences(); // Print differences immediately after update
//if ((keyword == "age") || (keyword == "height") || (keyword == "weight")) {
//printAgeCategory(); // Print age category immediately after update
//}
}
}
}
}
void printDifferences() {
float ageDiff = age - previousAge;
float heightDiff = height - previousHeight;
float weightDiff = weight - previousWeight;
//Serial.print("Age: ");
//Serial.println(age);
//Serial.print("Height: ");
//Serial.println(height);
//Serial.print("Weight: ");
//Serial.println(weight);
// Print differences
Serial.print("Difference in age: ");
Serial.println(ageDiff);
Serial.print("Difference in height: ");
Serial.println(heightDiff);
Serial.print("Difference in weight: ");
Serial.println(weightDiff);
// Check and print age difference conditions
if (ageDiff > 0) {
Serial.println("Age difference is greater than zero");
} else if (ageDiff < 0) {
Serial.println("Age difference is smaller than zero");
} else {
Serial.println("Age difference is zero");
}
if (ageDiff > previousAgeDiff) {
Serial.println("Change in age difference is greater than previous");
} else if (ageDiff < previousAgeDiff) {
Serial.println("Change in age difference is smaller than previous");
} else {
Serial.println("No change in age difference");
}
// Check and print height difference conditions
if (heightDiff > 0) {
Serial.println("Height difference is greater than zero");
} else if (heightDiff < 0) {
Serial.println("Height difference is smaller than zero");
} else {
Serial.println("Height difference is zero");
}
if (heightDiff > previousHeightDiff) {
Serial.println("Change in height difference is greater than previous");
} else if (heightDiff < previousHeightDiff) {
Serial.println("Change in height difference is smaller than previous");
} else {
Serial.println("No change in height difference");
}
// Check and print weight difference conditions
if (weightDiff > 0) {
Serial.println("Weight difference is greater than zero");
} else if (weightDiff < 0) {
Serial.println("Weight difference is smaller than zero");
} else {
Serial.println("Weight difference is zero");
}
if (weightDiff > previousWeightDiff) {
Serial.println("Change in weight difference is greater than previous");
} else if (weightDiff < previousWeightDiff) {
Serial.println("Change in weight difference is smaller than previous");
} else {
Serial.println("No change in weight difference");
}
// Update previous differences
previousAgeDiff = ageDiff;
previousHeightDiff = heightDiff;
previousWeightDiff = weightDiff;
}
void printAgeCategory() {
Serial.print("Age: ");
Serial.println(age);
Serial.print("Height: ");
Serial.println(height);
Serial.print("Weight: ");
Serial.println(weight);
if (age == 42.0) {
Serial.println("Mid-aged");
} else if (age == 20.0) {
Serial.println("Young");
} else if (age == 6.0) {
Serial.println("Child");
} else {
Serial.println("Age category not recognized");
}
}
void closeFile() {
myFile.close();
Serial.println("File closed.");
}