OK, here is the final code, I hope, as I'm installing the controller for my friend sometime this week. I've incorporated the code suggestion from Al in the delay loop.
Again, thanks to you all for the helpful suggestions, etc.
Sincere regards,
Mitch
Code:
'****************************************************************
'* Name : Train_Auto-Reverser.pbp *
'* Author : *
'* Notice : Copyright (c) 2010 *
'* : All Rights Reserved *
'* Date : 10/27/2010 *
'* Version : 1.0 *
'* Notes : Model train auto-reversing circuit using *
'* : a PIC12F675 and reed switch sensors. *
'****************************************************************
' ---------Configuration--------
' Accepted PICBasic Pro v2.6a default configuration for PIC12F675
' __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF
' Also enabled PWRT and BOD during programming of PIC.
' ---------Defines/Constants-------
DEFINE OSCCAL_1K 1 ' Calibrate internal oscillator
DEFINE ADC_BITS 10 ' Set number of bits in A/D result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
RY1 CON 4 ' Relay 1 (Power) to GPIO.4
RY2 CON 5 ' Relay 2 (Voltage reversing) to GPIO.5
' ---------Variables---------
SW1 VAR GPIO.0 ' Reed switch 1 to GPIO.0
SW2 VAR GPIO.1 ' Reed switch 2 to GPIO.1
adval VAR WORD ' Store result of each A/D conversion
delay VAR WORD ' Store result of math conversion
A0 VAR BYTE ' Index for measurement loop
' ---------Initialization--------
ANSEL = %00000100 ' Set AN2 analog, all others digital
CMCON = 7 ' Turn off analog comparators
ADCON0.7 = 1 ' Right justify A/D conversion output
' ---------Main Code---------
PAUSE 2000 ' Let everything stabalize for 2 seconds.
LOW RY2 ' Make sure RY2 is off for forward movement.
mainloop:
HIGH RY1 ' Turn on Power relay to start forward movement.
WHILE SW1 = 1 ' Keep RY1 active until reed switch SW1
HIGH RY1 ' is closed.
WEND
LOW RY1 ' Removes power and stops movement.
GOSUB delayloop ' Keep stopped for x number of seconds.(0-61)
HIGH RY2 ' Prepare RY2 for reverse movement.
PAUSE 500 ' Let RY2 settle before activate RY1.
HIGH RY1 ' Turn on power relay to start reverse movement.
WHILE SW2 = 1 ' Keep RY1 active until reed switch SW2
HIGH RY1 ' is closed.
WEND
LOW RY1 ' Removes power and stops movement.
LOW RY2 ' Prepare RY2 for next forward movement.
GOSUB delayloop ' Keep stopped for x number of seconds.(0-61)
GOTO mainloop ' Keep repeating until user terminated.
END
delayloop:
Delay = 0 ' Zero out variable
FOR A0 = 1 to 10 ' Take 10 readings
ADCIN 2, adval ' Read A/D channel 2 into adval variable.
Delay = Delay + adval
NEXT A0
delay = delay * 6 ' Creates values for PAUSE command that
PAUSE delay ' allow delays from approx. 0 to 61 seconds.
RETURN
Bookmarks