I am using MakeCode with CPX to make cardboard pinball machines and cardboard robots with kids/teens in single-session afterschool workshops. I want to see if I can have them build paper switches that trigger lights/sound/servos.
On the CPX forum, Adafruit support told me I need to add a pull-up resistor to get the (normally capacitive) pins to read an on/off switch as an input, and that there is one built-in to the CPX. Is there a way to program it using MakeCode?
My other question -- is it better to use a block looking for a click on a specific pin, or a block looking for a high pulse.
If it's not obvious, I am a beginning-level coder -- and want to keep things at that level for my workshops!
External "button"/internal pull-up resistor for CPX in MakeC
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- KathyCeceri
- Posts: 54
- Joined: Sun Mar 02, 2014 7:30 pm
- johnpark
- Posts: 985
- Joined: Wed Mar 25, 2009 2:15 pm
Re: External "button"/internal pull-up resistor for CPX in M
Hi Kathyc that sounds like a great workshop! Yes, you can use external switches by setting an on-board resistor first, with the 'set pull pin' block.
To use this in MakeCode, Add an 'on start' block and inside it, place a 'set pull pin' block, which comes from the PINS category in the advanced section of MakeCode. Change the drop down to 'A1' or whichever pin you want to use.
Then, you'll go to the INPUT category and get the 'on button A click' block, then you can change it from button A to pin A1 or whichever pins you're using. Each one will need the pull resistor set.
Here's a sample of the code: https://makecode.com/_fp2Fasf9485t
Hope this helps!
-John
To use this in MakeCode, Add an 'on start' block and inside it, place a 'set pull pin' block, which comes from the PINS category in the advanced section of MakeCode. Change the drop down to 'A1' or whichever pin you want to use.
Then, you'll go to the INPUT category and get the 'on button A click' block, then you can change it from button A to pin A1 or whichever pins you're using. Each one will need the pull resistor set.
Here's a sample of the code: https://makecode.com/_fp2Fasf9485t
Hope this helps!
-John
- KathyCeceri
- Posts: 54
- Joined: Sun Mar 02, 2014 7:30 pm
Re: External "button"/internal pull-up resistor for CPX in M
Thank you, John, this is exactly the information I need! And the sample code is super-helpful. I've just been following your cardboard tutorial to add a slide crank to a demo robot, so it's really nice to get an answer from you. :) ...kathy ceceri
- johnpark
- Posts: 985
- Joined: Wed Mar 25, 2009 2:15 pm
Re: External "button"/internal pull-up resistor for CPX in M
So glad that's helpful! Also note there are other ways to read the pins that your students may want to explore, such as this:
https://makecode.com/_gkzTYy0gXa9T
The pull-up resistor means that that pin will normally be "high", so the logic in that if statement says "if the pin _isn't_ high, that means the button has been pressed, grounding it to 'low'". That's why the "not" logic is used.
https://makecode.com/_gkzTYy0gXa9T
The pull-up resistor means that that pin will normally be "high", so the logic in that if statement says "if the pin _isn't_ high, that means the button has been pressed, grounding it to 'low'". That's why the "not" logic is used.
- AnneBarela
- Learn User Page
- Posts: 757
- Joined: Sat Mar 24, 2012 8:56 pm
Re: External "button"/internal pull-up resistor for CPX in M
We have a new guide that helps with the external switch use issues
https://learn.adafruit.com/make-it-switch
https://learn.adafruit.com/make-it-switch
- KathyCeceri
- Posts: 54
- Joined: Sun Mar 02, 2014 7:30 pm
- GuitarHero
- Posts: 40
- Joined: Fri Apr 06, 2018 10:01 am
Re: External "button"/internal pull-up resistor for CPX in M
The first example code from John doesn't work for me, but the second does.
So the button press obviously doesn't create an event.
Any suggestions how to fix that?
So the button press obviously doesn't create an event.
Any suggestions how to fix that?
Please be positive and constructive with your questions and comments.