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.
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.