Hello. I am a mobile developer with some years of experience, but this is my first project on Arduino, or on any microcontroller for that matter, so please assume some naïveté on matters of resources and c++.
I am building a garden irrigation controller. The idea is to have a headless controller that can be programmed via BLE by a mobile app. I am using an Arduino Uno with an AZ Delivery SD shield (a clone of the popular Adafruit SD shield with rtc clock). I am using a DSD Tech HM-19 for BLE; I will add relays for switching water valves on and off, for now I am testing with LEDs.
I am working with the Platform.io IDE on Visual Studio Code.
The mobile app sends programs as simple strings, which are parsed into objects on reception (Arduino side). Any WateringProgram has a number of WateringPhases. Programs are launched at a given time. When completed, a record is saved on the SD card, so I can schedule programs with a repetition interval of days, even if Arduino should restart.
I have solved all the basic problems (program parsing, using RTC, launch on a given time, logging on as card, ble communication); now I am integrating all the building blocks and I am seeing some strange behavior.
After this long premise, here is what happens.
The parsePrograms function parses a string into an array of WateringProgram objects, and logs the result on the serial. It has been working reliably so far.
void parsePrograms() {
CanvasParser parser = CanvasParser(canvas);
parser.parsePrograms(programsCount, programs);
Serial.print("\nThere are ");
Serial.print(programsCount);
Serial.println(" programs");
for (int i = 0; i < programsCount; i++) {
WateringProgram prog = programs[i];
prog.prettyPrint();
}
Serial.println("parseProgram ended");
}
Now I am trying to integrate the SD library. If I do this:
if (!SD.begin(chipSelect)) {
the parse program function stops working; in the log I get the correct amount of programs, but phases and other program properties are empty.
Although I am new to microcontrollers, I have debugging skills, so I tried to isolate and understand the problem. To be on the safe side I commented out all loop(), and just tested calling parsePrograms() and SD.begin on the setup function. I found out that the order does not seem to matter; if I call SD.begin, parsePrograms logs programs without phases. I also have tried adding delays, no changes anyway.
I guess this buggy behavior has something to do with memory allocation, but honestly I don't know what to try next. Any suggestion?