A & B Button Problem

Issue #36 resolved
Turgut Guneysu created an issue

Hi John,

I wast testing the button detection in microBlocks and noticed that “When B pressed” is not working right.

Please check out the code attached.

In my machine, A-button press displays LEFT bar correctly. However, B-button press displays what A&B press is supposed to display: the + sign.

In case of problems with upload, here is a pic of the blocks as well:

ALSO: As you know in micro:bit there are three distinct button states: A, B, A&B. And there are corresponding event blocks for all three.

In microBlocks, there is no A&B state capture. One has to resort to code as I have done above and code it with a logical AND.

But this creates a problem when no A&B is wanted and as such NOT coded. Yet when BOTH buttons are pressed the corresponding blocks execute and one gets an unwanted result. This does not happen in micro:bit coding environment MakeCode.

FINALLY: there is a difference in the interpretation of button press between MakeCode and microBlocks.

In MakeCode: a buttonPress happens when the button is pressed and released. More like a CLICK.

In microBlocks: a buttonPress happens when a button is pressed, not when it is CLICKED.

This makes it hard to port programs over due to different event states. And since there is no separate CLICKED state detection in microBlocks, as it is in MakeCode, it is also hard to explain to kids all the nuances of these events and the differences in the environments.

For harmony reasons:

  • is it possible to make the button press events similar, ie: detect CLICKs, rather than down presses.
  • have a third A&B event

This way, if someone wants to have finer granularity of UP/DOWN events, they can code it with buttonA/B true/false states.

And those who just want simple press detection can use the “When A/B/A&B pressed” events. Better yet rename them to CLICKED!

Sorry for the long description.

TG

Comments (7)

  1. John Maloney repo owner

    Interesting. The button hats are behaving as I expect them to, and I believe that behavior is consistent even though it is different from the MakeCode event model. You’re right that it’s not easy to code A & B pressed in MicroBlocks. On the other hand, I like the fact that button actions are triggered on the button down event. It’s more immediate, and that behavior matches what you get if connect a button to a digital input pin.

    I’ll think more about this issue.

  2. Turgut Guneysu reporter

    Hi John

    Just to clarify your statement “the button hats are behaving as I expect them to”, you DID NOT get the error I have described in the opening of my message, where when B-button is pressed one gets the A&B results ???

    For the rest of your reply, I'll await the results of your pondering. I concur that button down vs CLICK detection is more direct and REAL so to speak, however only for those interested in coding at that granularity level. I have yet to see that in the beginning groups that I've been dealing with. Not to mention that CLICK detection does not eliminate or prevent the possibility of wanting to detect up/down events in microBlocks. It just makes it simpler for the small ones.

    Thanks for your consideration.

  3. John Maloney repo owner

    Oh, I see! You put the “and” block into the “when A button pressed” block, not a general “when <condition>” block. When I tried this before, I reproduced your code from the pictures, and I didn’t notice that. I used the “when <condition>” hat, which worked as expected.

    When I loaded your project, I saw the behavior you describe.

    So there is a bug here. MicroBlocks should either not allow you to put the “and” block into a “when button pressed” hat or it should have done what you expected. Thanks for finding that!

    I thought of a way to have both MakeCode-like button events and MicroBlocks' current “act on button down” behavior. We could create a “Button Events” library that listens for various combinations of button presses and uses the broadcast mechanism to trigger user scripts for “A button clicked”, “B button clicked”, and “A and B clicked”. People porting projects from MakeCode that depend on MakeCode button behavior could simply import this library. As an added bonus, curious users could actually look inside the library to find out how it was implemented or even customize and extend it.

  4. Turgut Guneysu reporter

    Thanks John. I find your suggestion an excellent and very useful enhancement.

    I was just getting ready to write to you about my misuse of the “when A button pressed” block to detect A&B presses. I realized too late that I had not used the “when <condition>” block. Sorry. I was so conditioned that block coding did not allow errors, I did not even pay attention to the wrong block I had selected. Oh well, all for the betterment of the new tools!

  5. John Maloney repo owner

    Several people have asked for an easy way to trigger an action when the A and B buttons are pressed at the same time. The next release will extend the current “when _ button pressed” hat to include an “A+B” option in the menu. When that option is selected the hat will be triggered when the A and B buttons are pressed “at the same time” – that is, within about a tenth of second. If there is more time between the two button presses then they will be treated as separate A and B button events.

  6. Log in to comment