Hi again,

I'm stumped on what's happening when I run the following code, and having gone over it time and time again I might be overlooking the obvious.

I have a motor connected to the H-bridge module that arrived yesterday - making pins 1 high and 2 low makes the motor run one way and reversing which pin is high and low makes the motor run the opposite way. I'm using a separate 9v supply for the motor.

When first turned on, the threshold set means that the code is set to night mode. I then shine a light on the LDR and the motor runs until I connect GPIO.5 to 5v to simulate the door closing the micro-switch at the end of its travel. So far so good. However, if I remove the connection between GPIO.5 and +5v to simulate the door in the close state GPIO.5 remains latched high. Grounding the pin momentarily has no affect, and disconnecting the H-bridge has no affect. Only way to clear this is to turn off and on the EasyPic5 board.

Code:
ASM
 __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF 
endasm

DEFINE ADC_BITS 10              ' ADC resolution
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50
number VAR word                 ' Variable to store ADC result
day var byte                    ' Variable to indicate day or night

CMCON0=7
CCP1CON=0
ANSEL = %00000001               ' GPIO.0 A/D in, rest digital
ADCON0.7 = 1                    ' Right justify for 10-bit
GPIO = %00000000                ' Initialize outputs
TRISIO = %101011                ' GPIO 0,1,3,5 = input, 2 & 4 outputs


Motor1          var gpio.2      ' H-Bridge pin 1
Motor2          var gpio.4      ' H-Bridge pin 2
SW1             VAR gpio.5      ' Door open limit switch


low sw1                         ' Ensure limit switch is 0 (door is closed)
low Motor1                      ' Turn off H-Bridge pin 1
low Motor2                      ' Turn off H-Bridge pin 2
day=0                           ' Initialise unit to check if day time

main:
if sw1=0 then day = 0           ' If limit switch is open, and mode is night then
adcin 0,number                  ' Read AN0
if day=0 then                   ' Day 0 = night, Day 1 = Day
  If number >660 then           ' If ACD result is above a threshold
  low SW1                       ' Set state of limit switch to off
  high Motor1                   ' Turn on H-Bridge pin 1
  Low Motor2                    ' Turn off H-Bridge pin 2
endif
endif
if SW1 = 1 then                 ' Door closes limit switch and applies 5v to GPIO.5
  low Motor1                    ' Turn off H-Bridge pin 1 }  Stop motor
  low Motor2                    ' Turn off H-Bridge pin 2 }
  day = 1                       ' Set variable to day to indicate the door is open
endif

goto main
The logic behind this project is that when the light level increases at dawn past the threshold it drives the motor which by use of a threaded rod slides open the door on the coop. When the door closes a micro-switch at the end of the travel the power to the motor is turned off and the unit placed in day mode. At the end of the day a switch will be pressed directly applying 5v to pin2 of the H-Bridge thus reversing the motor, opening the door and opening the switch, changing its state from 1 to 0.

Any ideas ??

Malcolm