' PicBasic Pro program to play with a servo motor ' The program has two modes of operation: ' in one mode, the servo rotates according to the user's ' twirling of the potentiometer. ' In the second mode, the servo rotates until it trips a wire ' switch and then swings back to do it over again ' In the first mode a red led is lit and its brightness changes as ' the servo rotates. In the second mode a yellow led is used instead. ' a simple switch is used to change modes. ' Created October 4 2005 by Cory Forsyth ' Define ADCIN parameters DEFINE ADC_BITS 10 ' Set number of bits in result DEFINE ADC_CLOCK 3 ' Set clock source (3=rc) DEFINE ADC_SAMPLEUS 15 ' Set sampling time in uS define osc 4 output portd.3 ' the red led output portd.2 ' the yellow led input portb.0 ' the switch input portd.1 ' the trip-switch for the servo ' Set up aliases for all the ports switch var portb.0 yellowled var portd.2 redled var portd.3 servo var portc.3 servoswitch var portd.1 start: ' setup basic variables sensorvalue var word pulsewidth var word pulserange var word dutycycle var byte ' set up constants with the minimum and maximum pulsewidths minPulse CON 50 maxPulse CON 250 ' set up a constant with the time between pulses: refreshPeriod CON 20 ' initialize variables pulsewidth = minPulse pulseRange = maxPulse - minPulse TRISA = %11111111 ' Set PORTA to all input ADCON1 = %10000010 ' Set PORTA analog and right justify result Pause 500 ' Wait .5 second main: ' set the dutycycle for the pwm-ing dutycycle = 2* (pulsewidth - 50) if switch = 1 then pwm redled, dutycycle, 3 adcin 0, sensorValue pulseWidth = minPulse + (pulseRange * (sensorValue/10)) / 100 else pwm yellowled, dutycycle, 3 if servoswitch = 0 then pulsewidth = pulsewidth + 1 else ' The trip-switch has been tripped pulsewidth = minpulse endif endif ' For debugging purposes, send out a variable to be read. serout2 PORTD.7, 16468, [DEC dutycycle, 13, 10] ' print it to serial out, ' with linefeed and carriage return (10, 13) ' rotate the servo low servo pulsout servo, pulsewidth pause refreshPeriod goto main ' do it again