'****************************************************************************** '* Name : Controller_003.PBP * '* Author : Christopher Ross * '* Date : 12'03'2012 * '* Version : 3.0 * '* Project : Worlds Most Useless Machine * '* Website : thecircuitnerd.com * '* License : MIT * '* * '* Copyright (c) 2012 Christopher Ross, The Circuit Nerd * '* * '* Permission is hereby granted, free of charge, to any person obtaining a * '* copy of this software and associated documentation files (the * '* "Software"), to deal in the Software without restriction, including * '* without limitation the rights to use, copy, modify, merge, publish, * '* distribute, sublicense, and'or sell copies of the Software, and to * '* permit persons to whom the Software is furnished to do so, subject to * '* the following conditions: * '* * '* The above copyright notice and this permission notice shall be included * '* in all copies or substantial portions of the Software. * '* * '* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * '* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * '* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * '* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * '* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * '* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * '* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * '****************************************************************************** ' --------------------------------- ' Setup MicroControl Config ' --------------------------------- ' Disable the Voltage Comparators CMCON = 7 ' Set Porta.7 to Input and Rest to Outputs TRISA = %10000000 ' --------------------------------- ' Constants ' --------------------------------- LID_CLOSED CON 200 ' PULSEOUT Value for Lid when its closed LID_OPEN CON 100 ' PULSEOUT Value for Lid when its open'up ARM_CLOSED CON 170 ' PULSEOUT Value for Arm when its closed ' inside the box ARM_OPEN CON 40 ' PULSEOUT Value for Arm when its up and pressing the button. LOCK_CLOSED CON 200 ' PULSEOUT Value for Lock when its Closed ' Locked LOCK_OPEN CON 100 ' PULSEOUT Value for Lock when its open ' Un-Locked SWITCH_ON CON 0 ' Input vlaue when the switch is in the On Position. Input is Inverted. SWITCH_OFF CON 1 ' Input vlaue when the switch is in the Off Position. Input is Inverted. LED_ON CON 0 ' Output value to turn the Indicator LED On LED_OFF CON 1 ' Output Value to turn the Indicator LED Off ' --------------------------------- ' Variables ' --------------------------------- servo_lid VAR PORTB.1 ' Refernce to the Output Pin that the Lid Servo Control Line is Connected to servo_arm VAR PORTB.0 ' Refernce to the Output Pin that the Arm Servo Control Line is Connected to servo_lock VAR PORTB.3 ' Refernce to the Output Pin that the Lock Servo Control Line is Connected to opto_6V VAR PORTA.0 ' Refernce to the Output Pin that the Opto Isolator for the 6V is Conenected to. opto_9V VAR PORTA.1 ' Refernce to the Output Pin that the Opto Isolator for the 9V is Connected to. switch VAR PORTA.7 ' Refernce to the Input Pin that the Switch Is connected to. LED VAR PORTB.6 ' Refernce to the Output Pin that the LED is connected to. arm_value VAR word ' Current PULSEOUT Value for the Arm Servo lid_value VAR word ' Current PULSEOUT Value for the Lid Servo lock_value VAR word ' Current PULSEOUT Value for the Lock Servo loop_delay var word ' Counter Variable for loop delays'counts ' --------------------------------- ' Initial Values and Outputs ' --------------------------------- 'Set PULSEOUT Values to zero arm_value = 0 lid_value = 0 lock_value = 0 ' Turn On the 6V and 9V Opto Isolator LOW opto_6V LOW opto_9V ' --------------------------------- ' Open Lock Mode ' Set the lock to open and wait for a certain count to allow the lock to open ' before going to the next mode. ' --------------------------------- OpenLock: loop_delay = 0 arm_value = ARM_CLOSED lid_value = LID_CLOSED lock_value = LOCK_OPEN OpenLockLoop: loop_delay = loop_delay + 1 'After delay then go to next mode IF loop_delay > 14 THEN GOTO OpenLid ENDIF ' If the switch was turned off by the user then go to close lock mode IF switch = SWITCH_OFF THEN GOTO CloseLock ENDIF GOSUB ServoOut GOTO OpenLockloop ' --------------------------------- ' Open Lid Mode ' Set the lid to open and wait for a certain count to allow the lid to open ' before going to the next mode. ' --------------------------------- OpenLid: loop_delay = 0 arm_value = ARM_CLOSED lid_value = LID_OPEN lock_value = LOCK_OPEN OpenLidLoop: loop_delay = loop_delay + 1 'After delay then go to next mode IF loop_delay > 20 THEN GOTO OpenArm ENDIF ' If the switch was turned off by the user then go to close lid mode. IF switch = SWITCH_OFF THEN GOTO CloseLid ENDIF GOSUB ServoOut GOTO OpenLidLoop ' --------------------------------- ' Open Arm Mode ' Set the arm to open and the wait for the switch to turn off before going ' to the next mode. ' --------------------------------- OpenArm: loop_delay = 0 arm_value = ARM_OPEN lid_value = LID_OPEN lock_value = LOCK_OPEN OpenArmLoop: loop_delay = loop_delay + 1 'Once the switch has been turned off go to the next mode IF switch = SWITCH_OFF THEN GOTO CloseArm ENDIF GOSUB ServoOut GOTO OpenArmLoop ' --------------------------------- ' Close Arm Mode ' Set the arm to close and wait for a certain count to allow the arm to retreat ' back into the box. ' --------------------------------- CloseArm: loop_delay = 0 arm_value = ARM_CLOSED lid_value = LID_OPEN lock_value = LOCK_OPEN CloseArmLoop: loop_delay = loop_delay + 1 'After delay then go to next mode IF loop_delay > 40 THEN GOTO CloseLid ENDIF ' If the switch is turned back on by the user then go to open arm mode. IF switch = SWITCH_ON THEN GOTO OpenArm ENDIF GOSUB ServoOut GOTO CloseArmLoop ' --------------------------------- ' Close Lid Mode ' Set the lid to close and wait for a certain count to allow the lid to close ' --------------------------------- CloseLid: loop_delay = 0 arm_value = ARM_CLOSED lid_value = LID_CLOSED lock_value = LOCK_OPEN CloseLidLoop: loop_delay = loop_delay + 1 'After delay then go to next mode IF loop_delay > 20 THEN GOTO CloseLock ENDIF ' If the switch is turned back on by the user then go to open lid mode. IF switch = SWITCH_ON THEN GOTO OpenLid ENDIF GOSUB ServoOut GOTO CloseLidLoop ' --------------------------------- ' Close Lock Mode ' Set the lock to close and wait for a certain count to allow the lock to close ' --------------------------------- CloseLock: loop_delay = 0 arm_value = ARM_CLOSED lid_value = LID_CLOSED lock_value = LOCK_CLOSED CloseLidLock: loop_delay = loop_delay + 1 'After delay then go to next mode IF loop_delay > 20 THEN GOTO Shutdown ENDIF ' If the switch is turned back on by the user then go to open lock mode IF switch = SWITCH_ON THEN GOTO OpenLock ENDIF GOSUB ServoOut GOTO CloseLidLock ' --------------------------------- ' ShutDown ' Turn off both the 6V and 9V power, killing the MicroController also ' --------------------------------- Shutdown: HIGH opto_6V HIGH opto_9V ' This should have killed this programs own power and execution would stop. ShutdownLoop: PAUSE 5 IF switch = SWITCH_ON THEN GOTO OpenLock ENDIF GOTO Shutdownloop ' --------------------------------- ' Subroutine to PULSOUT the servo values. ' Returns to the Line that Called it after execution. ' --------------------------------- ServoOut: PULSOUT servo_arm, arm_value PULSOUT servo_lid, lid_value PULSOUT servo_lock, lock_value ' Most Servos Require 20ms duty cycle, so pause 12ms here and the rest of the PULSE ' OUT roughly equals 20ms PAUSE 12 RETURN