'--------Title--------

' File......robot_photo1.pbp
' Started....11/28/07
' Microcontroller used:  Microchip Technology 16F88
'                        microchip.com
' PicBasic Pro Code: micro-Engineering Labs, Inc.
'                    melabs.com 

'--------Program Desciption--------

' Displays readings from a photoresistor
' onto LCD display.  If the display reading is
' greater or equal to 150, the car will travel
' forward; if the display reading is less than
' 150, the car will turn.

'------------Comments--------------

' The resistance of the photoresistor that was used,
' (Jameco # 120299), varies from 300K in darkness
' to 8K ohms in bright light.  A 0.1 uF capacitor
' was in series with the photoresistor.

'-----New PicBasic Pro Commands----

'------------Connections-----------

'		16F88 Pin			Wiring
'		---------			---------- 
'  		RA0					LCD pin 11(DB4)
'       RA1                 LCD pin 12(DB5)
'       RA2                 LCD pin 13(DB6)
'       RA3                 LCD pin 14(DB7)
'       RA4                 LCD Register Select(RS)
'       RB3                 LCD Enable(E)
'       RB1                 Photoresistor Input
'       RB1                 To base NPN controlling Motor 1
'       RB2                 To base NPN controlling Motor 2
'       See schematic for the usual connections

'---------LCD Connections---------

'	     LCD Pin	   	      Wiring
'		---------		 	---------- 
'          1               Ground(Vss)
'          2               + 5v(Vdd)
'          3               Center of 20K Pot(Contrast)
'          4               RA4(Register Select,RS)
'          5               Ground(Read/Write,R/W)
'          6               RB3(Enable)
'          7               No Connection(DB0) 
'          8               No Connection(DB1)     
'          9               No Connection(DB2)     
'         10               No Connection(DB3)     
'         11               RA0(DB4)    
'         12               RA1(DB5)
'         13               RA2(DB6)
'         14               RA3(DB7)

'--------Revision History--------

'---------Constants/Defines-------

'---------Variables---------

    p0  var byte            'Byte for photoresistor reading
    
'---------Initialization--------

    ANSEL = 0               'Configure all pins to digital
                            'operation since not using ADC
                            '(Analog to Digital Converter)
                                
    OSCCON = $60	        'Sets the internal oscillator in the
                            '16F88 to 4 MHz   	  
                                
    PORTB = %00000000       'Sets up pins B0-B7 of PORTB at LOW
    
    TRISB = %00000001       'Sets up pin RB0 of PORTB as an input
                            'and the remaining PORTB pins as outputs
       
'--------Main Code--------

    pause 1000              '1 second pause to allow LCD to setup
    
start:                      'Start label for loop

' Read photoresistor and display value:

    pot 0,255,p0            'POT command, photoresistor reading
                            'sent to RB0, scale set for 255, p0
                            'assigned reading of photoresistor.
                            'p0 value may vary from a 
                            'minimum of 0 to a maximum of 255.
                            'In order to obtain the maximum reading
                            'of 255, you will probably have to
                            'experiment with the value of the
                            'capacitor in the robot_photo circuit.
                            'See details for POT command in the 
                            'PICBASIC PRO Compiler book.
                              
    lcdout $fe,1,"Photocell = ",#p0
                            'Clears LCD screen, displays 
                            '"Photocell = " and value of p0
                                     
' Choose to travel straight or turn:    
        
    if p0 >= 150 then        'If photoresistor value, p0, is
                             'greater than or equal to 150, the
    gosub straight           'program jumps to the 
                             'subroutine straight.
                             'If p0 >= 150 is false, i.e. p0 < 150,
                             'the program advances to the ELSE 
                             'program statement.
                             
    else 
    
    gosub turn               'Program jumps to subroutine turn
    
    endif
    
    goto start
    
    end
       
straight:                    'Subroutine straight

    high 1 : high 2          'Pins RB1 and RB2 are set at HIGH,
                             'turning on both NPN transistor switches,
                             'making the drive motors advance forward.   
    
    pause 20                 'Pause 20 mS
       
    return               
    
turn:                        'Subroutine turn

    high 1 : low 2           'Pin RB1 remains HIGH while pin RB2
                             'is set LOW,turning on only one NPN
                             'transistor switch, making the car turn.
    
    pause 20                 'Pause 20 mS
    
    return
    
    
    

        

