Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:59 01 Aug 2025 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 : Read buttons problem

Author Message
coliscip
Newbie

Joined: 16/05/2022
Location: Romania
Posts: 5
Posted: 05:53pm 04 Jul 2022
Copy link to clipboard 
Print this post

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: Romania
Posts: 5
Posted: 05:57pm 04 Jul 2022
Copy link to clipboard 
Print this post

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 States
Posts: 593
Posted: 06:00pm 04 Jul 2022
Copy link to clipboard 
Print this post

try

  If Pin(GPx) = 0 Then Print "x"
 
coliscip
Newbie

Joined: 16/05/2022
Location: Romania
Posts: 5
Posted: 06:06pm 04 Jul 2022
Copy link to clipboard 
Print this post

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: Romania
Posts: 5
Posted: 06:09pm 04 Jul 2022
Copy link to clipboard 
Print this post

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: Netherlands
Posts: 5089
Posted: 07:39pm 04 Jul 2022
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 1646
Posted: 08:31pm 04 Jul 2022
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 7937
Posted: 08:59pm 04 Jul 2022
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 4044
Posted: 09:49pm 04 Jul 2022
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 1646
Posted: 04:50am 05 Jul 2022
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 1646
Posted: 05:45am 05 Jul 2022
Copy link to clipboard 
Print this post

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


 
Print this page


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

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025