4bit LCD library code fixes

The 4bit LCD library has some too-clever coding that should probably be changed.

int DB[] = {7, 8, 9, 10};  //wire these to DB4~7 on LCD.


  for (int i=DB[0]; i <= DB[3]; i++) {

    digitalWrite(i,val_nibble & 01);
    val_nibble >>= 1;

  }

the for loop will break if the pin values for DB[0] - DB[3] are not in order.
This came up on a board with the pins hardwired to an LCD, so it’s not just theoretical.

I think this is the fix, although I haven’t tested it. I’m not sure whether I can declare variable
j there, but you get the idea.

  for (int i=0; i <= 3; i++) {
 
   int j = DB[i];
    digitalWrite(j,val_nibble & 01);
    val_nibble >>= 1;

  }

So what are the rules for fixing this bit of code? Can anyone step up and do it (assuming they test it thoroughly first)? I've got real life issues preventing 4 consecutive data pins as well.

There was a thread on this in the Software Development forum section that had all the code fixes. Don't know if the library ever got changed though. It's easy enough to tell.

The fix is trivial - just use a separate variable (or better, array) for the pins, and use that for indexing. The original code just wasn't really up to snuff as a general-purpose library.

There was another thread in this discussion group (bugs & suggestions) on LCD's, I think, where the LCD libraries were discussed. Mellis was talking about rewriting them himself I believe.

BUT FIXING THE OBVIOUS ANNOYING BUGS NOW WOULD BE A GOOD START. :)

Paul

The LiquidCrystal library in Arduino 0012 will support using any four (or eight) data pins, consecutive or not. In the meantime, feel free to upload an updated version of the library to the playground.