Simple Two Button input to LCD output issue

I need help on getting this code right.
What I need it to do is when no button input is detected, the LCD position 0.0 and 0.1 says "Insert part"

When Buttonpin12 is pressed lcd position 0.0 says "ready 1"
When Buttonpin13 is pressed lcd position 0.1 says "ready 2"

The issue im having is when either 12 or 13 is connected the output on the lcd is always "ready 2"

I think theres a code error where its interfering with each other. How do you separate them? I want each button to be independent of each other on the lcd readout.

#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD

int buttonpin13 = 13, buttonpin12 = 12;
int buttonState13 = 0, buttonState12 = 0;

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); // Change to (0x27,20,4) for 20x4 LCD.
void setup() {
// Initiate the LCD:
lcd.init();
lcd.backlight();
}

void loop() {
//(2)
buttonState12 = digitalRead(buttonpin12);
buttonState13 = digitalRead(buttonpin13);
delay(50);

if (buttonState12 == HIGH) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Ready 1");

}

else {
lcd.setCursor(0, 0);
lcd.print("Insert Part 1");
}

if (buttonState13 == HIGH) {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Ready 2");

}

else {
lcd.setCursor(0, 1);
lcd.print("Insert Part 2");
}
}

How are the button switches wired? Are there pullup or pulldown resistors? What value resistors?

Please read the forum guidelines to see how to properly post code and some information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in a code block.

Please go back and fix your original post.

1 Like

A 1000 ohm resistor is connected to each pin of 12 and 13. Then 5v is applied through the switch to 12 and 13 separately.

Please take a pen and paper and draw how the circuit is wired, take a photo of the drawing and post it. A picture is worth a thousand words, they say.

Here is the more professional way to wire a switch. The switch will read high when not pressed (open) and low when pressed (closed). No external resistor required unless there are long wires or a noisy environment.

image

1 Like

Here's a portion of your code autoformatted and posted with code tags:

...
  if (buttonState13 == HIGH) {
    lcd.clear();
    lcd.setCursor(0, 1);
    lcd.print("Ready 2");

  }

  else {
...

Isn't that much easier to read? Please fix your original post per @groundFungus's advice.

If you always see "Ready 2" then it must be the case tat buttonState13 == HIGH and the if clause also lcd.clear()ed whatever else was on the LCD.

Does pin 13 measure as HIGH or not?

13 Does read high. When I delete the pin 12 part of the code. pin 13 functions perfectly. Same when I do the Same thing when I delete pin 13 of the code.

I got it figured out. The lcd clear function was making it flicker way too much. The draw down resistor for both connections was not good either. It works now

From

...it sounds like the normal un-pressed condition should do the "insert part" branches, versus the normal unpressed condition of the pins has some sort of pull-up scheme.

If the normal un-pressed state is HIGH, then you should change the tests to make that state do the "Insert part" branches:

  if (buttonState12 == LOW) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Ready 1");

  }

  else {
    lcd.setCursor(0, 0);
    lcd.print("Insert Part 1");
  }

  if (buttonState13 == LOW) {
    lcd.clear();
    lcd.setCursor(0, 1);
    lcd.print("Ready 2");

  }

  else {
    lcd.setCursor(0, 1);
    lcd.print("Insert Part 2");
  }

@groundFungus's scheme provides safer pull-up normally-HIGH wiring using the internal pullups:

A potential problem with supplying 5V through the switch to the pin is the risk of shorting out your power supply through the Arduino.

(Edit: Does anybody have the original of this schematic? All I have is a bookmark of the image.)

Diagonal Button Wiring

Is the debounce capacitor for blocking DC when nothing is pressed, or absorbing the oscillation during the act of pressing the button? (or something else). Thank you.

It's for absorbing the high frequency oscillation when the switch opens and closes. It kills the switch bounce. You have to have an edge long enough to charge the capacitor up before the pin will read high.

3 Likes

Thank you. I thought coils were for blocking HF. Is the oscillation not high freq enough or is the capacitor just what is needed? (no more questions from me)

A coil (inductor) would do the job if wired in series with the switch. But inductors are more complicated and expensive to make, they are generally heavier and bulkier than capacitors. As @Delta_G said, the capacitor absorbs the spikes and as you said, the inductor blocks the spike. It’s mainly a matter of economy and simplicity.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.