Monday, January 30, 2006

Sensors and Time




Using Flex Sensor
Code Reference from Tom Igoe

'****************************************************************
' 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 20 ' Set sampling time in uS

PeakValue var word
SensorValue var word
LastSensorValue var word
Threshold var word
Noise var word

' serial pins and data reate:
tx var portc.6
rx var portc.7
n9600 con 16468

Threshold = 50 ' set your own value based on your sensors
PeakValue = 0 ' initialize peakValue
noise = 5 ' set a noise value based on your particular sensor

' Set PORTA to all input
TRISA = %11111111
' Set up ADCON1
ADCON1 = %10000010

Main:
' read sensor on pin RA0:
ADCin 0, sensorValue
serout2 tx, n9600, [sensorValue]
'original line was useful when sending data to HyperTerminal.
'original line: serout2 tx, n9600, ["peak reading", DEC peakValue, 13,10]
pause 100
Goto main

Processing code reference

import processing.serial.*;

Serial myPort; // The serial port

// initial variables:
int i = 1; // counter
int inByte = -1; // data from serial port

void setup () {
size(400, 300); // window size

// List all the available serial ports
println(Serial.list());
// I know that the third port in the serial list on my mac
// is always my Keyspan adaptor, so I open Serial.list()[2].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);

// set inital background:
background(0);
}
void draw () {
if (myPort.available() > 0) {
inByte = myPort.read();
serialEvent();
}
}

void serialEvent () {
// draw the line:
stroke(0,255,0);
line(i, height, i, height - inByte);
// at the edge of the screen, go back to the beginning:
if (i >= width) {
i = 0;
background(0);
}
else {
i++;
}
}

0 Comments:

Post a Comment

<< Home