LCD code works in some parts of program, not in others.

Hi -
My well pump is not strong enough to power more than one sprinkler head at a time. This project will switch sprinklers on and off, one at a time. Using UNO (real one) and LCD Keypad display of eBay origin. UNO works fine, Display works fine , except see notes in remarks in code , relay board works fine, and wiring/pins are fine. (unless someone sees what I don’t. Everything was working until… it wasn’t. Seems to be only two lines of LCD code have gone south. Have reloaded LiquidCrystal lib but didn’t change. Everything works OK except the two lines of code - and they used to but I apparently changed something it didn’t like.

sketch_jun13.ino (5.76 KB)

The indentation of your code indicates you are expecting a different program structure than what it actually has. Do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) and then check the resulting indication. Does this match the program structure you are expecting?

When you don't use braces ({}) with a flow control statement like if, only the single line immediately following is affected. This can lead to confusing bugs if you later add more lines and expect them to be part of the block. For this reason, I recommend always using braces, even when they are not strictly needed.

Thanks for the come back -

  1. Did an auto format, as you suggested. It rearranged things but the two lines still don't work.
  2. Added some brackets but the two lines still don't work.
  3. Walked through code, again for the umpteenth time, but the two lines still don't work.
  4. Blasted only two lines of code that won't work.

I welcome more suggestions, recommendations, and down-right criticism of my coding technique - anything that will help me see where I went wrong. (I mean with this code, not my life.)

OK - found it!
It was a timing thing. I was writing the --OFF (the lines of code that weren't working, but were) but I was writing the --ON for the next zone too fast and it over wrote the --OFF before I could see it.
Thanks for your help.

You're welcome. I'm glad to hear you found the problem.


  1. Did an auto format, as you suggested. It rearranged things but the two lines still don't work.

Just to make sure it's clear: Auto Format will never fix problems in your program. It's merely cosmetic and benefits only the humans reading the code. The indentation is completely ignored by the compiler. However, the automatic indentation is based on the code structure, so it makes it easy for humans to visualize that structure. For this reason, auto format can be a very useful troubleshooting tool, in addition to making your code easier to read and more professional in appearance.