PICAXE181 - Selfcalibrate touch sensor code

Tooms

Member
First thanks for making the cool 18M2, just what i need for some projekts

PICAXE181 Touch Sensor Demo
I got this test board and it is working well, but just one thing missing that maybe will be a good idea if you some day need to change it.
A small potmeter on a ADC input so you better can play with fine tuning the sensetive of the touch..... just a idea.


And for sharing here is my code for the PICAXE181 board and it is self calibrating each touch pad at startup and seem to work well.

Code:
; *******************************
; ***** Sample Header File  *****
; *******************************
;    Filename: 	  PICAXE181_selfcalib_touch_1.bas	
;    Date: 		  Today	
;    File Version:  0.0.0.1.beta
;    Written by: 	  Tooms
;    Function: 	  selfcalib touch 1	
;    Last Revision: This one
;    Target PICAXE: 18M2
; ******************************* 

Init:
	'Set all led high
	high B.4
	high B.5
	high B.6
	high B.7
	'Calc baseline for C.0 to W10
	for B18 = 1 to 20
		touch16 C.0, W8
		Let W10 = W8 / 20 + W10
	next
	LOW B.4 
	'Calc baseline for C.1 to W11
	for B18 = 1 to 20
		touch16 C.1, W8
		Let W11 = W8 / 20 + W11
	next
	LOW B.5
	'Calc baseline for C.2 to W12
	for B18 = 1 to 20
		touch16 C.2, W8
		Let W12 = W8 / 20 + W12
	next
	LOW B.6
	'Calc baseline for B.3 to W13
	for B18 = 1 to 20
		touch16 B.3, W8
		Let W13 = W8 / 20 + W13
	next
	LOW B.7
	'set detect value based on baseline + add-value
	'The last value in each line controlle how sensetive it is and 500 seem good value
	Let W10 = W10 + 500
	Let W11 = W11 + 500
	Let W12 = W12 + 500
	Let W13 = W13 + 500
	

Main:
	'Touch C.0
	touch16 C.0, W0
	if W0 > W10 then
		high B.4
	else
		Low B.4
	end if
	'Touch C.1
	touch16 C.1, W1
	if W1 > W11 then
		high B.5
	else
		Low B.5
	end if
	'Touch C.2
	touch16 C.2, W2
	if W2 > W12 then
		high B.6
	else
		Low B.6
	end if
	'Touch B.3
	touch16 B.3, W3
	if W3 > W13 then
		high B.7
	else
		Low B.7
	end if
	'Debug
	'pause 500
	goto main

Tooms
 

hippy

Ex-Staff (retired)
Thanks for sharing. There may also be the possibility of dynamic calibration so it can modify itself if changing environment does adversely affect things.

Simplest would be to sample and maintain min and max values and determine a halfway point. Some sort of narrowing min and max towards an average over time may be necessary if there's a extreme case which throws things off.

Not quite sure what the algorithm would actually be, what suits best, just a possibility to throw out there.
 

BeanieBots

Moderator
Was thinking along the same lines myself but got pipped to the post.
As for "what the algorithm would actually be", I was thinking along the lines of:-
noteing the max and min values everytime the sensor is read.
after X number of reads, compare with an EEPROM value.
If different from EEPROM value, then adjust EEPROM value by 1 in the appropriate direction.
X would be chosen to avoid excessive EEPROM updates.
The 'trip' point would be midway (or biased a bit higher) than the mid point.

Probably something I'll never actually get around to coding so I'll leave to somebody else now.
 

MartinM57

Moderator
..or self calibrating at startup and every 10 secs...
Code:
#PICAXE18m2
#terminal 4800
#no_data

