I am pretty new to both Arduino and C++. I am trying to make an automatic dog feeder that will eventually run with a stepper motor, hall sensor, LCD display and an RTC (DS1307). So far I only have the LCD and the RTC integrated. I created a menu using a Finite State Machine. After a bit of research, it seemed like the easiest way to achieve what I wanted as far as the feeder menu goes. The problem I am having is that the Arduino will sometimes reset itself when a button is pushed. The button that causes the Arduino to reset varies sometimes depending on the section of code that is commented out, but is almost always the return (back) button.
I began looking for where the problem might be occurring by commenting out parts of the code, starting from the last thing I implemented, which was the RTC. I commented out everything related to the RTC including the RTClib and Wire libraries, because I could not scroll past the first menu without the Arduino resetting. Everything seemed to work perfectly. I could scroll through my first set of menus. The problem came when I went into one of the secondary menus to set the time for the feeding times or the time for the RTC. I would hit the back button to either go to the previous menu or to go from setting the minutes back to setting the hour and the Arduino would reset. The L indicator light would flash once and the LCD would go all the way back to the main menu and all the entered values were lost.
I commented out the majority of the code in the function set_feed2_time and just left the lcd.print and the code again worked perfectly. I could scroll through my first set of menus, go into my secondary set and go back and forth between the values I was setting and they were being saved.
In a leap of faith and in an attempt to debug I added a Serial.print() at the end of my screen_scroll function that checks the state transition triggers and for some reason that makes the code work. My question is why is this happening? What is causing this issue with the code? Am I not understanding the functionality of a State Machine? I’ve been trying to figure out what is going on for a while now, but I am feeling pretty lost.
These are the components I am using
- Arduino Mega2560
- DS1307 RTC
- LCD1602 Module
Edit: my code is way too long to post so I have attached the file
Menu_U_Iv3.ino (13.7 KB)