'rob@faludi.com define OSC 20 ' define ADCIN parameters for analog sensors (not used in the current version) DEFINE ADC_BITS 10 ' Set number of bits in result DEFINE ADC_CLOCK 3 ' Set clock source (3=rc) DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS ' define hardware serial settings for MIDI DEFINE HSER_RCSTA 90h ' enable the receive register DEFINE HSER_TXSTA 20h ' enable the transmit register DEFINE HSER_BAUD 31250 ' set the baud rate 'set a constant with the baudmode 9600-8-n-1: inv9600 con 16468 'inverted for sending to a computer's serial port non9600 con 84 'non-inverted, for sending to the XPort TRISA = %11111111 ' Set PORTA to all input ADCON1 = %10000010 ' Set PORTA analog and right justify result TRISD = %00000000 blinky var portb.0 txPort var portc.4 rxPort var portc.5 txPort2 var portd.0 rxPort2 var portd.1 i var byte inputData var byte[3] outputData var byte[4] minOutputValue var word maxOutputValue var word outputRange var word minOutputValue = 0 maxOutputValue = 127 outputRange = maxOutputValue - minOutputValue rgbAverage var byte whiteMin var word whiteRange var word percentToScaleHIGH var word outputValHIGH var word trimVar var word[4] pauseValue var byte controlByte var byte controlbyte = 176 whiteMin = 0 ' rgb average at which white light begins to come on whiteRange = maxOutputValue - whiteMin for i = 0 to 3 pausevalue = 2000 gosub blink next main: 'serout2 txPort, noN9600, [65] gosub readpots for i = 0 to 2 INPUTDATA[i] = 0 'reset input data back to zero outputdata[i] = 0 'reset output data back to zero next serin2 rxPort, Non9600, [WAIT (255,1,128), inputData[0],inputdata[1],inputdata[2]] 'and set input data to the new value gosub calcRGBOutput gosub calcWhiteOutput serout2 txport2, inv9600, ["rIn:",dec inputData[0]," gIn:",dec inputData[1]," bIn:",dec inputData[2]," R:",dec outPutData[0]," G:",dec outPutData[1]," B:",dec outPutData[2]," W:",dec outputdata[3],13,10] for i = 0 to 3 HSEROUT [controlByte,i,outPutData[i]] ' MIDI output next goto main blink: high blinky pause pauseValue low blinky pause pauseValue return readPots: for i = 0 to 3 adcin i, trimVar[i] next return calcRGBOutput: for i = 0 to 2 inputdata[i] = inputdata[i] / 1 ' used to be /2 to translate from 8 bit to 7 bit outputData[i] = ( (inputData[i] * (trimVar[i]/10) ) / 100 ) ' scale by trim Pots outputdata[i] = outputdata[i] * 3 ' multiples of 100% for adjusting scale if outputdata[i] > 127 then outputdata[i] = 127 ' 7 bit ceiling for output data endif next return calcWhiteOutput: rgbAverage = (outputdata[0]+outputdata[1]+outputdata[2]) / 3 ' average of rgb whiteMin = trimvar[3] / 8 if rgbAverage < whiteMin then outputdata[3] = 0 ' hold the white lights low until it's time to turn 'em on else percentToScaleHIGH = ((rgbAverage - whiteMin) * 100) / whiteRange ' percent of white light outputValHIGH = percentToScaleHIGH * outputRange ' convert percent to output value outputData[3] = outputValHIGH / 100 ' reduce to proper value endif return