start0:

	pause 100
	sertxd ("started....", CR, LF)

	'Calc initial calibration value for C.0 to w10
	for b18 = 1 to 20
		touch16 C.0, w8
		w10 = w8 / 20 + w10
	next
	w10 = w10 + 500
	sertxd ("calibration value...", #w10, cr, lf)

main:
	do
		;read sensor twice and average - an unwound loop - less bytes
		touch16 C.0, w1
		touch16 C.0, w0
		w1 = w1 + w0 / 2
	
		sertxd ("touch = ", #w1, " - ")
	
		if w1 > w10 then
			sertxd ("high", CR, LF)
		else
			sertxd ("low", CR, LF)
		endif

		pause 250
	
	loop; loop back to start
	
start1:
	do
    	'recalibrate every 10 seconds
    	if time > 10 then
    		'Calc calibration value for C.0 to W10
    		'...careful not to set it to 0 on the way...
    		'...as task 0 could go wrong if it picks up a 0 as the baseline 
    		w11 = 0
			for b18 = 1 to 5
				touch16 C.0, w8
				w11 = w8 / 5 + w11
			next
	 		w10 = w11 + 500
    	 	sertxd ("calibration value...", #w10, cr, lf)
    	  	time = 0
    	end if
  loop
...trouble is it recalibrates every 15 seconds, not every 10 :( Looked at it for a few mins, but can't see why...
 

Haku

Senior Member
It's a great start but could with a couple of minor modifications regarding the initial calibration.
Because the Picaxe works in whole numbers the "Let W10 = W8 / 20 + W10" line has reduced accuracy compared with "Let W10 = W10 + W8 + / 2".
The 2nd point is W10 starts off at 0 which could make the final W10 value slightly off, so best to add "touch16 C.0, W10" before the loop, ending up with:
Code:
	'Calc baseline for C.0 to W10
	touch16 C.0,W10
	for B18 = 1 to 20
		touch16 C.0, W8
		Let W10 = W10 + W8 / 2
	next
As for dynamic, ongoing self calibration to adjust for slightly changing environments, I've no idea how you'd code something which on the outset seems quite complicated, just don't change environments :)
 

BeanieBots

Moderator
As for dynamic, ongoing self calibration to adjust for slightly changing environments, I've no idea how you'd code something which on the outset seems quite complicated, just don't change environments :)
Same way a PID temperature controller does only it's even easier because you only need the "I" part. I tried to describe it in post 3. Might have a go (next week) at producing the code if nobody else does. Shouldn't be more than a dozen lines.
 

MartinM57

Moderator
Just make sure it's not recalibrating when you have your finger on the sensor - it measures that as the norm and adds 500 to make the baseline. You then can't make it switch high with your finger until it recalibrates...(been there, done that already)
 

Technical

Technical Support
Staff member
trouble is it recalibrates every 15 seconds, not every 10 :( Looked at it for a few mins, but can't see why...
'Time' uses the internal interrupt timers, which have to be turned off momentarily during commands such as serout to maintain critical timing. As you have lots of serout commands every second in your loops 'time' will take longer on each tick than expected.
 

MartinM57

Moderator
OK - that would be something good to put in the next update of the Manual please. At the moment the manual just seems to indicate it's an invariant elapsed time since reset.

Is it valid to assign directly to time, like I did?

Also on disabletime in the manual, the following is a bit odd..
Code:
pause 5000 ; sleep for 23 seconds
..appears to be a copy/paste from disablebod (where it's odd too)

Look, now I'm turning into WA55 with picky things about the manuals ;)
 

Technical

Technical Support
Staff member
Thanks for the manual corrections.

Time is just a normal word length variable, the only difference is it automatically increments every 1 second. So

let time = 50
if time = w0 then
if time > 500 etc.

are all fine.
 

Marcwolf

Senior Member
Sensitivity and Environment of touch sensor

Hi
Just a quick question.. and please no flames re possible dangers as I am well aware of the insulating needs here

But how would a touch sensor handle being touched by a tounge. For example - making a piece of curved plastic that can fit infront of a mouth - it would be possible to mount touch sensor pads behind the plastic for the user to stick their tounge out and touch.

I'm not sure how the pads would handle residue like saliva giving a false reading.

Just an Idea

Dave
 

BeanieBots

Moderator
Any conductive object near the sensor (eg saliva) would add to the overall capacitance and hence give a slightly different value.
How much would depend on many factors. I think your best bet would be to try it.
 

MartinM57

Moderator
Seeing that the actual touch sensor (i.e the piece of PCB, washer etc) has to be insulated (bottle top, plastic case front panel etc) I can't see a problem on it being tounged (sic) rather than touched with a finger.

Experiment time...
 

Marcwolf

Senior Member
Seeing that the actual touch sensor (i.e the piece of PCB, washer etc) has to be insulated (bottle top, plastic case front panel etc) I can't see a problem on it being tounged (sic) rather than touched with a finger.

Experiment time...

My touch sensor board should be here tomorrow.. YAY!!!
 

hippy

Ex-Staff (retired)
As Marcwolf is our 'resident werewolf' I'd imagine he's after some cleverly concealed activators within a head mask.

The touch sensors should work with any appendage ( oo-er missus ) but what affect saliva residue would have I don't know. I imagine it would be able to compensate for that.
 

cactusface

Senior Member
Touchy subject..

Hi All,
Really must get a couple of these (18M2). What about using drawing pins as a sensor! ie. push it thro' a plastic panel, like a small project ASB/plastic box. If you want to be posh!! some pins are plastic coated.. (colour coding too) What do you think.

Or does it need a return path or ground (0v) connection too.

Regards
Mel.
 
Last edited:

MartinM57

Moderator
No (explicit) return path required - it's just a (capacitive) sensor on the end of a wire going back to the 18M2 touch pin(s).

Plastic coated drawing pins should be fine.

There are dire warnings in the Manual (2?) about not touching the sensor directly - it's not clear why (or what might happen, and with what likelihood), but a Microchip App Note I referenced in another thread had some stuff about ESD protecting the touch pins, but Rev-Ed have made no mention about the techniques it suggests.
 

hippy

Ex-Staff (retired)
The issue with directly touching the pad is that the 'mechanism used' doesn't then work well; you'll likely get wild or maxed-out readings. ESD is an issue, plus the risk of inducing out of spec voltages into the pin, but I think the primary isuue is about getting good, stable, consistent readings.
 

Marcwolf

Senior Member
As Marcwolf is our 'resident werewolf' I'd imagine he's after some cleverly concealed activators within a head mask.

The touch sensors should work with any appendage ( oo-er missus ) but what affect saliva residue would have I don't know. I imagine it would be able to compensate for that.
Yep - 10 out of 10 for Hippy. That exactly what I am planning. I can touch 3 sensors easily and extend that for 5 sensors.

By making the sensor points indented into the plastic so that one must press to fit the tounge into the point one can use the ridges to count to sensors.

To see what I meant - hold two fingers together infront of your mouth. When you press your tounge lightly against them you can feel that your touching the fingers. Press harder and the toungue will form itself into the gap between the fingers where the contact will be.

This could also be used for disabled people.

*Laughs* - Resident Werewolf indeed. Ok - My secrets out.

Take Care
Marc
 

kevrus

New Member
originally posted by technical
'Time' uses the internal interrupt timers, which have to be turned off momentarily during commands such as serout to maintain critical timing. As you have lots of serout commands every second in your loops 'time' will take longer on each tick than expected.
Just wondering, does this also apply to i2c as well
 

MartinM57

Moderator
...don't know, but easy to test

start:
sertxd ("starting 12c commands") ; start your stopwatch
DO 10000 or so i2c commands ; probably don't even need any hardware connected
sertxd ("finished 12c commands") ; stop your stopwtch
sertxd (#time) ; how close does the sertxd'd value match your stopwatch?

...give it a go and report back to the commune ;)

EDIT: idle thought - does the simulator model the time variable accurately - that might be useful (and clever too)
 
Last edited:

hippy

Ex-Staff (retired)
Other than by testing you'll have to wait for an official answer as I don't presently have one. I would expect I2C commands to have less effect on the 'time' variable as commands are quicker to execute whereas SERTXD takes as long as it takes to send all characters ( many ms ) and COUNT etc can be in the region of multiple seconds.

The simulater handles the 'time' variable but how accurately depends on how you mean by accurately. It won't reflect all the effects of lost timing interrupts because that's not how the simulation model works. It very likely just updates the 'time' value when the internal PC clock moves into a new second.
 

kevrus

New Member
MartinM57, I haven't yet ordered my 18's, but itsd imminent, and I will definitely test and report back when I do

Marcwolf, I wonder how many body parts you will be testing :)
 

Marcwolf

Senior Member
MartinM57, I haven't yet ordered my 18's, but itsd imminent, and I will definitely test and report back when I do

Marcwolf, I wonder how many body parts you will be testing :)

Only one - my tounge, and after slobbering all over the board I have demonstrated that it will work. I did have to increate the threshold from 5000 to 9000 in the code and that fixes any saliva that crossed from one contact to another. Only a firm press would trigger the LED.

One of my long term projects (much closer to completion now) is to develop a self contained animatronics system for a suit wearer. If you have seen the Underworld movies or the Predator movies - both of the creature costumes required several off set technicians to make the creature 'emote' - whilst the actor inside of the suit basically did the running around.

My system uses an SD21, a 18M2, and a 4DSystem UDrive to run the animatronic sequences. The next step is to provide an input mechanism that the wearer can use to trigger which sequence to run.

As most costumes have a enclosed head system or one where the actors mouth is well hidden using the tounge is an excellent way to control which sequence to run.

I was working on a voice recognition system using a throat mic - and althought it did work - it was not a universal system which meant that the actor had to train the suit to recognise their commands.

As anyone can stick their tounge out either left, right, or center you have a way to control things.

Part of the current suit includes a head's up display using a MAX7456 video overlay chip, and this can provide instant feedback on what command the actor is selecting.

Even without this a simple 7 segment led can be used to give a visual indication of the command and if positioned next to the actor's eye it can be seen by them but not the public.

Although I am sure that many members of this group can think of lots of fun games one can devise with touch sensors - something akin to "Pin the tail on the Donkey" where the players are blindfolded, have their hands behind their back and using their......

Well - I'll let others complete the picture :>
 
Last edited:
Top