![]() |
Forum Index : Microcontroller and PC projects : Read buttons problem
Author | Message | ||||
coliscip Newbie ![]() Joined: 16/05/2022 Location: RomaniaPosts: 5 |
Hello! I have connected 6 buttons from GP2 to gp6 to ground, without resistors, directly to ground I've tried: SetPin GP2, DIN, PULLUP SetPin GP3, DIN, PULLUP SetPin GP4, DIN, PULLUP SetPin GP5, DIN, PULLUP SetPin GP6, DIN, PULLUP SetPin GP7, DIN, PULLUP Do If Pin(GP2) = 1 Then Print "2" If Pin(GP3) = 1 Then Print "3" If Pin(GP4) = 1 Then Print "4" If Pin(GP5) = 1 Then Print "5" If Pin(GP6) = 1 Then Print "6" If Pin(GP7) = 1 Then Print "7" Loop -------------------------- and -------------------------- SetPin GP2, DIN SetPin GP3, DIN SetPin GP4, DIN SetPin GP5, DIN SetPin GP6, DIN SetPin GP7, DIN Do If Pin(GP2) = 1 Then Print "2" If Pin(GP3) = 1 Then Print "3" If Pin(GP4) = 1 Then Print "4" If Pin(GP5) = 1 Then Print "5" If Pin(GP6) = 1 Then Print "6" If Pin(GP7) = 1 Then Print "7" Loop and it print 2 3 4 5 6 7 2 3 4 5 6 7 2 3 4 5 6 7 and so on...forever, till I stop it In both variant of program (with or without "pullup" in defining of pins) What do I wrong? Thank you in advance! |
||||
coliscip Newbie ![]() Joined: 16/05/2022 Location: RomaniaPosts: 5 |
I forgot to mention... I do not press any key and it shows 2,3,4,5,6,7,2,3,4,5,6,7 ... |
||||
Quazee137![]() Guru ![]() Joined: 07/08/2016 Location: United StatesPosts: 593 |
try If Pin(GPx) = 0 Then Print "x" |
||||
coliscip Newbie ![]() Joined: 16/05/2022 Location: RomaniaPosts: 5 |
YES!!! tried: SetPin GP2, DIN, PULLUP SetPin GP3, DIN, PULLUP SetPin GP4, DIN, PULLUP SetPin GP5, DIN, PULLUP SetPin GP6, DIN, PULLUP SetPin GP7, DIN, PULLUP Do If Pin(GP2) = 0 Then Print "2" If Pin(GP3) = 0 Then Print "3" If Pin(GP4) = 0 Then Print "4" If Pin(GP5) = 0 Then Print "5" If Pin(GP6) = 0 Then Print "6" If Pin(GP7) = 0 Then Print "7" Loop And IT WORK! Thank you Quazee137 ! |
||||
coliscip Newbie ![]() Joined: 16/05/2022 Location: RomaniaPosts: 5 |
Now, please, how do I make to read just one time a button, even if i keep pressed a button? Thank you in advance! Ciprian |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5089 |
Hi Ciprian, In your test, you test for low level (key down) to print the text. Next you should wait for key release (key up). If Pin(GP2) = 0 Then Print "2" do: loop until pin(gp2)=1 'key up end if If Pin(GP3) = 0 Then Print "3" etc,,, It gets more complex if you need to register multiple keys at the same time... Edited 2022-07-05 05:40 by Volhout PicomiteVGA PETSCII ROBOTS |
||||
Tinine Guru ![]() Joined: 30/03/2016 Location: United KingdomPosts: 1646 |
Something like this (unable to test, right now): SetPin GP2, DIN, PULLUP SetPin GP3, DIN, PULLUP SetPin GP4, DIN, PULLUP SetPin GP5, DIN, PULLUP SetPin GP6, DIN, PULLUP SetPin GP7, DIN, PULLUP portmem% = 0 portread% = 0 do portread% = port(gp2,6) if portread% <> portmem% then if portread% and not 1 then print "2" if portread% and not 2 then print "3" if portread% and not 4 then print "4" if portread% and not 8 then print "5" if portread% and not 16 then print "6" if portread% and not 32 then print "7" end if portmem% = portread% loop Edited 2022-07-05 06:35 by Tinine |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7937 |
How about allocating interrupts to the pins and toggling a bit on just the falling edge? Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4044 |
With just switches, don't be surprised to get multiple apparent presses due to glitches. If you do, you'll need hardware or software de-bounce. Jo9hn |
||||
Tinine Guru ![]() Joined: 30/03/2016 Location: United KingdomPosts: 1646 |
Debounce could be something like: SetPin GP2, DIN, PULLUP SetPin GP3, DIN, PULLUP SetPin GP4, DIN, PULLUP SetPin GP5, DIN, PULLUP SetPin GP6, DIN, PULLUP SetPin GP7, DIN, PULLUP portread% = 0 scan_period% = 5 scan_count% = 0 settick scan_period%, scanin 'Read the port every 5ms do if scan_count% > 7 then 'State of port hasn't changed for at least 8 scans if portread% and not 1 then print "2" if portread% and not 2 then print "3" if portread% and not 4 then print "4" if portread% and not 8 then print "5" if portread% and not 16 then print "6" if portread% and not 32 then print "7 scan_count%=0 end if loop sub scanin if scan_count% = 0 then portread% = port(gp2,6) inc scan_count% elseif portread% <> port(gp2,6) then scan_count% = 0 else inc scan_count% end if end sub Craig |
||||
Tinine Guru ![]() Joined: 30/03/2016 Location: United KingdomPosts: 1646 |
Oops, I see a boo-boo: SetPin GP2, DIN, PULLUP SetPin GP3, DIN, PULLUP SetPin GP4, DIN, PULLUP SetPin GP5, DIN, PULLUP SetPin GP6, DIN, PULLUP SetPin GP7, DIN, PULLUP portread% = 0 filtered% = 0 scan_period% = 5 scan_count% = 0 settick scan_period%, scanin 'Read the port every 5ms do if scan_count% > 7 then 'State of port hasn't changed for at least 8 scans if filtered% and not 1 then print "2" if filtered% and not 2 then print "3" if filtered% and not 4 then print "4" if filtered% and not 8 then print "5" if filtered% and not 16 then print "6" if filtered% and not 32 then print "7 scan_count%=0 end if loop sub scanin if scan_count% = 0 then portread% = port(gp2,6) inc scan_count% elseif portread% <> port(gp2,6) then scan_count% = 0 else inc scan_count% if scan_count% = 8 then filtered% = portread% end if end if end sub |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |