How to exit loop when touch sensor "pressed" for two seconds??

nanogear

Member
In this subrutine:

Code:
08M2

subturnoff: 
           low B.2
           pause 2000
           high B.2 , B.0
           time = 0
           do
                touch16 [TOUCHCONFIG], C.4,w1	
           loop until w1 > 3600 
           goto main
How can I make that the picaxe exit loop until w1 > 3600 after two seconds?


Thank you so much!!
 

techElder

Well-known member
I don't put mine in a loop. I just check the TOUCH again after delaying PAUSE 2000. If the sensor is still touched, branch to that part of the program.
 

hippy

Ex-Staff (retired)
You could reset the time to zero whenever the TOUCH value is < 3600, so time will only ever be > 2 when the touch value has always been over 3600 for those two seconds ...

Code:
time = 0
Do
  Touch16 [TOUCHCONFIG], C.4, w1
  If w1 < 3600 Then
    time = 0
  End If
Loop Until time > 2
 

erco

Senior Member
Another option is execute a subroutine to measure how long the sensor was touched. Something like:

testsensor: ' touchpad subroutine waits for a touch and release, "times" it in variable b1
b1=0 ' reset counter
flag=0 ' flag=0 unless 2 seconds exceeded
retest:touch16 [TOUCHCONFIG], C.4,w1 ' check sensor
if w1>3600 then inc b1:endif ' increment counter b1 if triggered
if w1<3600 and b1>0 then exit ' go to exit after sensor triggered & released
pause 50 ' small pause, adjust for timing
goto retest ' loop back
exit: if b1>n then flag=1 ' set flag=1 if 2 seconds exceeded
return

I'm far from a PicAxe and that's off the top of my head, but something along those lines will work. By measuring the length of the touch, you could also branch to different parts of the program.
 

nanogear

Member
Another option is execute a subroutine to measure how long the sensor was touched.
I'm far from a PicAxe and that's off the top of my head, but something along those lines will work. By measuring the length of the touch, you could also branch to different parts of the program.
Thank you erco for your help. I have another project that need meausure time of the touch sensor touched and your example can help me to understand how to design the code. For the purposes of this code, I only needed a simple loop and the example of hippy works great.

Thank you so much!
 

erco

Senior Member
Happy to help. After a few changes, this syntax checked out, but I don't have an 08M2 to try it out.
Code:
#picaxe 08m2

testsensor: ' touchpad subroutine waits for a touch and release, "times" it in variable b1
 b1=0 ' reset counter
 bit0=0 ' flag bit0=0 unless ~2 seconds exceeded
 retest:touch16 [%11101000], C.4,w1 ' check sensor
 if w1>3600 then inc b1:endif ' increment counter b1 if triggered 
if w1<3600 and b1>0 then exita ' go to exit after sensor triggered & released
 pause 50 ' small pause, adjust for timing
 goto retest ' loop back
 exita: if b1>40 then let bit0=1:endif ' set flag bit0=1 if 2 seconds exceeded
 return
 

IronJungle

Senior Member
I'm not at a picaxe to test this, but my limited experience with touch sensors told me that they do not always take the same amount of time to detect a touch. If that is indeed the case I think erco's code may be unreliable. B1 would not increment at predictable intervals.

Having said that, erco has a few zillion times more experience with touch sensors so I could be full of it.....:eek:
 
Top