Monthly Archives: June 2004
Lantronix Device Connection with serial feedback
The Lantronix Device has to have its connect mode set to verbose, so that it will send ASCII N when not connected, ASCII D when disconnecting, and ASCII C when a connection is made.’ connection to the Lantronix device through a hex inverter:TX var portd.0RX var portd.1′ serial to PC for debugging:debugTX var portc.6′ some indicator LED’s for later use:yellowLED var portb.2greenLED var portb.7redLED var portb.3′ two switches:switch1 var portb.0switch2 var portb.1′ data coming in the serial port:inbyte var byte’ set the switch pins to input mode:input switch1input switch2′ clear all variables:clearmain: ‘ the Lantronix device is set to connect mode D5, meaning that ‘ it will return a byte to the PIC to indicate ‘ the connection status.
Continue reading
Analog Smoothing Algorithm
// divide by 4 to print the result as a byte: Serial.print(smoothed/4, BYTE); // delay before next reading delay(10);}// Blink the reset LED:void blink(int howManyTimes) { int i; for (i=0; i< howManyTimes; i++) { digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(200); }}// Smooth out an analog reading:void smoothValue(int rawValue) { if (rawValue > smoothed) { smoothed = smoothed + (rawValue – smoothed)/alpha; } else { smoothed = smoothed – (smoothed – rawValue)/alpha; }}Written in PicBasic Pro, tested on a PIC 18F252:’ Analog smoothing algorithm’ by Tom Igoe ‘ This program reads an analog input and smooths out the result by averaging ‘ the result with past values of the analog input.’
…This needs to be a global variablebyteVar var byte ‘ a byte variable to send out seriallyalpha var byte ‘ the number of past samples to average bytrimPotValue var word ‘ the trimmer pot input’ serial variables and constants:tx var portc.6rx var portc.7inv9600 con 16468′ Variables for subroutines:i var byteLEDPin var portb.7gosub blinkmain: ‘ read the trim pot to determine alpha between 1 and 10: adcin 1, trimPotValue alpha = (trimPotValue / 114) + 1 ‘ get an analog reading: adcin 0, analogVal ‘ smooth it: gosub smoothValue ‘ to see the difference, try outputting analogVal ‘ instead of smoothed here, and graph the difference. Continue reading