CPX External Button

Microsoft's MakeCode platform for easy blocks-style programming

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jvanier
 
Posts: 3
Joined: Wed Jun 10, 2015 11:33 pm

CPX External Button

Post by jvanier »

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.

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

Re: CPX External Button

Post by jvanier »

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.

User avatar
johnpark
 
Posts: 985
Joined: Wed Mar 25, 2009 2:15 pm

Re: CPX External Button

Post by johnpark »

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

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

Re: CPX External Button

Post by jvanier »

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).

User avatar
johnpark
 
Posts: 985
Joined: Wed Mar 25, 2009 2:15 pm

Re: CPX External Button

Post by johnpark »

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

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

Return to “MakeCode”