Change LCD display by button in loop

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Gumbajoe
 
Posts: 43
Joined: Fri Nov 14, 2014 4:04 pm

Change LCD display by button in loop

Post by Gumbajoe »

Hello,

I have a functioning Arduino project (I am using an Uno) which records the temperature from 6 sensors, displays their readings and the temperature difference between the sensors sequentially on an LCD, changes the LCD color based on the temperature difference between certain sensors, and logs the data to an SD card - thanks to a lot of help from this forum.

Right now the LCD display cycles through the values. I would like to have a push-button allow the user to cycle through the values so the LCD display will show data from only sensor they want to see at that moment, and with each sequential push of the button the next sensor value will be displayed: e.g. push button and see sensor 1 data on LCD, push button again see sensor 2 data on LCD, etc.

I can find some things online about how to make a simple program that will change what is displayed on the LCD (e.g. simple text message), but I can't seem to find any examples which allow this function while the loop continues to run (e.g. sketch continues to log temps to the SD card, grabs new sensor readings, etc). My Arduino programming books also don't seem to cover this, or I perhaps I am just missing it.

This may be completely obvious to others out there - can someone point me in the right direction or give a link to a reference which would be useful to enable this functionality?

Thanks!

User avatar
Franklin97355
 
Posts: 23938
Joined: Mon Apr 21, 2008 2:33 pm

Re: Change LCD display by button in loop

Post by Franklin97355 »

You need what is called a state. you have a variable for the display "state" and with each push of the button you increment the state and as the program goes through the loop it reads the variable and displays only that display. You could use a switch statement to do this.

User avatar
Gumbajoe
 
Posts: 43
Joined: Fri Nov 14, 2014 4:04 pm

Re: Change LCD display by button in loop

Post by Gumbajoe »

Thanks adafruit admin - I have my arduino in a project box

https://www.adafruit.com/products/271

with a 2 x 16 color LCD and a data logging shield, with a couple of jacks and two potentiometers along the top. I have barely 13 mm to either side of the arduino board and shield where I could mount a surface button to toggle the state, but all of the panel mount buttons I see on your website are jumbo for a project box crammed full of stuff like mine, like this one

https://www.adafruit.com/product/1505

Can you suggest a button which I can surface mount on a smaller project box like mine?

Here's pick of the empty space in the project box

Code: Select all

[attachment=0]Adafruit Arduino project box.JPG[/attachment]
Thanks!
Attachments
Adafruit Arduino project box.JPG
Adafruit Arduino project box.JPG (596.73 KiB) Viewed 1052 times

User avatar
Gumbajoe
 
Posts: 43
Joined: Fri Nov 14, 2014 4:04 pm

Re: Change LCD display by button in loop

Post by Gumbajoe »

Welp, bought the jumbo button anyway and I will try to make it fit into the enclosure.

Thanks for the tip on the state machine. I managed to edit my code so I have four LCD screens I can scroll through sequentially by pushing a button before the cycle repeats, which is what I want. I found this site helpful for others who come across this thread:

https://programmingelectronics.com/tuto ... d-version/

This code works well: pin 19 goes to a momentary pushbutton that then goes to +5v (following the example from the state machine website listed above)

Setup

Code: Select all

// this constant won't change:
const int  buttonPin = 19;    // the pin that the pushbutton is attached to
const int ledPin = 18;       // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT_PULLUP);
Loop

Code: Select all

  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 

  // turns on the LED every four button pushes by 
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of 
  // the division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
    }
The serial monitor reports as you push the button 4 times:
on
number of button pushes: 1
off
on
number of button pushes: 2
off
on
number of button pushes: 3
off
on
number of button pushes: 4
off
The serial monitor stops posting results after "off" until you push the button again.


I am running into trouble with button bouncing. Apparently the bounce library has been replaced with bounce2 on arduino playground, and almost everything I can find uses the bounce library, not bounce2. I find the limited documentation on bounce2 hard to follow with my little brain. When try to follow the example from Bounce2 - change (detecting state change), the button seems to have a marked delay in triggering. If I hold it for about 2 seconds, it will eventually change, but the response seems markedly delayed and inconsistent. Button pushes of 1 second or less are not detected.

In setup

Code: Select all

// this constant won't change:
const int  buttonPin = 19;    // the pin that the pushbutton is attached to
const int ledPin = 18;       // the pin that the LED is attached to

// Instantiate a Bounce object :
Bounce debouncer = Bounce(); 
    // After setting up the button, setup the Bounce instance :
  debouncer.attach(buttonPin);
  debouncer.interval(500);
    
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);

