Thanks Louie and Al.
Louie: Yes, I do need to use parenthesis. That was an oversight. I hope you are correct about the division remainders. That is one of my concerns.
Al: It's not critical, either way, to my application but what you suggest makes good sense. I will probably test it each way.
I've tentatively completed the code for my project if either of you, or anyone else, would care to critique it for me. I could really use your more experienced opinions. I hope to breadboard it tomorrow.
Thanks again,
Mitch
Code:'**************************************************************** '* Name : Train_Autoreverser.pbp * '* Author : * '* Notice : Copyright (c) 2010 * '* : All Rights Reserved * '* Date : 10/27/2010 * '* Version : 1.0 * '* Notes : Model train autoreversing circuit using * '* : a PIC12F675 and reed switch sensors. * '**************************************************************** ' ---------Configuration-------- ' PICBasic Pro v2.6a default configuration for PIC12F675 ' __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF ' ---------Constants/Defines------- DEFINE OSCCAL_1K 1 ' Calibrate internal oscillator DEFINE ADC_BITS 8 ' Set number of bits in result DEFINE ADC_CLOCK 3 ' Set clock source (3=rc) DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS SW1 CON 0 ' Reed switch 1 to GPIO.0 SW2 CON 1 ' Reed switch 2 to GPIO.1 RY1 CON 4 ' Relay 1 (Power) to GPIO.4 RY2 CON 5 ' Relay 2 (Voltage reversing) to GPIO.5 ' ---------Variables--------- adval VAR BYTE ' Store result of each A/D conversion. delay VAR BYTE ' Store result of math conversion to seconds. ' ---------Initialization-------- ANSEL = %00000100 ' Set AN2 analog, all others digital. CMCON = 7 ' Turn off analog comparators. ' ---------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. IF SW1 = 0 THEN ' Keep moving in the forward direction until LOW RY1 ' SW1 reed switch is closed. Then turn off ELSE ' RY1 power relay to stop movement. HIGH RY1 ENDIF GOTO delayloop ' Keep stopped for x number of seconds.(1-63) HIGH RY2 ' Turn on reversing relay for reverse movement. HIGH RY1 ' Turn on power relay to start reverse movement. IF SW2 = 0 THEN ' Keep moving in the reverse direction until LOW RY1 ' SW2 reed switch is closed. Then turn off ELSE ' RY1 power relay to stop movement. HIGH RY1 ENDIF LOW RY2 ' Get RY2 ready for next forward movement. GOTO delayloop ' Keep stopped for x number of seconds.(1-63) GOTO mainloop ' Keep repeating until user terminated. END delayloop: ADCIN 2, adval ' Read A/D channel 2 into adval variable. IF adval <4 THEN ' Make 1 second the shortest time delay. adval = 4 delay = (adval /4) * 1000 ' Divide adval value (1-255) by 4 and multiply ENDIF ' by 1000 to get millisecond value for PAUSE PAUSE delay ' command. Place this value in delay variable.




Bookmarks