'*  Name    : HoodV27.PBP                                       *
'*  Author  : Bob Found                                         *
'*  Notice  : Copyright (c) 2012 Found Engineering              *
'*          : All Rights Reserved                               *
'*  Date    : 5Mar2012                                          *
'*  Target  :16F628A
'*  Version : 2.7                                               *
'*  Notes   : Logic to open and close hood on car using PIC MCU,
'*            Spahl remote controller, and 4 reversing relays                                           
'*  Version History:  V1.0 Start 
'*  V2.0 Eliminated all the switches except for lock, using Potentiometer
'*       feedback and A/D conversion.
'*  V2.1 Actual resistance values for Firgelli Actuators inserted
'*       into code
'*  V2.2 Matched actual board pinouts to ports chosen here
'*  V2.3 Used ADC bit values, not resistance or voltage, for position variables
'*  V2.5 Used code from HoodTest20.  Changed word to byte variables
'*  This version works for the basic operation (no fail safe code).
'*  V2.6 Added beeper and lock logic. Added comments .
'*  Note: local jog switches bypass this logic and attach direct to relays.
'*  Refer to HoodRelays2012v23.sch and HoodRelays2012v25.pcb.
'*  V2.7 Changed comment delimiter to apostrophe, semi-colon prevents
'*  debugger (ICD) from working under certain conditions. Also changed
'*  horn logic when opening hoods.  Created subroutine for horn on close.                                                 
define	LOADER_USED	1
ANSEL=%00000011     'set first two bits for AN0 and AN1, others are digital i/o
ANSELH= %00000000   'Port B set to digital i/o
TrisA = %11111111   'port A all inputs
TrisB = %00000000   'port B all outputs

DriveMin    var byte 'closed =0
PassMax     var byte '255 counts in ADC=4.51 Vdc 
DriveHalf   var byte 'Half open/closed=127 
PassHalf    var byte '127

DrivePos    var byte  'position of driver actuator, pot value converted to counts
Passpos     var byte  'position of pass actuator
x           var byte  'general loop count variable

'These are actual ports for 16F628
DrivePot    var porta.0   'input from driver side actuator potentiometer
PassPot     var porta.1   'same, for pass side
RemOpen     var porta.2   'remote switch signal to open hoods
RemClose    var porta.3   'remote switch signal to close hoods
Lock        var porta.4  'hood lock (input)
'***** LCD def.  don't need in installed version*****
define  LCD_DREG        PORTD
define  LCD_DBIT        4
define  LCD_RSREG       PORTE
define  LCD_RSBIT       0
define  LCD_EREG        PORTE
define  LCD_EBIT        1
define  ADC_BITS        8
define  adc_sampleus    50

' These have to be modified for 16F628
DriveOpen   var portb.0 'Drives relay to open driver side hood
DriveClose  var portb.1 'relay to close driver side hood
PassOpen    var portb.2 'ditto
PassClose   var portb.3 'ditto
Beep        var portb.4 'horn alarm

'*****************Constant Values******************
DriveHalf = 127  
PassHalf = 127
PassMax = 230
DriveMin = 0 

'****************Start Program********************
Start: DriveOpen=0    'initialize all relays and buzzer to zero (off)
       Beep = 0
Main:   gosub ADC   'find out position of actuators
        lcdout $fe, 1,	"Waiting for input"
        lcdout $fe, $c0, "Drive=", #DrivePos, "   Pass=",#passpos
        pause 100    
        if RemOpen=0 then gosub Open  'if remote switch to open is activated, gosub
        if RemClose=0 then gosub Close'if remote switch to close is activated, gosub
        goto Main
Open:  if Lock=1 then goto Nolock 'hood is locked when=0, sound the buzzer for 1 second 
       pause 500
       pause 500
       goto Main     'hood is locked, go back and check for remote inputs
Nolock: DriveOpen=1             'Open driver side first  
       gosub ADC               'get position of actuators 
       if DrivePos>DriveHalf then  'When drive actuator is 1/2 open, start Pass actuator
       loop until Passpos>PassMax 'keep operating both actuators until passenger is 
       return                     'at max of 232 counts (or whatever value required)
Close: PassClose=1            'close passenger side first  
       do until Passpos<=PassHalf  'When Pass actuator is 1/2 close, start Drive actuator
       gosub ADC
       loop                   'keep checking position.  Is it 1/2 closed?
       DriveClose=1           ' start drive actuator
       do until  DrivePos=DriveMin   'keep running until drive actuator is fully closed
       gosub ADC              'check position
       gosub beep3
Beep3  for x=0 to 2
       Beep=1                 'hood is closed, please lock. buzzer sounds 3 times
       pause 200
       pause 200
       next x
ADC:    ADCON0.1 = 1       'enable the A/D converter
        adcin 0, DrivePos  'get value for driver actuator, max is 217 counts or 4.26 VDC
        adcin 1, Passpos   'get value for pass actuator, max is 232 counts or 4.51 VDC
        lcdout $fe, 1      'clear display
        lcdout $fe, $c0, "Drive=", #DrivePos, "   Pass=",#passpos'display values