Hi, I'm developing a watch and I'm trying to load images from a micro sd, but it gives me the following error:
Show your proof.
Here is the original sketch, deleted by OP...
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <SPI.h>
#include <SD.h>
#include <Fonts/FreeSansBold18pt7b.h>
#include <Wire.h>
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <TimeLib.h>
#include <cst816t.h>
#define TFT_CS_LCD 9
#define TFT_RST_LCD 8
#define TFT_DC_LCD 10
#define TFT_MOSI_LCD 3
#define TFT_SCLK_LCD 46
#define SCK_MIC_SD 4
#define MISO_MIC_SD 17
#define MOSI_MIC_SD 11
#define CS_MIC_SD 18
String wifiIcon = "";
String btIcon = "";
String cuoreIcon = "";
String stepIcon = "";
#define TP_SCL 16
#define TP_SDA 15
#define TP_RST 7 // Pin di reset
#define TP_IRQ 6 // Pin di interrupt
#define TP_ADDR 0x15
#define TP532 0x24
#define MPU6050 0x68
#define MAX30102 0x57
const char* ssid = "";
const char* password = "";
int16_t lastY = -1;
// Variables to store the last date and time values
int lastYear = 0;
int lastMonth = 0;
int lastDay = 0;
int lastHour = 0;
int lastMinute = 0;
const float referenceVoltage = 3.3;
const float resistorDivider = 2.0;
#define batteryPin 5
int ultimo_aggiornamento = -20000;
bool microsd = false;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 7200, 60000);
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS_LCD, TFT_DC_LCD, TFT_MOSI_LCD, TFT_SCLK_LCD, TFT_RST_LCD);
TwoWire Wire2 = TwoWire(1); // 1 è il bus I2C secondario
cst816t touchpad(Wire2, TP_RST, TP_IRQ);
int pagina_utente = 0; //home page = 0, settings = 1
void disegna_icona(String iconData, int x_start, int y_start, int width, int height, uint16_t color) {
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
int index = row * width + col;
if (index >= iconData.length()) continue;
char pixel = iconData.charAt(index);
if (pixel == '1') {
tft.drawPixel(x_start + col, y_start + row, ST77XX_BLACK);
} else {
tft.drawPixel(x_start + col, y_start + row, color);
}
}
}
}
String readImageFile(const char* filename) {
String imageData = "";
File file = SD.open(filename, FILE_READ);
if (!file) {
Serial.print("Errore nell'aprire il file ");
Serial.println(filename);
return "";
}
while (file.available()) {
char c = file.read();
if (c == '0' || c == '1') {
imageData += c;
}
}
file.close();
Serial.print("File caricato: ");
Serial.println(filename);
return imageData;
}
void stato_batteria() {
int adcValue = analogRead(batteryPin); // Legge il valore ADC
float voltage = adcValue * referenceVoltage / 4095.0 * resistorDivider; // Calcola la tensione della batteria
// Calcola la percentuale di batteria (considerando 4.2V come 100% e 3V come 0%)
int batteryPercentage = (voltage - 3) * 100.0 / (4.1 - 3);
// Limita la percentuale tra 0 e 100%
if (batteryPercentage > 100.0) batteryPercentage = 100.0;
if (batteryPercentage < 0.0) batteryPercentage = 0.0;
int larghezza = map(batteryPercentage, 0, 100, 0, 26);
tft.fillRect(180, 13, 30, 20, ST77XX_WHITE);
tft.fillRect(210, 18, 7, 12, ST77XX_WHITE);
tft.fillRect(182, 15, 26, 16, ST77XX_BLACK);
tft.fillRect(182, 15, larghezza, 16, ST77XX_GREEN);
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(2);
if (batteryPercentage > 99) {
tft.setCursor(130, 15);
} else if (batteryPercentage < 10) {
tft.setCursor(150, 15);
} else {
tft.setCursor(135, 15);
}
tft.fillRect(130, 15, 50, 20, ST77XX_BLACK);
tft.print(batteryPercentage);
tft.print("%");
}
void wifi(bool stato) {
if (stato) {
disegna_icona(wifiIcon, 20, 10, 24, 24, tft.color565(0, 191, 255));
} else {
disegna_icona(wifiIcon, 20, 10, 24, 24, tft.color565(128, 128, 128));
}
}
void bluetooth(bool stato) {
if (stato) {
disegna_icona(btIcon, 50, 10, 16, 24, tft.color565(0, 191, 255));
} else {
disegna_icona(btIcon, 50, 10, 16, 24, tft.color565(128, 128, 128));
}
}
void passi(int passi) {
tft.setTextSize(2);
if (passi > 9999) {
tft.setCursor(35, 205);
} else if (passi > 999) {
tft.setCursor(42, 205);
} else if (passi > 99) {
tft.setCursor(47, 205);
} else {
tft.setCursor(55, 205);
}
tft.fillRect(35, 205, 80, 14, ST77XX_BLACK);
tft.print(passi);
}
void data_ora() {
int currentYear = year();
int currentMonth = month();
int currentDay = day();
int currentHour = hour();
int currentMinute = minute();
if (currentYear != lastYear || currentMonth != lastMonth || currentDay != lastDay || currentHour != lastHour || currentMinute != lastMinute) {
Serial.printf("Data e ora aggiornate: %02d:%02d %02d/%02d/%04d\n", currentHour, currentMinute, currentDay, currentMonth, currentYear);
tft.fillRect(35, 70, 190, 100, ST77XX_BLACK);
tft.setTextSize(2);
tft.setFont(&FreeSansBold18pt7b);
tft.setTextColor(ST77XX_WHITE);
tft.setCursor(35, 120);
String hourStr = currentHour < 10 ? "0" + String(currentHour) : String(currentHour);
String minuteStr = currentMinute < 10 ? "0" + String(currentMinute) : String(currentMinute);
tft.print(hourStr + ":" + minuteStr);
tft.setTextSize(2);
tft.setFont();
tft.setTextColor(ST77XX_WHITE);
tft.setCursor(60, 140);
tft.print(currentDay < 10 ? "0" + String(currentDay) : String(currentDay));
tft.print("/");
tft.print(currentMonth < 10 ? "0" + String(currentMonth) : String(currentMonth));
tft.print("/");
tft.print(currentYear);
lastYear = currentYear;
lastMonth = currentMonth;
lastDay = currentDay;
lastHour = currentHour;
lastMinute = currentMinute;
}
}
void Home_page() {
tft.drawRoundRect(10, 8, 222, 30, 20, ST77XX_WHITE);
tft.drawRoundRect(7, 180, 113, 90, 33, ST77XX_WHITE);
tft.drawRoundRect(123, 180, 113, 90, 33, ST77XX_WHITE);
if (microsd) {
disegna_icona(stepIcon, 50, 226, 32, 40, ST77XX_WHITE);
disegna_icona(cuoreIcon, 157, 210, 46, 40, ST77XX_RED);
} else {
Serial.println("Errore: non è possibile caricare le icone dalla scheda SD.");
}
wifi(1);
bluetooth(1);
passi(99999);
if (millis() - ultimo_aggiornamento > 10 * 1000) {
ultimo_aggiornamento = millis(); // Aggiorna il timestamp
stato_batteria(); // Chiama la funzione desiderata
}
data_ora();
}
void impostazioni() {
tft.fillScreen(ST77XX_BLACK);
tft.setTextSize(2);
tft.setTextColor(ST77XX_WHITE);
tft.setCursor(35, 120);
tft.print("Impostazioni");
delay(2000);
}
void setup() {
Serial.begin(115200);
SD.begin(CS_MIC_SD, SPI, 1000000); // Usa 1 MHz (1000000 Hz)
analogReadResolution(12);
Wire2.begin(TP_SDA, TP_SCL);
touchpad.begin(mode_touch);
WiFi.begin(ssid, password);
int retryCount = 0;
bool wifiConnected = false;
while (WiFi.status() != WL_CONNECTED && retryCount < 20) {
delay(500);
Serial.print(".");
retryCount++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connesso a Wi-Fi");
timeClient.begin();
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime();
setTime(epochTime);
int currentYear = year();
int currentMonth = month();
int currentDay = day();
int currentHour = hour();
int currentMinute = minute();
int currentSecond = second();
Serial.printf("Orario ricevuto: %02d:%02d:%02d %02d/%02d/%04d\n", currentHour, currentMinute, currentSecond, currentDay, currentMonth, currentYear);
} else {
Serial.println("Connessione Wi-Fi fallita. Impostazione data/ora predefiniti.");
setTime(0);
}
delay(1000);
digitalWrite(TFT_CS_LCD, HIGH);
digitalWrite(CS_MIC_SD, LOW);
SPI.begin(SCK_MIC_SD, MISO_MIC_SD, MOSI_MIC_SD, CS_MIC_SD);
int retrySD = 0;
while (!SD.begin(CS_MIC_SD) && retrySD < 5) {
Serial.println("Errore nell'inizializzazione della scheda SD! Riprovo...");
delay(1000);
retrySD++;
}
if (retrySD == 5) {
Serial.println("Errore nell'inizializzazione della scheda SD! Verifica la connessione hardware.");
microsd = false;
} else {
microsd = true;
if (!SD.exists("/wifi.txt") || !SD.exists("/bluetooth.txt") || !SD.exists("/cuore.txt") || !SD.exists("/step.txt")) {
Serial.println("Errore: File richiesti non trovati sulla scheda SD!");
microsd = false;
} else {
Serial.println("Scheda SD inizializzata con successo.");
wifiIcon = readImageFile("/wifi.txt");
btIcon = readImageFile("/bluetooth.txt");
cuoreIcon = readImageFile("/cuore.txt");
stepIcon = readImageFile("/step.txt");
}
}
digitalWrite(CS_MIC_SD, HIGH);
digitalWrite(TFT_CS_LCD, LOW);
delay(1000);
digitalWrite(TFT_CS_LCD, LOW);
digitalWrite(CS_MIC_SD, HIGH);
tft.init(240, 280);
tft.setRotation(2);
tft.fillScreen(ST77XX_BLACK);
Home_page();
}
void loop() {
if (touchpad.available()) {
tft.fillRect(0, 0, 240, 280, ST77XX_BLACK);
switch (touchpad.gesture_id) {
case GESTURE_NONE:
Serial.print("NONE");
break;
case GESTURE_SWIPE_UP:
Serial.print("SWIPE DOWN");
Home_page();
break;
case GESTURE_SWIPE_DOWN:
Serial.print("SWIPE UP");
impostazioni();
break;
case GESTURE_SWIPE_LEFT:
Serial.print("SWIPE LEFT");
break;
case GESTURE_SWIPE_RIGHT:
Serial.print("SWIPE RIGHT");
break;
case GESTURE_SINGLE_CLICK:
Serial.print("SINGLE CLICK");
break;
case GESTURE_DOUBLE_CLICK:
Serial.print("DOUBLE CLICK");
break;
case GESTURE_LONG_PRESS:
Serial.print("LONG PRESS");
break;
default:
Serial.print("UNKNOWN ");
break;
}
// Stampa la posizione (x, y) e il numero di dita
Serial.print("at (");
Serial.print(touchpad.x);
Serial.print(", ");
Serial.print(touchpad.y);
Serial.print(") fingers: ");
Serial.println(touchpad.finger_num);
}
delay(100); // Aggiungi un breve ritardo per evitare di sovraccaricare la seriale
}
with this script the micro sd is read correctly
#include <SPI.h>
#include <SD.h>
void setup() {
// Inizializzazione seriale
Serial.begin(115200);
// Inizializzazione SPI
SPI.begin(11, 17, 4, 18); // SCK, MISO, MOSI, CS
// Inizializzazione della microSD
if (!SD.begin(18)) {
Serial.println("Errore nell'inizializzazione della microSD");
return;
}
Serial.println("MicroSD inizializzata con successo");
}
void loop() {
// Non serve codice nel loop per questo esempio
}
if the SD card is sharing a SPI bus with other devices there can be problems - see SD card reader sharing SPI bus
how have you connected the various devices to the ESP32?
upload a schematic?
followed by
is not the same as