This code sensor data display and store to SD card. below code is working. but there is problem. after uncomment the if condition inside the go_to_menu_mode() function. It shows SD card attached but Error opening file in Serial monitor. Is it possible? What is the reason for that?
#include <SD.h>
#include <SPI.h>
#include <TFT_eSPI.h>
#include <HardwareSerial.h>
#include "logo.h"
#define RE 2
#define DE 2
#define RO 13 // Modbus -> RX on GPIO13
#define DI 15 // Modbus -> TX on GPIO15
#define EN 25
#define PWR_EN 12
#define CS_SD 5 // Chip select pin for SD card
#define CS_TFT 4 // Chip select pin for display
#define BTN_1 32 // S2
#define BACK_BTN 33 // S3
#define DWN_BTN 35 // S4
#define OK_BTN 34 // S5
#define WIFI_BTN 39 // S6
#define PWR_AND_UP_BTN 36 // S7
#define powerOnTimeOut 1000
#define powerOffTimeOut 2000
bool btnPressed = 0;
unsigned long currentMillis = 0;
int btnDebounceDelay = 200;
#define TFT_GRAY 0x5AEB // Define background colour
TFT_eSPI tft = TFT_eSPI();
HardwareSerial modSerial(2); // Use UART2 which allows you to set custom pins to serial
// Define mod addresses
const byte nitro[] = { 0x01, 0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c };
const byte phos[] = { 0x01, 0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc };
const byte pota[] = { 0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0 };
const byte soil_ph[] = { 0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0b };
const byte soil_moist[] = { 0x01, 0x03, 0x00, 0x12, 0x00, 0x01, 0x24, 0x0f };
const byte elec_conduct[] = { 0x01, 0x03, 0x00, 0x15, 0x00, 0x01, 0x95, 0xce };
const byte soil_temp[] = { 0x01, 0x03, 0x00, 0x13, 0x00, 0x01, 0x75, 0xcf };
byte values[11];
void setup() {
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
pinMode(EN, OUTPUT);
pinMode(PWR_AND_UP_BTN, INPUT);
pinMode(BTN_1, INPUT);
pinMode(BACK_BTN, INPUT);
pinMode(DWN_BTN, INPUT);
pinMode(OK_BTN, INPUT);
pinMode(WIFI_BTN, INPUT);
pinMode(PWR_EN, OUTPUT);
pinMode(CS_TFT, OUTPUT);
pinMode(CS_SD, OUTPUT);
digitalWrite(EN, HIGH);
digitalWrite(CS_TFT, HIGH);
digitalWrite(CS_SD, HIGH);
delay(powerOnTimeOut);
digitalWrite(PWR_EN, HIGH);
while (digitalRead(PWR_AND_UP_BTN))
;
Serial.begin(115200);
modSerial.begin(9600, SERIAL_8N1, RO, DI);
SPI.begin();
// Initialize TFT
digitalWrite(CS_TFT, LOW);
delay(100);
tft.init();
tft.setRotation(3);
tft.fillScreen(TFT_WHITE);
tft.setSwapBytes(true);
tft.pushImage(60, 0, 200, 200, logo);
delay(200);
tft.setCursor(50, 220, 4);
tft.setTextColor(TFT_YELLOW);
tft.setFreeFont(&FreeSansBold12pt7b);
auto_typing_text("ELZIAN AGRO EYE");
tft.setFreeFont(NULL);
delay(1000);
digitalWrite(CS_TFT, HIGH);
delay(100);
// Initialize SD caed
digitalWrite(CS_SD, LOW);
delay(100);
while (!SD.begin(CS_SD)) {
Serial.println("SD card mount failed, retrying...");
delay(1000);
}
digitalWrite(CS_SD, HIGH);
delay(100);
}
void loop() {
go_to_menu_modes();
}
// Handle menu
void go_to_menu_modes() {
String menuOptions[] = { "Measuring", "Records", "Upload to Internet", "Wifi Settings" }; // Can add more options
int currentMode = 0;
int maxModes = sizeof(menuOptions) / sizeof(menuOptions[0]);
while (true) {
digitalWrite(CS_TFT, LOW);
delay(100);
clear_screen();
print_header();
tft.setCursor(130, 40, 4);
tft.setTextColor(TFT_GREEN);
tft.setFreeFont(&FreeSansBold12pt7b);
tft.println("Menu");
tft.setFreeFont(NULL);
tft.setTextColor(TFT_BLUE);
tft.setTextFont(4);
// for (int i = 0; i < maxModes; i++) {
// if (i == currentMode) {
// tft.setTextColor(TFT_YELLOW); // Highlight current mode
// } else {
// tft.setTextColor(TFT_BLUE);
// }
// tft.println(" " + String(i + 1) + ". " + menuOptions[i]);
// }
digitalWrite(CS_TFT, HIGH);
delay(100);
digitalWrite(CS_SD, LOW);
delay(100);
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
} else {
Serial.println("SD card attached");
if (SD.exists("/data.txt")) {
Serial.println("File exists");
} else {
Serial.println("File does not exist, creating file...");
}
File file = SD.open("/data.txt", FILE_WRITE);
if (file) {
file.println("kavinda");
file.close();
Serial.println("data written to file");
} else {
Serial.println("Error opening file for writing");
}
}
digitalWrite(CS_SD, HIGH);
delay(100);
delay(2000);
}
}