scaling joystick/potentiometer for varying pause

tony_g

Senior Member
lately i have been playing around with my wii nunchuk again using the joystick axes to control an el cheapo stepper motor using the received value to control the pause between steps for control of its rotational speed from slow to fast or vice versa.

the neutral value for the axis rests at 128 and i have had no problem scaling the pause value for it from the joystick input of 0 to 126 having a pause of 400 between steps if the axis value is 126 up to a pause 1 between steps if the stick is hard left with a 0 value.

the issue for me which i seem to get stumped on is scaling fro the opposite direction going from 130 to 255 and getting the pause going the same as the opposite direction in the manner of pause 400 for stick value of 130 to pause 1 for stick at 255.


Code:
#com 3
#picaxe 20m2
symbol joy_x = b1
symbol joy_y = b2


dirsb=%00011110

Init:
i2cslave $A4, i2cslow, i2cbyte
Writei2c 0xf0,(0x55)
Pause 10
Writei2c 0xfb,(0x00)
Pause 10

retrieve:
writei2c (0)
Pause 10
Readi2c (joy_x,joy_y)


if joy_x <126 then pan_left
if joy_x> 130 then pan_right
goto retrieve

pan_left:
for b10=0 to 3
	lookup b10,(%00011000,%00001100,%00000110,%00010010),pinsb
	w2= joy_x*80/21+1
	pause w2
	next b10
goto retrieve
	
pan_right:
for b10=0 to 3
	lookup b10,(%00010010,%00000110,%00001100,%00011000),pinsb
	w3= ??????
	pause w3
	next b10

goto retrieve

i had used some scaling code that was shown to me to use for a different project in the past and was able to use the calculations to get the lower scaled pause value but have not been able to use it successfully to get the upper stick values scaled accordingly and now im a little stumped with it lol.

thanks,
tony
 

bluejets

Senior Member
I had a similar problem using r/c input to a sound board. Neutral position low frequency and increase in either direction.
Can't remember how I did it but it was fairly easy. I will try to find it.
I think i may have posted it here somewhere, diesel sound on an 08m.
 

tony_g

Senior Member
thanks bluejts, i will have a look around.

diesel engine sound, thats an interesting idea to add to a trial truck im slowly putting together amongst other projects, did you use the 08m to generate the sound frequencies for that?
 

westaust55

Moderator
Try:
w3 = - Joy_x -1 * 80 /21 +1

If you want the number of steps totally balanced then change the test:
if joy_x> 130 then pan_right

to be > 129
 

tony_g

Senior Member
thanks westy but thats not gonna work sitting here checking with the calculator, the pan left at the moment works fine and im just trying to get the pan right to work in the same fashion.

what it needs to do is if the stick is pushed right only slightly and is giving a read of say 130 then the pause needs to be around the 400 value and to decrease the further the stick value goes more to the right until the stick value hits its max of 255 to which the pause value will be down to 1.

i was hoping it would be quick and straightforward as with the left command but requires more math skill from me i think lol :confused:
 

bluejets

Senior Member
thanks bluejts, i will have a look around.

diesel engine sound, thats an interesting idea to add to a trial truck im slowly putting together amongst other projects, did you use the 08m to generate the sound frequencies for that?
Yes....but have to confess it wasn't my design initially.

It operated somehow on exclusive "or" aggangement but could never find out the principle behind it.

Tried to swing it over into an 08m2 but there were some differences, having to do with memory I think.

Even so, it was quite easy to modify just the same.

I'll try to find some details.

We had a massive flood here 6 months ago and it took out everything, just managng to get back into it and try to find where I left off.
 

hippy

Technical Support
Staff member
You could write your "w2= joy_x*80/21+1" as ...

w2 = joy_x
w2 = w2 * 80 / 21 + 1

As that works for joy_x values of 0 to 125, you simply have to convert w2 from its 131 to 255 value to a similar 0 to 125 value, where 0 is furthest from the centre. Untested but probably ...

w2 = joy_x
w2 = 255 - w2
w2 = w2 * 80 / 21 + 1

