I have a fairly big project and have run into a strange situation. I am asking for general design guidance, not so much detailed code debugging. I have ~2,800 lines of code spread across multiple ESP32S3s and 20+ files (.h and .ino). I just want to make sure my code layout is sound.
In general, all the code functions as expected. The boards talk to each other, data is stored and retrieved in Preferences, and the states are executed as expected. But I ran into one weird situation. I have a rotary encoder with a switch on one of the boards. All the code runs in a SimpleFSM state machine design and ezButton for buttons.
On the controller board (the one with the rotary encoder), I have this code layout:
sketch_controller
βββ commands.h
βββ controller_esp_now.h
βββ controller_esp_now.ino
βββ controller_fsm.h
βββ controller_fsm.ino
βββ debug.cfg
βββ launcher_storage.h
βββ launcher_storage.ino
βββ libraries
β βββ ezButton
βββ rotary.h
βββ rotary.ino
βββ seven_segment.h
βββ seven_segment.ino
βββ sketch_controller.ino
sketch_controller.ino is the main entry point. It looks like:
#include <ezButton.h>
#include <Preferences.h>
#include <stdbool.h>
#include <esp_now.h>
#include "commands.h"
#include "launcher_storage.h"
#include "rotary.h"
#include "seven_segment.h"
#include "controller_esp_now.h"
#include "controller_fsm.h"
/* hardware pin definitions */
void setup() {
/* setup serial, hardware buttons, */
storage_setup();
rotary_setup();
seven_segment_setup();
controller_esp_now_setup();
controller_fsm_setup();
}
void loop() {
/* enable some hardware ezButton loops */
rotary_loop();
seven_segment_loop();
controller_esp_now_loop();
controller_fsm_loop();
}
Each .ino has void <name>_setup()and void name_loop() methods.
My problem is the button on the rotary encoder. When I have:
rotary.h
#define ROTARY_BUTTON 14
ezButton select_launcher_btn(ROTARY_BUTTON);
rotary.ino
void rotary_setup() {
select_launcher_btn.setDebounceTime(50); // set debounce time to 50 milliseconds
}
void rotary_loop() {
select_launcher_btn.loop();
if(select_launcher_btn.isPressed()) {
Serial.println("The launcher_selected button is pressed");
}
}
The button press is not always detected. In some states of the FSM it is detected, but not others. If I move this code into controller.ino file, the rotary encoder button press is recognized every time. Note, I just moved the button definition and ezButton loop to the controller.ino, not the code that detects the button press.
#define ROTARY_BUTTON 14
ezButton select_launcher_btn(ROTARY_BUTTON);
void loop() {
select_launcher_btn.loop();
}
I tried putting a print statement in the rotary_loop() function, and it was always being called, as I expected, so I do not understand why the button is not detected. I also printed out the state of the button in the rotary_loop() function, and it printed 1 all the time even when the button was pushed (i.e. connected to ground).
Again, I am just looking to remove any potential issues with my overall design that may be causing this problem.
Thanks!