Weighted averaging of an analog input (Smoothing)

Here’s an example that filters an analog sensor reading by taking a weighted average of samples of the sensor. It’s based on this algorithm:

 filteredValue = x * rawValue + (1-x)*lastFilteredValue;

Where X is a value between 0 and 1 that indicates how reliable the new raw value is. If it’s 100% reliable, X = 1, and no filtering is done. If it’s totally unreliable, x = 0, and the raw result is filtered out. Examples for Wiring and PicBasic Pro follow:

Written in C for Arduino

Continue reading “Weighted averaging of an analog input (Smoothing)”

Peak Finding

updated 9 Sept 2014

This example finds the peak value of an analog sensor over time. It assumes the sensor is moving in a simple curve.

The peak value is initially set at zero. The program checks to see that the current value is greater than the current peak value. If so, then it saves the current value as a peak.  If the current value is below a given threshold and the current peak value is above it, then the current peak is taken as the final peak. This example also includes a noise value, that allows you to adjust for sensor fluctuation.

The graph below gives you a visual sense of how the program operates. At readings 1, 2, and 3, the latest reading becomes the new peak. At reading 4, the peak remains unchanged because reading 4 is less than the last recorded peak. But the peak isn’t reported until reading 5, when the current reading goes below the threshold. This is the one disadvantage to this peak finding method: you don’t know you’ve had a peak until after it’s over.

peak_explained

Continue reading “Peak Finding”

Native PIC programming on OSX

Hans Steiner and Patrick Dwyer forwarded a couple of links that should make it possible to program PIC microcontrollers on the Mac. You need to do it in C, and you need to use the Mac Developer Tools, and you need to find or build a programmer that can interface to the tools provided. But here’s a few links (haven’t tried this myself yet):
GNU PIC utilities – GPUTILS is a collection of tools for the Microchip (TM) PIC microcontrollers.
SciSpot lists a link to MacrocASM, an assembler/programmer for the PIC that’s native to OSX, and a few links to hardware programmers.
The combination of these should mean you can program the PIC in C on a Mac. However, there are still some things missing, for those used to higher level programming like BASIC and CCS C. The nice function libraries, like ADC and serial, that come with CCS C and PicBasic Pro, are not here. You will definitely need to know more about the lower level details of the PIC. But hey, it’s a step out of PC world for Mac PIC people.
A bit more from Brygg Ullmer at LSU:
“Regarding Mac programming of PICs: we have had success with this at LSU, to at least some extent. The “picp” program compiled cleanly and worked fine for us from Mac Mini’s (OSX at the time), downloading to PICSTART-compatible programmers (both real and clone, following leads from this list) with a USB->Serial dongle. (Some dongles worked, and others not; but I suspect you guys have a handle on this).
“This was only a half-solution, as we were compiling on the Linux-based CCS compiler on a different machine, scp’ing it over, then picp’ing it on the Mac Mini to a PIC. We’ve just successfully Fedorified a number of Mac Minis (we needed support for some devices which had clean support under Linux, but not under OSX; e.g., using evrouter and gizmod). That *could* open a path toward using CCS on Mac hardware, too — but I slightly doubt they’ve released PPC-compatible binaries yet.”
9-15-05

Technorati Tags: , , ,

Bluetooth Stuff

Everybody loves the bluetooth nowadays. I learned what Bluejacking is at E-tech this year. As a wise woman pointed out to me: “Bluejacking, that’s what teenage boys do in the privacy of their bedrooms, right?” Similar enough.

Mike Sharon pointed out this little BT accelerometer board. I’d change the PIC for an 18F, but it’s really nice. The radio on it can be found here.

Here’s parallax’ EmbeddedBlue module.

I have a Promi SD 102 and the Promi ESD from Initium. They’re nice. They’ve got a distributor in Connecticut, Lemos:

Lemos International Co. Inc.
1305 Post Road
Suite 305
Fairfield, Ct. 06430
TEL: 203-254-1531
FAX: 203-254-7442
www.lemosint.com
sales@lemosint.com

A note on the Promi SD: Jamie Allen got this from the folks at Initium:

“If you want to use Promi-SD with Tx, Rx, and ground pins only, you have to disable hardware flow control feature on Promi-SD.
The AT command for this is:

AT+UARTCONFIG, baudrate, parity, stopbit, flow control

For example, if you use 9600 bps, no parity, one stopbit, no hardware flow control, then command is

AT+UARTCONFIG,9600,N,1,0

Or, you may do same job at Promi-WIN(v3b) software.”

I also have the EmbeddedBlue board from Parallax. Also nice, but the form factor’s harder to work with on a breadboard.

Here’s another one,

Mark Argo turned me on to blueRadios as well. They offer a version for those who want to write their own Bluetooth stack, which is worth the extra cost if you’re looking for custom functions.

Anyone got more of them? I want easy ones, something I only have to send AT commands to via a serial port, at worst. The less soldering, the better, as long as it’s cheap. I want world peace too.

Edge Detection

Most of the time, you don’t need to know whether a switch is on or off so much as you need to know when it changed. You want to know when it turned on or when it turned off. In other words, you want to find the edge of the transition from on to off, or vice versa. This is often called edge detection.

The basic idea is that you check not only what the state of the switch is, but what the state was the last time you checked, and compare the two. Here’s an example in PicBasic Pro:

Continue reading “Edge Detection”