Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 17:50 28 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : 1 pin button pad

Author Message
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 08:40pm 26 May 2023
Copy link to clipboard 
Print this post

Hi. the quid button pads arrived and seem to work. Using at 3.3V though they're arduino so for 5V. results for up,down,left,right,right button.
SETPIN 31, AIN
do
PRINT PIN(31)
pause 1000
loop




 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 09:09pm 26 May 2023
Copy link to clipboard 
Print this post

this will be easier to case
SETPIN 31, AIN
do
bp=int(PIN(31)*1000)
PRINT bp 'PIN(31)
pause 1000
loop
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 11:57pm 26 May 2023
Copy link to clipboard 
Print this post

It works well. My tron game is much more playable compared to touch area buttons.. and poking lcd screens is not good for them.Good button pad for the price of a mars bar and uses just 1 pin.
 OPTION EXPLICIT
 OPTION DEFAULT NONE
 OPTION DEFAULT INTEGER
 OPTION BASE 1 'arrays start at 1
 option autorun on
 dim px,py,pdx,pdy
 dim cx(3),cy(3),cdx(3),cdy(3),cpa(3)
 dim ts,tr,csc=0,psc=0,cm
 dim bp
 SETPIN 31, AIN
 
 cls

do
'init players start positions
 px=260:py=rnd*120+60:pdx=-1:pdy=0
 cx(1)=rnd*100+80:cy(1)=50:cdx(1)=0:cdy(1)=1:cpa(1)=1
 cx(2)=rnd*100+80:cy(2)=190:cdx(2)=0:cdy(2)=-1:cpa(2)=1
 cx(3)=rnd*50+50:cy(3)=rnd*80+80:cdx(3)=1:cdy(3)=0:cpa(3)=1
 
'draw play area & 4 random triangles on border
 box 0,0,319,239,1,rgb(white),rgb(black)
 ts=rnd*160+40
 triangle 0,ts,12,ts+8,0,ts+16,rgb(White)
 ts=rnd*160+40
 triangle 319,ts,307,ts+8,319,ts+16,rgb(White)
 ts=rnd*240+40
 triangle ts,0,ts+8,10,ts+16,0,rgb(White)
 ts=rnd*240+40
 triangle ts,239,ts+8,228,ts+16,239,rgb(White)
 line 0,239,80,239,1,rgb(blue)'left button
 line 140,239,220,239,1,rgb(blue)'right button
 line 319,40,319,120,1,rgb(blue)'up button
 line 319,179,319,239,1,rgb(blue))'down button
 'main
 do
'check user button pressed
bp=int(PIN(31)*1000)
if bp<3000 then 'if less than 3.3V then a button pressed
 button
end if
px=px+pdx:py=py+pdy
'has player crashed  
   if pixel (px,py) >0 then 'player crashed
     box 0,0,319,239,1,rgb(white),rgb(black)
     text 60,100,"You Lost",l,5:pause 5000:exit do
   end if
   pixel px,py,rgb(magenta)      
'computer move
   for cm=1 to 3
     if cpa(cm)=1 then
       if cdx(cm)<>0 then
         if pixel (cx(cm)+cdx(cm),cy(cm)) >0 then
           cdx(cm)=0:cdy(cm)=(py>cy(cm))-(py<cy(cm))
         end if
       else
         if pixel (cx(cm),cy(cm)+cdy(cm)) >0 then
           cdy(cm)=0:cdx(cm)=(px>cx(cm))-(px<cx(cm))
         end if
       end if  
       cx(cm)=cx(cm)+cdx(cm):cy(cm)=cy(cm)+cdy(cm)
 'has computer player crashed      
       if pixel (cx(cm),cy(cm)) >0 then
         cpa(cm)=0'computer crashed
         if cpa(1)+cpa(2)+cpa(3)=0 then 'all crashed
           box 0,0,319,239,1,rgb(white),rgb(black)
           text 70,100,"You Won",l,5:pause 5000:exit do
         end if
       end if
         pixel cx(cm),cy(cm),rgb(yellow)
     end if  
   next cm
   
   pause 50 'game speed
 loop
 
loop
sub button 'which button pressed?
 if bp<20 then if pdx=0 then pdy=0:pdx=-1:end sub'left pressed?
 if bp<1800 then if bp>1500 then if pdx=0 then pdy=0:pdx=1:end sub'right pressed?
 if bp<700 then if bp>300 then if pdy=0 then pdx=0:pdy=-1:end sub'up pressed?
 if bp<1200 then if bp>900 then if pdy=0 then pdx=0:pdy=1'down pressed?
