Page 1 of 1

Programming for button release

Posted: Thu Jul 20, 2023 4:15 pm
by davey110
I want to program a button so that after it is pressed nothing actually happens until the button is released. (Then I could time how long it was pressed, so as to avoid accidental taps.)

But it appears that BUTTON_PRESSED doesn't work a second time until it has been released.

I tried the following code, but it does not wait for the button to be released. What would be the proper way to do this? Is it even possible?

OPTION BASE 1
GRAPHICS
BUTTON "test" TEXT "button" AT 100,100 SIZE 200,100

DO
p=BUTTON_PRESSED ("test")
IF p=1 THEN PRINT "pressed"
UNTIL p=1

DO
PRINT p ' At this point, p=1 as expected
p=BUTTON_PRESSED ("test") ' I expected it to still indicate the button as pressed, since it is being held pressed.
PRINT p ' At this point, p has changed back to 0 even though the button was kept pressed
UNTIL p=0

PRINT "released"
TEXT ' switching to TEXT reveals what was printed
END

Re: Programming for button release

Posted: Thu Jul 20, 2023 9:12 pm
by Mr. Kibernetik
BUTTON_PRESSED means BUTTON_was_PRESSED rather than BUTTON_is_PRESSED

Maybe you can try sprites to get exact on/off state. You can check "Music & Sound/synthesizer.txt" example.

Re: Programming for button release

Posted: Fri Jul 21, 2023 12:14 am
by davey110
How do you find examples/music&sound/synthesizer.txt? I have been going around in circles for 15 min trying to get it. When I think I've almost got it it's not there and I end up going back to Board Index. Are there instructions somewhere how to find things on the Forums?

Re: Programming for button release

Posted: Fri Jul 21, 2023 8:48 am
by Dutchman
The folder 'Examples' in the root-folder contains several folders with example-programs. These programs can be modified in order to get experience with the functions and commands. If that folder is missing, then you can restore it with the following command (in a one-line program):
RESTORE EXAMPLES
restores deleted or modified example files.

Re: Programming for button release

Posted: Fri Jul 21, 2023 10:26 am
by Mr. Kibernetik
Examples folder is in your BASIC application files, not on Forum.
This folder was there when you just installed BASIC.

If for some reason you are missing this folder in your BASIC app, you can follow Dutchman's advice to restore it.

Re: Programming for button release

Posted: Fri Jul 21, 2023 5:38 pm
by davey110
I was looking for Examples (and the file) on the forum, instead of the device. I found it now. Thank you

Re: Programming for button release

Posted: Wed Jul 26, 2023 4:58 pm
by Chooch912
I am intrigued by what davey110 is trying to do and was looking at the synthesizer example, but when I press the keys I can't get any sounds on my iPad 8th gen, v 16.5.1. I made sure my sound setting was on and rebooted my iPad 3 times with still no sound.

What am I doing wrong?

REVISED
Okay, I just tried "play midi file" and the sound came on without doing anything else. I then tried the synthesizer and I had sound.

Why didn't I get sound on synthesizer the first 3 times I tried it?

Re: Programming for button release

Posted: Tue Oct 03, 2023 4:02 am
by davey110
Mr. Kibernetik wrote:
Thu Jul 20, 2023 9:12 pm
BUTTON_PRESSED means BUTTON_was_PRESSED rather than BUTTON_is_PRESSED

I think I am not clear on how buttons operate: Exactly what do you mean "button WAS pressed"? When was the button pressed? When does it no longer indicate that it is pressed? ( i.e. expression returns "0"). I have assumed that when it is released it will immediately give "0" as the result. I think I'm missing something here that is causing me problems.

Re: Programming for button release

Posted: Wed Oct 04, 2023 6:19 pm
by Mr. Kibernetik
davey110 wrote:
Tue Oct 03, 2023 4:02 am
I think I am not clear on how buttons operate: Exactly what do you mean "button WAS pressed"? When was the button pressed? When does it no longer indicate that it is pressed? ( i.e. expression returns "0"). I have assumed that when it is released it will immediately give "0" as the result. I think I'm missing something here that is causing me problems.
"Pressed" means not "pressed and then released" or "pressed and is still pressing", but just "pressed". It is not important if you have already released it or not. Releasing the button is not important, because buttons operate on press, not on release.
Any next check will return 0 until you press the buttobn again.

Once again. It is not important when you pressed the button and when you checked for that fact. If you have pressed the button some time before, the check will return 1. And any next check will return 0 untill you press the button again. "Press" and "release" are different events, and releasing the button is not detected.

Re: Programming for button release

Posted: Thu Oct 12, 2023 3:59 am
by davey110
Thank you for those details. I have a routine which scans the button and toggles its function. But if I couldn’t detect the button release then it would just keep scanning and toggling extremely rapidly. So I used this routine:

IF BUTTON_PRESSED("E") THEN
p=1
WHILE p=1 ' wait for button release
p=BUTTON_PRESSED("E")
ENDWHILE
e2=1-e2 ‘ toggle e2 between 0 and 1
ENDIF

(Program continues according to value of e2).

Maybe not the best way, but your explanation will help next time. Thank you.