auto select

johnlong

Senior Member
Hi All
Have done the following code to auto select which output to use to maintain
a preset value against a sensor reading
The problem I am having is that I want it to use pins D.7 too D.4 only
The code moves up and down the 4 pins ok until it reaches D.4 or D.7
Has any one any sugestions as how to cap it to use only these 4 pins so if it is already at pin D.4 or D.7 then just return
to main leaving D.7 or D.4 on
Code:
'Attempt to auto select which output pin to use 
dirsD = %11110000
b3=25


outpinsD = %01000000  
pause 3000 
main: 
 do
  	readadc A.1,b2
 	if b2<>b3 then gosub auto         
 	
     	pause 3000  	 
	
 	loop 
 	
 auto:
 if  b2<b3 then
 
 
 outpinsD = outpinsD>>1 
		
 
  elseif b2>b3 then  
 	outpinsD=outpinsD<<1
		
	endif
	return
rgards john
 

Buzby

Senior Member
Code:
[color=Green]'Attempt to auto select which output pin to use [/color]
[color=Purple]dirsD [/color][color=DarkCyan]= [/color][color=Navy]%11110000[/color]
[color=Purple]b3[/color][color=DarkCyan]=[/color][color=Navy]25[/color]


[color=Purple]outpinsD [/color][color=DarkCyan]= [/color][color=Navy]%01000000  [/color]
[color=Blue]pause [/color][color=Navy]3000 [/color]
[color=Black]main: 
 [/color][color=Blue]do
      readadc A.1[/color][color=Black],[/color][color=Purple]b2
      [/color][color=Blue]if [/color][color=Purple]b2[/color][color=DarkCyan]<>[/color][color=Purple]b3 [/color][color=Blue]then gosub [/color][color=Black]auto         
      [/color][color=Purple]b3 [/color][color=DarkCyan]= [/color][color=Purple]b2
      [/color][color=Blue]pause [/color][color=Navy]3000         
      
      [/color][color=Blue]loop 
      
 [/color][color=Black]auto:
 [/color][color=Blue]if  [/color][color=Purple]b2[/color][color=DarkCyan]<[/color][color=Purple]b3 [/color][color=Blue]then
      if [/color][color=Purple]pinD.4 [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]then
            [/color][color=Purple]outpinsD [/color][color=DarkCyan]= [/color][color=Purple]outpinsD[/color][color=DarkCyan]>>[/color][color=Navy]1 
      [/color][color=Blue]endif 
            
 
elseif [/color][color=Purple]b2[/color][color=DarkCyan]>[/color][color=Purple]b3 [/color][color=Blue]then
       if [/color][color=Purple]pinD.7 [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]then
            [/color][color=Purple]outpinsD[/color][color=DarkCyan]=[/color][color=Purple]outpinsD[/color][color=DarkCyan]<<[/color][color=Navy]1
       [/color][color=Blue]endif
endif
      
return[/color]
 

johnlong

Senior Member
Thanks Buzby
Thats steared me in the right direction like my original when pin D.7 was reached it would fall off the edge and not contained
so added another sub to split up the disssision after reading the adc and worked the outpinsD around your manipulation for pinD
Code:
dirsD = %11110000
b3=25


outpinsD = %01000000  
pause 3000 
main: 
 do
      readadc A.1,b2
      if b2<b3 then gosub auto 
      if b2>b3 then gosub auto1        
      pause 3000       
      loop 
      
 auto:
 
           if  outpinsD=16 and b2<b3 then
            return 
       elseif  b2<b3  then
      
            outpinsD = outpinsD>>1
            endif
            return
            
 auto1:
if outpinsD=128 and b2>b3 then
	return
      elseif b2>b3  then
            outpinsD=outpinsD<<1
       endif
      return
regards john
 

Buzby

Senior Member
Hi John,

I'd stick with checking D.4 and D.7, not outpinsD.

outpinsD will have other bits set if you use any of the D.0 to D.3 pins as outputs.

If fact, using outpinsD at all is a bit flakey, as if you do use more outputs on D, the << and >> shifts will shift those bits as well.

