' programme AROBOT NEW commandé par recepteur 27Mhz
' 14-11-2002
' 


#include "start.bas"

        BYTE n,nt
        BYTE T
        INT comp,comp2,mb
	DDRD  = 0			' PD5=PD4=PD3=PD2=input
	SCONF = 0x4C			' B,C = sorties
	lcdinit()
      
        
        PACTL.7=0
        PACTL.5=1
        PACTL.4=0
        PACTL.6=1
        TMSK2.1=0
        TMSK2.4=1
        TMSK2.6=0
        TFLG2.6=0          		

        
        n=180  
        nt=180
        PORTB.2 = 0
	cli			' autoriser les inter
        


	print " arobot 2 "

        do
          PACNT=0
          wai
          comp=PACNT
          
     '    print "comp=",comp      'comp pour gauche / droite
      '   tempo(50)
       
      '    print "comp2=",comp2      'comp pour haut / bas
      '   tempo(10)
          

     
        if comp2<105 and comp>40 and comp<55 then
          gosub AVANCER
        end if

        
        if comp2>160 and comp>40 and comp<55 then
          gosub RECULER
        end if

        
        if comp2<105 and comp<40 then
          gosub DROITE
        end if


        if comp2>160 and comp<40 then
         ' gosub GAUCHE
        end if

        if comp2>130 and comp2<150 then
          gosub STOP
        end if


        loop

        
     

         

 
           
' sous programme


STOP:
        PORTB.2 = 0
        tempo(40)
        n=180
'        wai
        mb=0
        RETURN 

RECULER:  
	if mb = 0 or mb=1 then 
	PORTB.2=0
	tempo(40)
	
        PORTB.0=0
        PORTB.1=0
        tempo(5)
        PORTB.2=1
	mb=2
	end if
        RETURN
       
AVANCER:
	if mb = 0 or mb = 2 then
	PORTB.2=0
	tempo(40)
        PORTB.0=1
        PORTB.1=1
        tempo(5)
        PORTB.2=1
	mb=1
        end if

        RETURN

DROITE:
       n=130
       wai

       RETURN 
        
TOUTDROIT:        
       n=180
       wai
       
       RETURN

GAUCHE: 
       n=230
       wai 
  
       RETURN

TOURELLEGAUCHE:
               nt=nt+2
               if nt>230 THEN nt=230
               
               RETURN

TOURELLEDROITE:
               nt=nt-2    
               if nt<130 THEN nt=130

               RETURN

TOURELLECENTRE:
               nt=160
       
               RETURN   


function tempo(cnt)
	int i, k
	for cnt=cnt to 0 step -1
		for i=0 to 1000
		next
	next
	end function

        interrupt function rtiint at $FFDA
	comp2=0
        
        do
          comp2=comp2+1
        loop until PORTA.0=0

        

        
    	PORTA.4 = 1
        FOR T = 1 to n
        NEXT T
        PORTA.4 = 0
        
         PORTA.5 = 1
        FOR T = 1 to nt
        NEXT T
        PORTA.5 = 0

	TFLG2.6 = 1		' autoriser ints a nouveau
	end function







#include "lcd.bas"
