Due to high demand expect some shipping delays at this time, orders may not ship for 3-4 business days.
0

CPX External Button
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

CPX External Button

by jvanier on Sun Nov 28, 2021 5:02 pm

Hi,

Is there a simple way to detect an external push button connected to a Circuit Playground Express pin through Makecode?

The hardware setup is similar to https://learn.adafruit.com/android-gboa ... al-buttons: button connected to ground on one side an pin A4 on the other side. I set the pull up with a "set pull pin A4 to up" block on start.

Image

The "on pin A4 down" event never triggers when the external button is pressed. I verified with a voltmeter that the voltage does switch from 3.3V when the button is not pressed to 0V when the button is pressed so the circuit is fine. Also doing "digital read pin A4" shows that the state changes.

The docs for onEvent https://makecode.adafruit.com/reference ... n/on-event say that you can use pins or touch inputs. I expected "pin A4" to be A4 as a normal digital input like a button and "touch A4" to be A4 in capacitive touch mode. Is this correct?

I also tried "on pin A4 falling" as shown in the docs https://makecode.adafruit.com/learnsyst ... lling-edge where they describe reading a micro-switch and that's not working either.

What am I missing to simply get an event from an external switch? I know could use "if digital read pin A4" in a forever loop, but then I'd need to manage state and debouncing so I'd prefer a simpler solution.

jvanier
 
Posts: 3
Joined: Wed Jun 10, 2015 11:33 pm

Re: CPX External Button

by jvanier on Mon Nov 29, 2021 11:55 am

According to https://github.com/microsoft/pxt-adafruit/issues/619, the convention is that everything in "Input" is capacitive touch and "pin A4" is an alias for "touch A4" (pin A4 is deprecated but kept for compatibility).

I think it's necessary to use "digital read pin A4" in a forever loop to detect button presses.

jvanier
 
Posts: 3
Joined: Wed Jun 10, 2015 11:33 pm

Re: CPX External Button

by johnpark on Tue Dec 07, 2021 9:03 pm

I think this method should work for you: https://www.youtube.com/watch?v=RLX9jOv6zfU using a button instead of the door sensor.

johnpark
 
Posts: 902
Joined: Wed Mar 25, 2009 2:15 pm

Re: CPX External Button

by jvanier on Wed Dec 08, 2021 10:44 am

Thanks for the reply and the video! I'll try the pin rise block.

Do you know how it would work for denouncing? If the pin rise handler is already running, can it run again? If no, I can just do delay 20ms at the end of the pin rise handler to suppress the bounces. Otherwise I'll have to use variable in the handier (check if variable is false at the start of the handler, set it to true, do the stuff the button is supposed to do, wait 20ms, then set the variable back to false).

jvanier
 
Posts: 3
Joined: Wed Jun 10, 2015 11:33 pm

Re: CPX External Button

by johnpark on Wed Dec 08, 2021 12:08 pm

I don't recall needing to debounce it, but if you do the method you described should work well.

johnpark
 
Posts: 902
Joined: Wed Mar 25, 2009 2:15 pm

Please be positive and constructive with your questions and comments.