up or down

johnlong

Senior Member
Hi All
Just having a play to see if possible to select a pin on a variable changeable setting in
relation to the results of a sensor. Without pre setting the pins to ie if X<1 then high else.........ect
In effect finding its own balance
ie set point 30 reading 31 make next pin high (all other low), no change in reading make next pin high
example start B.4 (high)->B.5 (high)-->time elapse-->no change-->B.6(high) ect
lower than set as above in reverse

have come up with the following creating a mask for b1 to look at B.4 to B.7 works fine up and dwn
whilst there is no highs on the lower pins how can I tell it to discount the lower pins values
Code:
b12=20 'set value required
b10=20 ' sensor value
high b.6 'set a point to start from
'high b.2
	do 'if no difference loop until a change occurs
	' finds the pins setting to achive balance
	let b1=pinsB&%11110000
	if pinB.0=1 then:dec b10 'for simulation to replicate sensor reading only
	elseif pinB.1=1 then inc b10 'as above
	endif
	if b10>b12 then 
		gosub up
	elseif b10<b12 then
		gosub dwn
		endif
		loop 
up: 
	let b1=pinsB&%11110000*2 max 128 ' make a mask and limit it so the bits 
		select case pinsB           'do not fall off the edge
	case 16 high B.5 low B.4
	case 32 high B.6 low B.5
	case 64 high B.7 low B.6
	case 128 high B.7 low B.6,B.5,B.4
		end select
	return
dwn:
	let b1=pinsB&%11110000/2 min 16
		select case pinsB
	case 128 low B.7 high B.6
	case 64 low B.6 high B.5
	case 32 low B.5 high B.4
	case 16 high B.4 low B.7,B.6,B.5
		end select
	return
hope the explination makes sense
regards john
 

hippy

Technical Support
Staff member
Not quite sure exactly what it is you want to achieve but it seems you maybe wanting to update some pins while not affecting others.

The best way to do that is to set bits within say 'b0' as if they were pins, and set other pins as bits in say 'b1', then combine the two ...

Code:
b0 = %10100000
b1 =     %1100
pinsB = b0 | b1 ; Output will be %10101100
Code:
For b1 = %0000 To %1111
  For b0 = %0000 To %1111
    pinsB = b1 * 16 | b0
    Pause 500
  Next
Next
 

westaust55

Moderator
Does this do the sort of thing you are seeking:

Code:
  DO
    FOR b0 = 0 TO 2
	GOSUB display
    NEXT b0
    FOR b0 = 3 TO 1 STEP -1
	GOSUB display
    NEXT b0
  LOOP

Display:
	b1 = b0 // 4 + 4
	LOW B.4, B.5, B.6, B.7
	HIGH b1
	RETURN
 

johnlong

Senior Member
Thanks
As always a point in the right direction
distance learning is so hard at times lol the following does what I wanted turn a pin on if condition met no further action
elseif not turn on next pin
Code:
	symbol bOptr=200
	b10=20 'sensor ie readadc A.0,b10
	b12=20 'required value
	high b.6
	high b.0,b.1,b.2,b.3 ' to test if highs of lower pins effect upper pins dont (hehe)
	b0=6 'start up with pin B.6 high
	poke bOptr,b0 'store b0 value
	do
	if pinD.7=1 then:dec b10 'for simulation to replicate sensor
	elseif pinD.6=1 then inc b10 'as above
	endif
	if b10>b12 then
		peek bOptr,b0 'free up b0
		if b0=7 then:b0=6:endif 'Set the max for b0 to pin  B.7 full power
		b0=b0+1		
		gosub up			
	elseif b10<b12  then
		peek bOptr,b0
		if b0=4 then: b0=5:endif 'set the min for b0 to pin B.4 low power
		b0=b0-1		
	GOSUB dwn   		
	endif	
	loop
Dwn:	
	b1 = b0 // 4 + 4 
	LOW B.4, B.5, B.6, B.7	
	HIGH b1	
	poke bOptr,b0 ' store b0
	RETURN
up:	
	b1 = b0 // 4 + 4 
	LOW B.4, B.5, B.6, B.7	
	HIGH b1	
	poke bOptr,b0
	RETURN
Westaust if you have the time can you break down b1=b0//4+4 so I can understand whats happening there
should drop into the main code nicely fingers crossed
regards
john
 
Last edited:

hippy

Technical Support
Staff member
I'd be tempted to do something like this ...

Code:
dirsB = %1111????

Select Case b10
  Case > b12 : b0 = b0 * 2 Min %00010000 Max %10000000
  Case < b12 : b0 = b0 / 2 Min %00010000
End Select

pinB.7 = bit7
pinB.6 = bit6
pinB.5 = bit5
pinB.4 = bit4
 

johnlong

Senior Member
Hi Hippy
Tryed my damnest but could not get it to make B.4 to B.7 high or low
I see it altering the relivent values but unable to convert that into a high or low
will commit seplica forthwith
regards
john
 

westaust55

Moderator
Thanks


Westaust if you have the time can you break down b1=b0//4+4 so I can understand whats happening there
should drop into the main code nicely fingers crossed
regards
john
Hi John,

Firstly, the PICAXE I/O definitions, B.0, B.1, . . . B.7, C.0, C.1, .. C.7, etc
are all in fact pre-defined constants where B.0 = 0, B.1 = 1, B.7 =7, C.0 =8, C.7 = 15, etc

So to control only B.4 to B.7 you need a value between 4 and 7 inclusive.

Now to that formula:

The //4 part determines the modulus (remainder) after a division by 4. So the valid results are 0, 1, 2 and 3.
but we need values of 4 to 7 so we include the + 4 part.

When you have a variable that will have a larger range you should scale it so that the result is in the range 0 to 3

Lets say you use READADC A.0, b5 and have a value from 0 to 255 in byte variable b5.
for a simple linear scaling you can use the calculation
b1 = b5 / 64
because of the integer math even at 255 the result 255/64 (=3.98) will give us 3
If scaled correctly, the //4 part is in reality redundant but just in case we end up with a larger value than 3 that can affect an output outside the pins we wish to control the //4 is a safety net of sorts.
 
Top