You could then compact that down to one line, with the result in 'w3' as per original code ...

w3 = 255 - joy_x * 80 / 21 + 1
 

westaust55

Moderator
thanks westy but thats not gonna work sitting here checking with the calculator, the pan left at the moment works fine and im just trying to get the pan right to work in the same fashion.
w3 = - Joy_x -1 * 80 /21 +1
equates to (256 - joy_x - 1 ) * 80 / 21 + 1
or (256 - 1 - joy_x) * 80/21 +1 ==> (255-joy_x) * 80/21 +1


EDIT:
Actually, you are correct - it will not work as you have a word variable and I was thinking byte variables so cannot be smart and use a negation - darn!

w3 = - Joy_x -1 * 80 /21 +1
equates to (65536 - joy_x - 1 ) * 80 / 21 + 1

where because the result is expected to be a word so it must be as hippy has it
ie 255 - joy_x * 80 / 21 + 1
 
Last edited:

Goeytex

Senior Member
Very Good,

I think that Hippy has posted it before but it doesn't hurt to repeat it again. A general formula for scaling values (mapping?) is ...

New_value = (Data_value - in_minimum) * (out_maximum - out_minimum) / (in_maximum - in_minimum) + out_minimum

Example: I need to scale an ADC so that 0 to 1023 becomes 100 to 800 .... Where we read the ADC into variable W1. The new value will be W2

So ....
New_value = (ADC_Value - 0) * (800 - 100) / (1023 - 0) + 100

Picaxe Code:
Code:
Readadc10 C.2 , W1
W2 = W1  *  700   / 1023  + 100   [COLOR="#008000"]'Scale ADC [/COLOR]
 

bluejets

Senior Member
Tony,
Found the code.
May not apply exactly to what you want here but should give you an idea or two.
Send pm and I'll send over sound code.

Code:
'This program drives an 08M picaxe chip.Input is pin4 and output
'is pin2.These pins measure r/c signal in from throttle channel 
'which varies from 1.0ms(low)1.5ms(middle)2.0ms(high)which in 
'turn give a pulsin signal from 100 to 150 to 200 and give an 
'output which varies from Neutral position to full throttle 
'giving a zero to 3.3volt at full forward and from Neutral to 
'full reverse giving the same zero to 3.3volts. 
'The reason for 3.3volts is the chip this part drives,the 
'"sound generator",is an 08 chip and it has a low resolution
'input for adc.So a zero to 3.3volt signal in the chip that this
'drives gives sound that goes from idle to full revs.
'pin 1 monitors main battery voltage. If this falls below 7.0v
'an alarm signal (1) is sent from pin 0 to the input pin 3 on the 08 sound chip
'which interrupts the diesel sound and gives a "beep---beep" sound. Also flashes
'an "ALARM" LED on the back of the boat.
'Jeff Jorgensen for Connor's Police Launch May 2011.



Main:
	pulsin 4,1,b0					'read rc signal 
	sertxd("RC input value  ",#b0,13,10)	'output to serial monitor
	readadc 1,b3					'read voltage from min drive battery
	if b3<=87 then alarm				'if battery ,7.0v then send alarm (1)
Start:
	if b0=>150 then forw				'forward speed
	if b0<150 then revse				'reverse speed

forw:
	b1=b0-150						'b1 is forward speed percentage
	b1=b1*511/100	 				'b1 set at 66% of max(3.3volts at 5 volt supply)
	pwmout 2,60,b1					'output this voltage and use at input to sound chip
	sertxd("forward is ",#b1,13,10)
	goto main
revse:
	b2=150-b0						'b2 is reverse speed percentage
	b2=b2*511/100				'b2 set at 66% of max(3.3volts at 5volt supply)					
	pwmout 2,60,b2					'output this voltage and use as input to sound chip
	sertxd("reverse is ",#b2,13,10)
	goto main
alarm:
	high 0         					'turns on LED
	pause 250						'pause 250msec
	low 0							'turn off LED
	pause 250 						'pause 250msec  
	goto start   					'go back to "START"
 
Last edited:
Top