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();
}
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.
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
...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.)
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.
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.