Better use something like :
Code:
[color=Black]shiftUp: [/color][color=Green]' Routine called to shift Up
      [/color][color=Blue]if [/color][color=Purple]pinD.7 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then [/color][color=Black]: [/color][color=Blue]return [/color][color=Black]: [/color][color=Blue]endif [/color][color=Green]' Quit if at max already
      [/color][color=Purple]pinD.7 [/color][color=DarkCyan]= [/color][color=Purple]pinD.6
      pinD.6 [/color][color=DarkCyan]= [/color][color=Purple]pinD.5
      pinD.5 [/color][color=DarkCyan]= [/color][color=Purple]pinD.4
      pinD.4 [/color][color=DarkCyan]= [/color][color=Navy]0[/color]
[color=Blue]return[/color]

[color=Black]shiftDn: [/color][color=Green]' Routine called to shift Down
      [/color][color=Blue]if [/color][color=Purple]pinD.4 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then [/color][color=Black]: [/color][color=Blue]return [/color][color=Black]: [/color][color=Blue]endif [/color][color=Green]' Quit if at min already
      [/color][color=Purple]pinD.4 [/color][color=DarkCyan]= [/color][color=Purple]pinD.5
      pinD.5 [/color][color=DarkCyan]= [/color][color=Purple]pinD.6
      pinD.6 [/color][color=DarkCyan]= [/color][color=Purple]pinD.7
      pinD.7 [/color][color=DarkCyan]= [/color][color=Navy]0[/color]
[color=Blue]return[/color]
Cheers,

Buzby
 
Last edited:

hippy

Technical Support
Staff member
I would not directly access the pins, but keep the bit settings in a separate variable, say b0 here ...

Init : b0 = %01000000
Shift up : b0 = b0 << 1 Max %10000000
Shift down : b0 = b0 >> 1 Min %00010000

To output, combine that with any other variable which holds other output bits ...

outpinsD = b0 or b11

Or transfer the bits one at a time which allows HIGH D.0 etc elsewhere ...

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

It is possible to have all bits in one variable but then the shifting code gets more complicated.
 

johnlong

Senior Member
Hi
After 2 days of getting know were with the shifting of the D pins between D7 and D4
I figured well copy it out of PE5 into PE6 and hey presto it works

Code:
#picaxe 40x2
         
dirsD = %11111111
 outpinD.4 = bit4
 outpinD.5 = bit5
 outpinD.6 = bit6 
 outpinD.7 = bit7

b3=25

 outpinD.4=1 
high D.0,D.2

pause 3000   
main: 
  do
      readadc A.0,b2
      	
      if b2<b3 then gosub shift_down      
      if b2>b3 then gosub shift_up  
                
       pause 3000       
        loop 
      
 shift_down:
 
       if outpinD.4=1   and b2<b3 then:return:endif   
           
      if outpinD.7 = 1  then:outpinD.7 = 0:outpinD.6 = 1  
     elseif outpinD.6 = 1 then:outpinD.6 = 0:outpinD.5 = 1
     elseif outpinD.5 = 1 then: outpinD.5 = 0:outpinD.4 = 1
endif
return
shift_up:
if outpinD.7 = 1 and b2>b3 then:return:endif
	  if outpinD.4 = 1  then:outpinD.4 = 0:outpinD.5 = 1  
     elseif outpinD.5 = 1 then:outpinD.5 = 0:outpinD.6 = 1
     elseif outpinD.6 = 1 then: outpinD.6 = 0:outpinD.7 = 1
endif
return
I know it seems abit odd but I just prefer the simplicity of the simulator in PE5 (see the chip alter values all in one pannel)
so a big thanks just got to slot it in to the main program
regards
john
 

Buzby

Senior Member
I know it seems a bit odd but I just prefer the simplicity of the simulator in PE5 ...
Not odd at all. I also prefer the PE5 simulator for certain projects. The PE6 simulator is packed full of so many bells and whistles that it can't keep up with itself !.

Horses for courses :)
 
Top