In loop

Code: Select all

debouncer.update(); // updates the object before reading its value
// read the pushbutton input pin:
if ( debouncer.rose() ) {
     // add one to button push counter :
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {
      // if the current state is LOW then the button
      // went from on to off:
      Serial.println("off"); 
    }
  // turns on the LED every four button pushes by 
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of 
  // the division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
    }


Pushing the button does not change the screen, the serial monitor reads'
off
off
off
off
off
on
number of button pushes: 1
off
off
off
off
off
off
on
number of button pushes: 2
off
off
off
off
The serial monitor prints "off" about every 0.6 second or so.

I tried attaching the button to ground rather than +5V with no change. No change with changing the code from debouncer.fell to debouncer.rose. There is no change in responsiveness when I change the debouncer interval from 500 to 250. I am uncertain why this code is so unresponsive to state change, it's worse than not having bounce2 in the code at all.

Thanks!

User avatar
Gumbajoe
 
Posts: 43
Joined: Fri Nov 14, 2014 4:04 pm

Re: Change LCD display by button in loop

Post by Gumbajoe »

Whelp. looks like an anomaly in wiring - for some reason the pin is not being pulled to ground, so when the button is pushed the pin can't tell that there has been a state change. Why it is not being pulled to ground is not clear, but this does not have anything to do with debouncing.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Change LCD display by button in loop

Post by adafruit_support_mike »

As a sanity check, are you seeing the correct voltages on either side of the switch while it's open, and the correct change in voltage when you press the switch?

User avatar
Gumbajoe
 
Posts: 43
Joined: Fri Nov 14, 2014 4:04 pm

Re: Change LCD display by button in loop

Post by Gumbajoe »

I am using the A5 pin on the SD shield to monitor the switch used to detect state change as I have used up almost all the other pins on the Arduino Uno.

The button resistance drops to zero when pressed, and is infinite when open, it's not a button problem.. Measuring the A5 pin voltage on the Adafruit SD shield with the button open: 4.136V; button pressed 4.99V. No wonder why the button does not cause the Arduino to recognize a state change. The 10K resistor on the SD board measures 2.2k, which seems very weird, it really is a 10k resistor.. All the wiring on the SD shield looks right.

I recreated the same circuit that is mainly built on the SD shield using a separate breadboard. Using the same arduino but with the SD shield removed, the same A5 pin with the button open measured 1.1V, button closed 4.99V. Button works fine with the circuit on the breadboard. with a state change showing on the LCD with each button push.. The 10K resistor on the breadboard measures 10K.

After getting nowhere on this for a couple of hours, as my wiring seemed correct, an EE friend suggested looking at the schematic for the SD shield, and pointed out that the pin A5 on the adafruit SD shield I am using looks like it is tied to the real time clock:

adafruit shield schematic.jpg
adafruit shield schematic.jpg (96.95 KiB) Viewed 974 times
[/url]
https://learn.adafruit.com/adafruit-dat ... /downloads

https://datasheets.maximintegrated.com/en/ds/DS1307.pdf

On the schematic analog inputs are labeled A0-A3, with the A4 and A5 pins labeled RTC SDA and RTA SCL, Unfortunately, I only read through the Adafruit SD shield tutorial and relied on the pin markings on the Adafruit SD shield, and did not review the schematic for the Adafruit SD shield when building the circuit. The pins are labeled A4 and A5 on the Adafruit SD shield, and not RTC SDA and RTA SCL, leading me to think they were open analog pins just like A0-A3:

Adafruit SD shield analog pin labeling.jpg
Adafruit SD shield analog pin labeling.jpg (90.84 KiB) Viewed 974 times
[/url]

My friend suggested this might be why I can't pull the pin to ground.

I now realize now the the RTC must communicate to the arduino somehow, and that way must be through one or more of the arduino pins.

Luckily I have one open pin left I can use - all of the other pins on my Uno are used up. I'll rewire the board and let you know how it goes. If this is the case, you may want to relabel the Adafruit SD shield A4 and A5 pin markings as RTC SDA and RTC SCL, or put in all caps on your nice SD shield tutorial "don't use the A4 or A5 pins!" so noobs like me who don't read the schematic and only look at the markings on the board don't get confused!

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Change LCD display by button in loop

Post by adafruit_support_mike »

The RTC communicates with the Arduino through the I2C interface, which is handled on pins A4 and A5. You'll need to move the switch connection to another pin.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”