Problems with GPIO push button on self build emonPi

Hi @pb66 @borpin @Robert.Wall
Tried all suggestions and various combinations, goes to page 0 “wifi” and no further , its not the end off the world, would have been a nice feature to get right.Thank you for all your input.

There has to be a simple answer to why you cannot get it to work.

The logic you need goes roughly like this, which works in both directions. If you’ve only got one button and can only cycle round one way, you obviously ignore the unnecessary part:

if (up_pushbutton)
  increment page_number
  if (page_number > max_page)
    page_number = 0

if (down_pushbutton)
  decrement page_number
  if (page_number < 0)
    page_number = max_page

display page page_number

It depends on page number being allowed to go negative, i.e it must be a signed variable,
and pages are numbered 0 – max_page, so that there are max_page+1 pages in total.

So coming into the logic with a “middle” page being displayed, it just steps up and down as you expect and the second “if” in each part doesn’t apply.

If you come in with “max_page” being displayed and go up, page_number becomes one greater, the second “if” comes into play and sets the page_number to zero, and it’s displayed.

Likewise, if you come in with page zero displayed and go down, the page_number becomes -1, the second “if” comes into play and sets the page_number to max_page, and it’s displayed.

As I inferred earlier, with the logic you appear to have, if I have pages 0 – 5 inclusive, my “max_page” is 5, your max_number_pages would seem to be the same but the count of pages is 6.

The other point for you to look at is: Do you have a Page 0? or is your lowest numbered page 1?

I prefer solutions that only change the value once and do not ever change it to a value that might be invalid.

if (up_pushbutton)
  if (page_number + 1 > max_page)
    page_number = 0
  else
    increment page_number

if (down_pushbutton)
  if (page_number -1 < 0)
    page_number = max_page
  else
    decrement page_number

display page page_number

YMMV

If there’s another thread around that picks up page_number, that would indeed be safer.