end sub
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 01:06am 27 May 2023
Copy link to clipboard 
Print this post

1 hand and a camera in the other  https://www.youtube.com/watch?v=ChLwrszmFig
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 06:16am 27 May 2023
Copy link to clipboard 
Print this post

SELECT/CASE might be a better way to test the buttons.


do
 bp=pin(31)
 select case bp
   case <.02
     do stuff here
   case 0.6 to 0.72
     do stuff here
   case 1.1 to 1.3
     do stuff here
   case 1.78 to 1.82
     do stuff here
 end select
loop


(values shown are only examples)

It has a couple of advantages. You are window testing so you can easily test for buttons while still allowing for resistance tolerances. Good for diagonals. It's fast. You can put the most likely buttons early in the select statement then any later ones won't be tested. No *1000 needed - FP is as fast as integers. You can make it very structured if you want too. with each case setting the direction then calling a common movement sub.

Just an idea - it may be worth a try for comparison.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 02:14pm 27 May 2023
Copy link to clipboard 
Print this post

Thanks Mick. I converted the original touch screen using case to a sub as I was not sure how to use case in this instance so thanks for the info. Will try.
'define touch areas  
 gui area 1,0,199,80,40 ':line 0,239,80,239,rgb(blue)'left button
 gui area 2,140,199,80,40 ':line 140,239,220,239,rgb(blue)'right button
 gui area 3,279,40,40,80 ':line 319,40,319,120,rgb(blue)'up button
 gui area 4,279,159,40,80 ':line 319,179,319,239,rgb(blue)'down button
'check user button pressed
   tr=TOUCH(REF)
   if tr>0 then
     select case tr
       case 1 'left pressed
         if pdx=0 then pdy=0:pdx=-1
       case 2 'right pressed
         if pdx=0 then pdy=0:pdx=1
       case 3 'up pressed
         if pdy=0 then pdx=0:pdy=-1
       case 4 'down pressed
         if pdy=0 then pdx=0:pdy=1
     end select
   end if
   px=px+pdx:py=py+pdy

Touch buttons work but not too good with finger presses. The button board is much better and the buttons click when pressed but pressing 2 buttons at once needs another look at. Handy board for the price.
Cheers, stan
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 04:25pm 27 May 2023
Copy link to clipboard 
Print this post

Case worked using float var
'check user button pressed
bp!=pin(31)
if bp!<3.1 then 'a button has been pressed
select case bp!
  case <.02
    if pdx=0 then pdy=0:pdx=-1 'left pressed
  case 1.5 to 1.8
    if pdx=0 then pdy=0:pdx=1 'right pressed
  case 0.3 to 0.6
    if pdy=0 then pdx=0:pdy=-1 'up pressed
  case 0.9 to 1.1
    if pdy=0 then pdx=0:pdy=1 'down pressed
end select
endif
px=px+pdx:py=py+pdy

I guess I could skip case 0.9 to 1.1 as if the other 3 buttons were not pressed then it must be the last one.
happy trails, stan
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 04:50pm 27 May 2023
Copy link to clipboard 
Print this post

The variables default to float so you don't really need the ! either unless you've used OPTION DEFAULT INTEGER. :)

You don't really need the test for <3.1 either. Not in this case as there aren't that many comparison tests so it's not going to slow things down much. YMMV of course.

You can use CASE ELSE as the last case and use it to zero pdx and pdy. That will make it do nothing if no buttons are pressed.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 06:51pm 27 May 2023
Copy link to clipboard 
Print this post

if bp!<3.1 then 'a button has been pressed
is there to see why bother testing no keys pressed
Mick, you have shown a way of case that wasn't obvious to me from the manual.ta
That float is fast I got to get used to...not used to it in microcontroller basic
the post was about the button pad and how cheap and it works easy with 1 a to d pin.
I'm impressed it's so simple yet works. switches and pull ups and de bounce and wires and lots of gpio pins l8r. for 5 buttons this is handy.. in the spirit,hobbyist

is float as fast as integer? I can't be to do loops and timer to try, someone else must have done this.
Edited 2023-05-28 06:14 by stanleyella
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 08:28pm 27 May 2023
Copy link to clipboard 
Print this post

Floats are pretty fast, but integer is still faster, generally. Try using a float as a for-next variable and compare it with an integer.  :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024