Latching Push Button Woes

Status
Not open for further replies.

chronix33

New member
Hello all, very new to this.

I’m slowly learning how to use Teensyduino to program the board for certain things. Right now I’m starting small, I have 3 leds that I am attempting to turn on/off with latching buttons. The issue is this:

If I press button 1, leds 1 and 3 light.
If I push button 2, led 2 functions exactly as intended.
If I push button 3, nothing happens. Pic below:

Sorry for the crappy drawing

http://i.imgur.com/Cgw3vr1.jpg

I’m powering the board via USB, not the pads, I just used those to show my supply.

Code is here: https://create.arduino.cc/editor/chronix33/5d8d8a03-db68-4426-8f37-9d94cb4579bb/preview

Building on breadboard.

Also I do everything on mobile, I have no interwebz connection at home.
 
This doesn't look quite right:

Code:
  if (digitalRead(pb1==HIGH))

You probably meant this:

Code:
  if (digitalRead(pb1) ==HIGH)


Two other small issues....

1: Your drawing doesn't look quite right. It shows pins 0, 1, 2 connected directly to GND, which couldn't possibly work. Maybe it's not actually wired like that?

The normal way to connect pushbuttons is actually much simpler. You wire each button between the pin and GND. Then in the code, you use pinMode(pin, INPUT_PULLUP). This way requires no physical resistor, since there's one built inside the chip. All you have to do is use INPUT_PULLUP mode to activate it.

However, this will cause the logic to be "backwards". That's actually the way most people normally do this, where HIGH means the button isn't pressed and LOW means it's pressed.


2: Usually the best way to read buttons is with the Bounce library. You can get a good example with File > Examples > Teensy > USB_Keyboard > Buttons. The Bounce library adds functions risingEdge() and fallingEdge() to let you know when the button changes, and it automatically deals with mechanical chatter so you get just 1 change even if the contacts chatter. As you try to do more complex things with buttons, this feature can really make everything so much easier, so I highly recommend using that example. A little extra effort to use the Bounce library will pay itself back many times over when you avoid the tough problems with mechanical chatter (which very fast boards like Teensy are much better at detecting).
 
Thanks for the response, let me try and clarify.

I’m using pull down resistors to pull the pin low. My final application will eventually be an HID switch/button panel, and for some of my components to operate the way they are intended I don’t believe I can reverse the logic state aka use the built in pull up.

I will modify the code the way you showed and see if that helps.

What has gotten me confused is the second button works flawlessly, maybe I’ll try and spread the inputs around and see if I might can isolate the issue to a faulty button or connection or something.
 
What has gotten me confused is the second button works flawlessly

This happens only by coincidence. "pb2" is set to 1, and HIGH is defined as 1. So "pb2==HIGH" evaluates to true, which is the number 1. So digitalRead(pb2==HIGH) reads pin 1, and returns LOW or HIGH, which are 0 or 1. So it works, but only by coincidence, because "pb2==HIGH" evaluates to 1.
 
you could easily flip the logic around via software

if ( !digitalReadFast(pb2) == HIGH ) { ..do_something(); } // the reading is actually LOW, but we flipped the read with a "!", which flips the logic
 
So I’ve implemented the code change Paul said, and I’m still having this issue. Could it be I’m not using a large enough resistor for my pull down? Due to work schedule I can only fiddle around on the weekends, just trying to get an idea of how I could do this using the pull down method.
 
Status
Not open for further replies.
Back
Top