LCD goes crazy, not the usual

Funny problem with my project:

I've been working on a menu based brewing controller.

I can scroll through menus with an encoder,using the click button to make selections.

All was working fine until I tried to tidy up a loop that let's you set desired temperature, date, times etc.

When I exit from THIS loop, my 20x4 LCD goes mad.

If I wind the encoder for maybe 10 or so clicks, it suddenly rights itself and all is fine.

Any ideas?

Could it be me pointing to some variable outside of an array or something like that?

Cheers!
Kind regards,
Alex

Apologies, bad etiquette on my part.

Have adjusted my code so that I don't accidentally point to some out of bounds place in a String array and it's working tickety-boo again.

Cheers!

Just in case it's useful to anybody, I use a little arduino dedicated to reading an encoder which is read in by this snippet.

void receiveEvent(int howMany) {
  while (1 < Wire.available()) { // loop through all but the last
    increase = Wire.read(); // receive byte as a byte
    decrease = Wire.read(); // receive byte as a byte
    button = Wire.read(); // receive byte as a byte
    if (increase == 1) {
      menuCounter++;
      incrementor++;
    }

    if (decrease == 1) {
      menuCounter--;
      incrementor--;
    }

    //Keep the menu counter within the available array
    if (menuCounter >= counterMax) {
      menuCounter = counterMax;
    }
    if (menuCounter <= counterMin) {
      menuCounter = counterMin;
    }

    //Keep the menu counter within the available array
    if (incrementor >= incrementorMax) {
      incrementor = incrementorMax;
    }
    if (incrementor <= incrementorMin) {
      incrementor = incrementorMin;
    }

I was locally setting the counterMin and counterMax so that menuCounter can be used to display menu options from strings like this:

  counterMin = 0; //sets the minimum value the counter can have in this menu gets called in the receiveEvent
  counterMax = 2; //sets the minimum value the counter can have in this menu gets called in the receiveEvent


  //enables the menu options to be scrolled through
  String options[3] = {"Set HLT temp ", "Set HLT Timer", "EXIT         "};

The problem came about when I set the counterMax to 100 so that I could dial in a maximum temperature.

I was still using the menuCounter to implement the change of temperature, however exiting from that menu would leave the menuCounter way out of the bounds of the String array in the previous menu and the screen would go insane.

I've implemented a seperate counter called incrementor now which has its own incrementorMax and incrementorMin.

Works a charm!

What's the point of the parameter in your receiveEvent()?

I don't understand this-
increase = Wire.read(); // receive byte as a byte
decrease = Wire.read(); // receive byte as a byte
button = Wire.read(); // receive byte as a byte

Whatever Wire.read() returns, increase is equal to decrease is equal to button. So if Wire.read() returns a 1, both increase ==1 and decrease ==1 which looks like it would cancel out.

So I have an Atmega168 which is dedicated to my encoder.

Whenever the encoder changes state, the Atmega168 sends out an array of bytes over i2c.

For example, if the encoder increments, the transmission looks like this:
1,0,0

If the encoder decrements, the transmission is:
0,1,0

And if the button is pressed:
0,0,1

The receiving device takes the incoming data and assigns it byte by byte to the variables increase, decrease and button.

Have a search around for examples on the wire libraries. The examples in the reference section only deal with transmitting one byte so it's worth casting your net a bit further.

Cheers! :slight_smile: