My history is with Microchip PIC controllers and their MPLAB.ide.
“Call function” will effectively goto “Function” somewhere in code, but normally come back to where it came from, i.e. the next line after “Call function”.
Starting out with ESP32 in Platformio has been great fun, but now I’m writing a proper project, not just playing with on-line tutorials.
During my “void setup()” I read an SD card on SPI bus and get data onto a TFT display screen. Works well enough, but in void Init_SD_CARD() { where I do “SD.begin”, it could fail or maybe not even have a card inserted.
So, I’ve included 2 TFT screens, inside the SD card initialization “void Init_SD_CARD()” function.
- “Insert SD Card” …. card NOT OK, this flashes
- “SD Card ok” ……… now reading data OK
Without a card, I get screen 1.
Then insert the card, it changes to screen 2 ………… but takes several seconds to get back to my “void setup()” path, where it starts putting data on the TFT screen.
Part of my code …
Not shown SD_ONbus() and TFT_ONbus() they just swap the CS lines for the SPI bus.
//init SD Card ###########################################################
void Init_SD_CARD() {
Card_Error = 0;
SD_ONbus(); //swap CS lines
while (!Serial) { ; } //wait for serial port to connect
Serial.print("Initializing SD card … ");
if (!SD.begin(SD_CS)) {
Serial.println(“Failed!”);
//NO CARD !!! Splash screen ============================ CARD ERROR ====
Card_Error = 1;
TFT_ONbus(); //swap CS lines
tft.fillScreen(TFT_BLUE);
tft.setTextColor(TFT_WHITE);
tft.setTextSize(2);
tft.drawString (“Insert” ,90, 100,4);
tft.drawString (“SD Card” ,60, 170,4);
Init_SD_CARD(); //try to Init_SD_CARD() again
}
//CARD OK !!! Splash screen ============================ CARD OK =====
TFT_ONbus(); //swap CS lines
tft.fillScreen(TFT_BLUE);
tft.drawString (“SD Card” ,60, 170,4);
tft.drawString (“OK” ,110, 220,4);
Serial.println(“OK \n”);
SD_ONbus(); //swap CS lines
} // STRANGE !!! this takes ages to go back to ...
// READ SD Card files ##############”
//###########################################################################
void setup() {
Serial.begin(115200); //Serial terminal baud rate 115200
delay(100);
// Set CS pins as OUTPUTS
pinMode(TFT_CS, OUTPUT);
pinMode(SD_CS, OUTPUT);
// Set CS pins OFF = HIGH //TFT and SD OFF bus
digitalWrite(TFT_CS, HIGH);
digitalWrite(SD_CS, HIGH);
//init TFT screen ####################################################
TFT_ONbus(); //swap CS lines
tft.init();
tft.setRotation(4); //4 is Portrait with PCB pins at bottom
//Splash screen on TFT Screen ########################################
tft.fillScreen(TFT_BLUE);
tft.setTextColor(TFT_WHITE);
tft.setTextSize(2); //textsize multiplies font size
tft.drawString (“MIDI” ,100,250,4);
tft.drawString (“Keyboard” ,40,300,4);
tft.drawString (“Controller” ,45,350,4);
delay(1000);
// READ SD Card files #################################################
SD_ONbus(); //swap CS lines
Init_SD_CARD(); //Initialize SD card reader
// takes ages to come back here !!! ============
List_SD(“/”); //list all files on SD card
Read_SongFile(“/XXXX XXXX.txt”);
Send_Chords();
//setup MAIN SCREEN --------------------------------------
TFT_ONbus(); //swap CS lines
tft.fillScreen(TFT_BLACK);
blah … blah … blah … TFT screen layouts
} // end of setup
//#######################################################################
void loop() {
// Functions put my SD card data on TFT screen …
// I press some buttons, things happen …
// All works fine !
} // end of loop
This all “works”, but why several seconds to exit “void Init_SD_CARD()” and get back into “void setup()” ???
I do eventually get all my data on screen and then void loop() { works fine.
Any ideas guys ?
Thank you
Trevor