My beer brewing application almost works...

Hi,
This is my first attempt at writing my own program.
The program is built around a single switch function which controls the flow of the program. The user navigates through some menus where the brewing parameters can be changed, like temperature and time. When it comes to brewing, a PID algorithm reads the temperature and outputs a signal to a SSR which drives a heating element. I'm using a PT100 for temperature and an optical rotary encoder with a push button to navigate.

Overall the program does what I intend it to do. Except I started seeing some weird behavior when adding another Case. The last case I wanted to add was "Case: 4 //Hop settings" where the user can choose when to add hops to the boil. The LCD went blank when entering Case 4 and everything froze. I thought it was due to low memory. Around 50% of dynamic memory was used for global variables.

I made some changes to the code, e.g. write the menu text strings to flash with the "PROGMEM" function. This reduced the previous 50% memory usage to 27% but it did nothing to fix the problem with Case 4. The program still freezes when I enter Case 4. Now I'm at a loss what to do, please help!

Temp_StateMachine.ino (28.9 KB)

Sorry, can't read your attachment on my phone. Is it too long to post, between code tags?

The usual way to debug these things is to put in more Serial.print() statements, just so you can track what part of your sketch is running when it appears to freeze. Loops (for/while) are a common culprit, so double check the conditions that are intended to exit the loop. Have you used "=" instead of "==" somewhere, for example?

Hi Nyxen,

It appears you were just missing the brackets for "case 4". Try the code attached and let us know how you get on!

Kind regards,

Zeb

Temp_StateMachine.ino (28.9 KB)

PaulRB:
Sorry, can't read your attachment on my phone. Is it too long to post, between code tags?

The usual way to debug these things is to put in more Serial.print() statements, just so you can track what part of your sketch is running when it appears to freeze. Loops (for/while) are a common culprit, so double check the conditions that are intended to exit the loop. Have you used "=" instead of "==" somewhere, for example?

Yes, it is too long to post.

I have tried to find the error by printing to the monitor. The last thing that happens when going from Case 1 to Case 4 is the State variable changes to 4, as it should.

ZebH:
Hi Nyxen,

It appears you were just missing the brackets for "case 4". Try the code attached and let us know how you get on!

Kind regards,

Zeb

Thanks for taking a look Zeb, the brackets were just one of the things I removed to see if it had any effect. It seems to work the same with or without the brackets.

With the amount of code you have in each case, I'd be inclined to put the code in a function and call the function from the switch/case. Also be careful when declaring variables inside a case, that can cause some strange behavior in the compiler.

It seems to work the same with or without the brackets.

If there are variables declared within the case, you need the brackets.

I've been rearranging a bit in my program. I moved most of the variables out of the switch function. I also moved my menu navigation code from Case: 1 and Case: 2 and made a function menuNavFunction();.

Case 4 is still broken. Program freezes.

As a result of having the navigation code in a function, the button function in the end of the case gets called less often which results in a button that almost never responds.

Attached is the updated program.

Is there anything else I can try?

Temp_StateMachine.ino (26.3 KB)

Be careful of the comment lines with a backslash as the last character on the line, that continues the comment on the next line of code, and is likely completely eliminating the case 4 line:

    //**************************************************************************\\
    case 4: //Hop settings

david_2018:
Be careful of the comment lines with a backslash as the last character on the line, that continues the comment on the next line of code, and is likely completely eliminating the case 4 line:

    //**************************************************************************\\

case 4: //Hop settings

It works!! Thank you! :smiley: