Serial Call-and-Response (Microcontroller side)
This code reads three sensors and sends them out serially when prompted by another computer. It works with this Processing example. Arduino/Wiring, PicBasic Pro, and BX-24 versions are shown.
code and fabrication resources for physical computing and networking
tigoe.net | pcomp home | blog | code, circuits, & construction | my del.icio.us links
{ Category Archives }
This code reads three sensors and sends them out serially when prompted by another computer. It works with this Processing example. Arduino/Wiring, PicBasic Pro, and BX-24 versions are shown.
This program controls a stepper motor from an analog input. In testing it, I used a potentiometer, which gives a persistent value. An analog input that changes when it’s not contacted, like a SpectraSymbol linear or circular pot, wouldn’t work with this example.
Thanks to the Fall ‘05 Wednesday morning class for the early experimentation on this code.
Written in PicBasic Pro, tested on a PIC18F252
This program connects a microcontroller to a mySQL database via a Lantronix Xport and a PHP script.
The PHP script it uses is the SQL_datalog.php script in the Network Data Logging Suite.
Technorati Tags: networked objects, networks
This suite of programs takes data from a sensor and saves it to a text file on a network. Each sensor reading is time stamped. The suite illustrates the basic principles involved in sending sensor data to a networked file or database.
The first program involved is a microcontroller program, written in PicBasic Pro, tested on a PIC18F258. It waits for serial input from an external program. Then it reads its analog sensor, and sends the result out in two bytes.
The second program is the same microcontroller code in Wiring/Arduino, thanks to Jamie Allen for the cleanup.
The third program involved is a desktop computer program, written in Processing. It requests data via its serial port from the microprocessor and sends that data to a CGI program on a web server. It passes the data into the CGI using an HTTP GET request. This program only sends every three seconds, so as not to overwhelm the server with hits.
The fourth program is a CGI (common gareway interface) program, written in PHP. It takes in data from an HTTP GET request and appends it to a text file, along with the time the request was received. Note that this program does not check to see how big the file is, or whether the incoming data is properly formatted, so it isn’t terribly secure.
The fifth program is another PHP script that logs the data to a mySQL database. Running this doesn’t require any change in the microcontroller code, but it does require a slight change in the Processing code. The change is in the sentToNet() method, and is noted below.
Technorati Tags: networked objects, networks
This program takes input from a Sharp GP2D120 infrared ranging sensor and outputs the result in ASCII. The ranging formula comes from an excellent article on Acroname’s site, tweaked a bit to work with my circuit.
Here’s how to find the average of 9 samples, or the median number of a sorted list of samples. Wiring and PicBasic Pro examples follow.
Thanks to Zach Layton for correcting my bubble sort code.
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 Wiring, tested on an Arduino board.
This example finds the peak value of an analog sensor over time. It assumes the sensor is moving in a simple curve.
The program checks to see that the current value is above a given threshold, then checks to see if the value is greater than the previous value. If so, then it saves the current value as a peak. When the current value goes below the threshold again, it outputs the last peak value recorded.
For those applications where you want to sense a person touching an object, but can’t use a switch or a force-sensitive resistor, charge-transfer touch sensors can often be very useful. The one used in this example is made by Quantum, and it’s very simple to use.
Continue Reading »
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: