Button issues scrolling with state machine menu (switch case)

Hello everyone,

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)

Power supply or wiring issues would be my first concern.
After that some kind of undefined behaviour due to writing to invalid memory or memory fragmentation due to use of String class.

But as I’m on my tablet, I can download your attachment.
Or rather I can download it, but I can’t open it because the tablet doesn’t know what to do with an “ino” file.
Now that’s more Apples fault than yours, but it doesn’t help that you do not post your code directly. I suppose it was too big. >:(

russo08:
Edit: my code is way too long to post so I have attached the file

You now have a 13k program. Is the problem something that has arisen because of the last 5 lines of code you added? If so what were they?

Programs should be tested after every few lines of code are added. That makes it much easier to identify the cause of a problem.

Trying to find an error in 13k of unfamiliar code is very time consuming. Maybe you can write a short program that illustrates the problem.

My wild guess is that your code is writing beyond the bounds of an array or else you are using the String class. It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R

Robin2:
Programs should be tested after every few lines of code are added.

I honestly think this is my issue. At his point, my code is so long and there are so many moving parts and I definitely did not test everything each step of the way or with every case I added. The last things I added were the two functions I called set_feed1_time and set_feed2_time and the states SET_FTIME1_HOUR, EDIT_FTIME1_MINUTE, EDIT_FTIME2_HOUR, and EDIT_FTIME2_MINUTE. Prior to their addition, everything was working fine. As you pointed out though, I did not test out these piece by piece. I just added them all at once and ran the code.

I only use one String class I believe and that's only in one section of the code that was working fine. The Serial.println(state) I added at the bottom of my screen_scroll function seems to fix the issues I am having. I'm not sure why. I have a break for every switch case so it cant be falling through. I may have to just rewrite everything and take your advice on testing every few lines added or at least every new case added.

pcbbc:
Power supply or wiring issues would be my first concern.

I've double checked the wiring and even rewired it to make sure and I've tried to power the Arduino directly to the wall.The problem still persists. I don't really use any String classes in my code. I think i only use it in one instance. Yep, the code was way too big and exceeded the character limit :frowning:

Thank you guys for your input and attempts to help.

russo08:
I may have to just rewrite everything and take your advice on testing every few lines added or at least every new case added.

I know (from experience) just how unpleasant that seems but it is probably the quickest and simplest solution.

Maybe have a look at Planning and Implementing a Program

By breaking the code into several short single-purpose functions it will be much easier to develop, test, debug and maintain. Each function can be tested on its own.

...R