Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 01:15 16 Jul 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 : Need help with touch button

Author Message
littlebear85
Newbie

Joined: 02/02/2016
Location: Australia
Posts: 6
Posted: 02:16pm 25 Feb 2016
Copy link to clipboard 
Print this post

I have been experimenting with touch buttons to switch an led on & off two touch buttons were used and when pressed the text within that button comes up with led on and the other led off...

but I'm wondering if it's possible to have on off from only one touch button?

if so can someone please lead the code in the right direction.


OPTION EXPLICIT

cls


COLOUR RGB(BLUE), RGB(BLACK)
FONT 1, 3


circle 150,50,40 'led on
circle 150,150,40 'led off

main:
DO
setpin 16,dout '
IF TOUCH(X) <> -1 THEN tapped
LOOP
END

SUB tapped
LOCAL tx,ty
tx=TOUCH(X)
ty=TOUCH(Y)

if tx > 110 and tx < 190 and ty > 10 and ty < 90 then
text 150, 45, "LED ON", CM, , 1, RGB(White): pin(16)=1 : print "LED ON"
Endif
if tx > 110 and tx < 190 and ty > 110 and ty < 190 then
text 150, 150, "LED OFF", CM, , 1, RGB(White): pin(16)=0 : print "LED OFF"
endif
pause 200

END SUB


return
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2934
Posted: 02:45pm 25 Feb 2016
Copy link to clipboard 
Print this post

Hi littlebear85,

First of all, welcome on board!

There are many ways to do this - no doubt you will get lots of posts with peoples ideas & recommendations. Below is an approach based on your code:

Assuming that the code you posted is working with two buttons, then slightly modify the following:

1> DIM a variable which we will use to store a 'toggle' status. Call it something like 'ledStat' ; so add this line (below OPTION EXPLICIT): DIM ledStat

2> set the initial value for ledStat to -1. We will be using the values of -1 and 1 to represent Off and On respectively.

3> In your 'tapped' sub, check your tx and ty values for a 'valid touch' of a single button; then if correct, modify your code (in between the IF tx,ty and EndIf) to:

ledStat = ledStat * -1 'this toggles the value between -1 and 1
IF ledStat=-1 THEN
pin(16)=0
print "LED Off" 'add any other commands you need to trigger if LED is Off
ELSE
pin(16)=1
print "LED On"
ENDIF

By the way, in your main Do..Loop, you do not need to keep setting pin(16) to dout. Just do this once before the Do..Loop.

As already mentioned, there are lots of ways to do this; the method above is based on the code you have already written (and understand).

Good luck . . .

WW
 
littlebear85
Newbie

Joined: 02/02/2016
Location: Australia
Posts: 6
Posted: 03:23pm 25 Feb 2016
Copy link to clipboard 
Print this post

Thanks White Wizzard I will give that a go
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2934
Posted: 10:12pm 25 Feb 2016
Copy link to clipboard 
Print this post

@littlebear85,

Have sent you a PM . . .

Basically you need to set the initial value of ledStat to -1. One other 'correction' mentioned in PM.

Let us know if these solve your issue
 
littlebear85
Newbie

Joined: 02/02/2016
Location: Australia
Posts: 6
Posted: 11:47pm 25 Feb 2016
Copy link to clipboard 
Print this post

hi white wizzard the second approach has worked and can switch from the same touch button now
but when I click outside the touch button area I'm getting

[28] If tx > 110 And tx < 190 And ty > 10 And ty < 90 Then
Error: Multiline IF without matching ENDIF
Edited by littlebear85 2016-02-27
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2934
Posted: 11:50pm 25 Feb 2016
Copy link to clipboard 
Print this post

If possible, can you copy/paste your complete code so we can locate the error?

I will run my eyes over your PM'd code but this may not highlight your current issue.

PM code if it is 'sensitive'!
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2934
Posted: 11:53pm 25 Feb 2016
Copy link to clipboard 
Print this post

Add ENDIF after PAUSE 200 (and before END SUB).

That should fix it!!

EDIT: To help find these kind of errors it is good practice to 'tab' lines of code inside DO...LOOP, FOR...NEXT, SELECT CASE.....END SELECT, IF....ELSE...ENDIF etc

Then each 'opening command' (i.e. FOR, DO, SELECT, IF) should have a 'closing command' (i.e. NEXT, LOOP, ENDSELECT, ENDIF) that will be in the same column in your code window.

Best by example:

DO
FOR x = 1 to 100
IF x>10 AND x<90 THEN
PIN(16)=0
ELSE
PIN(16)=1
ENDIF
PAUSE 100
NEXT x
LOOP


Please - no comments about what the code does!! It is just to show tabbed spacingEdited by WhiteWizzard 2016-02-27
 
littlebear85
Newbie

Joined: 02/02/2016
Location: Australia
Posts: 6
Posted: 01:01am 26 Feb 2016
Copy link to clipboard 
Print this post

that done the trick

now I need led off text come up on the touch when led is off
 
littlebear85
Newbie

Joined: 02/02/2016
Location: Australia
Posts: 6
Posted: 01:16am 26 Feb 2016
Copy link to clipboard 
Print this post

I have altered this line but the text is over lapping the other

print "LED Off" : text 150, 45, "LED OFF", CM, , 1, RGB(White): pin(16)=0 : print "LED OFF
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2934
Posted: 01:22am 26 Feb 2016
Copy link to clipboard 
Print this post

@littlebear85

PM your exact code to me and I will gladly pinpoint the issue of 'overlapping' code.

I know one thing we need to do but I just want to eliminate any other causes first by removing 'redundant' lines of code that may be adding to the cause.

You can also email me (address in my 'footer' of any of my posts!)

EDIT: can you take a quick photo of your 'overlapping text' and email that - doesn't need to be high quality! Thanks Edited by WhiteWizzard 2016-02-27
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2934
Posted: 02:31am 26 Feb 2016
Copy link to clipboard 
Print this post

@littlebear85

Code returned in PM with 'overlapping' issue hopefully fixed.
Remember that any text/string drawn by the TEXT command will only use the space needed on the TFT to display the string. Hence anything on the display already 'outside' the required area will remain in situ.

In the returned code, I forgot to tidy the SETPIN 16,DOUT command. Move this out from your main DO...LOOP and place it immediately before the DO line (i.e. you only need to set the pin once!)

WW
 
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