TOUCH or NO TOUCH

techElder

Well-known member
I use a 2 TASK program to accomplish something. The PAUSEs in these routines are about 1/3 too slow in a multiplexed program with 2 TASKs.

I've used this routine to determine what the base line NO TOUCH point is on a 1 inch diameter sensor made from double-sided PCB material.

Code:
calibrateNoTouch:		' Periodically determine and store the touch ON/OFF levels with no touch
	
	touchOffLevel = 0	' reset every time

	for counter = 1 to 10

		touch16 TOUCHPIN, adcreading

		touchOffLevel = adcreading / 10 + touchOffLevel

	next

	touchOnLevel = touchOffLevel + 100

	time = 0		' restart the time

	return
To go along with that base line routine, I use the following to determine whether a touch to the sensor has been there for 2 seconds. I'm not at all satisfied with this because the PAUSE 2000 allows another TASK to gain attention which makes that TASK execute quicker. Not good when you want an LED to blink at a consistent rate.

I hope someone has a better TOUCH routine to suggest.

Code:
do

	touch16 TOUCHPIN, adcreading

	if adcreading > touchOnLevel then

		' here is the problem line. The PAUSE gives the other TASK more attention
		pause 2000		' wait to see if still touched after 2 seconds

		touch16 TOUCHPIN, adcreading	' see if the touch is still there

		if adcreading > touchOnLevel then
			suspend 1 ' suspend the other task while touching
			' do something here
			gosub flashSomething ' change accepted
			' pause 1500
			restart 1 ' restart the other task after touching stuff complete
		endif

	else

		if time > 10 then		' wait 10 seconds before allowing another no touch calibration
			
			gosub calibrateNoTouch
			
			gosub checkBattery
	
		endif
	endif

loop
 

nanogear

Member
I use a 2 TASK program to accomplish something. The PAUSEs in these routines are about 1/3 too slow in a multiplexed program with 2 TASKs.

I've used this routine to determine what the base line NO TOUCH point is on a 1 inch diameter sensor made from double-sided PCB material.

Code:
calibrateNoTouch:		' Periodically determine and store the touch ON/OFF levels with no touch
	
	touchOffLevel = 0	' reset every time

	for counter = 1 to 10

		touch16 TOUCHPIN, adcreading

		touchOffLevel = adcreading / 10 + touchOffLevel

	next

	touchOnLevel = touchOffLevel + 100

	time = 0		' restart the time

	return
To go along with that base line routine, I use the following to determine whether a touch to the sensor has been there for 2 seconds. I'm not at all satisfied with this because the PAUSE 2000 allows another TASK to gain attention which makes that TASK execute quicker. Not good when you want an LED to blink at a consistent rate.

I hope someone has a better TOUCH routine to suggest.

Code:
do

	touch16 TOUCHPIN, adcreading

	if adcreading > touchOnLevel then

		' here is the problem line. The PAUSE gives the other TASK more attention
		pause 2000		' wait to see if still touched after 2 seconds

		touch16 TOUCHPIN, adcreading	' see if the touch is still there

		if adcreading > touchOnLevel then
			suspend 1 ' suspend the other task while touching
			' do something here
			gosub flashSomething ' change accepted
			' pause 1500
			restart 1 ' restart the other task after touching stuff complete
		endif

	else

		if time > 10 then		' wait 10 seconds before allowing another no touch calibration
			
			gosub calibrateNoTouch
			
			gosub checkBattery
	
		endif
	endif

loop

It can be used a if/then with time = 2 instead of pause 2000 ?
 

techElder

Well-known member
nanogear, you don't have to quote and entire post every time you post a question. Pick and choose what you want to quote. It's very hard to read threads where everyone that posts a reply quotes the entire post above their new post. It seems ridiculous after a few posts. Enough on that.

I use the "time" variable elsewhere, so I had to use PAUSE in this routine.

You can even use a plain FOR ... NEXT statement to delay.

The main point with that delay is to keep the routine from branching for inconsequential touches. Holding the touch for greater than 2 seconds is the entry to to the menu routine.
 
Top