We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
serial read() help! (Read 1766 times)
serial read() help!
Mar 4th, 2010, 5:12am
 
i have made the following arduino sketch:
Code:
#include <CapSense.h>

/*
* CapitiveSense Library Demo Sketch
* Paul Badger 2008
* Uses a high value resistor e.g. 10M between send pin and receive pin
* Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
* Receive pin is the sensor pin - try different amounts of foil/metal on this pin
* Best results are obtained if sensor foil and wire is covered with an insulator such as paper or plastic sheet
*/


CapSense cs_4_2 = CapSense(4,2); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
void setup()
{

cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
Serial.begin(9600);

}

void loop()
{
long theremin = cs_4_2.capSense(30);
// tab character for debug windown spacing
int test=666;
Serial.print(theremin);
Serial.print("\t");
Serial.print(test);
//Serial.print("\r");
Serial.print("\n"); // print sensor output 1

delay(10); // arbitrary delay to limit data to serial port
}


it takes capacitive input using capsense library and prints serial back to processing in the format:

capsense \t otherdata \n

the tabs are used to identify new data in processing and newline is used to difer when a pile of data is read.

my question is how do i write a processing sketch that catches these two pieces of data and stores them in two variables.
i have searched and troubleshooted and tried my way forth and havent figured anything out yet.

thx! Cheesy
Re: serial read() help!
Reply #1 - Mar 4th, 2010, 5:23am
 
ive figured something out. now i can read only the first bunch of data:
Code:

import processing.serial.*;

Serial port;
String buff1 = "";
String buff2 = "";
int NEWLINE = 10;
int oldval=0;
int farve=255;
int mode=0;
int val=0;
// Store the last 64 values received so we can graph them.
int[] values = new int[64];

void setup()
{
size(512, 512);
strokeWeight(3);

println("Available serial ports:");
println(Serial.list());

// Uses the first port in this list (number 0). Change this to
// select the port corresponding to your Arduino board. The last
// parameter (e.g. 9600) is the speed of the communication. It
// has to correspond to the value passed to Serial.begin() in your
// Arduino sketch.
port = new Serial(this, Serial.list()[1], 9600);

// If you know the name of the port used by the Arduino board, you
// can specify it directly like this.
//port = new Serial(this, "COM1", 9600);
}

void draw()
{
background(53);
stroke(farve);

// Graph the stored values by drawing a lines between them.
for (int i = 0; i < 63; i++)
line(i * 8, height - values[i]/100, (i + 1) * 8, height - values[i +1]/100);

while (port.available() > 0)
serialEvent(port.read());
println(oldval);
int oldval2=constrain(oldval,0,500);
farve=(int)map(oldval2, 0,500,0,255);
}

void serialEvent(int serial)
{
//
if (serial != NEWLINE) {

if(serial!= TAB){

buff1 += char(serial);
}


// Store all the characters on the line.

} else {

// The end of each line is marked by two characters, a carriage
// return and a newline. We're here because we've gotten a newline,
// but we still need to strip off the carriage return.
buff1 = buff1.substring(0, buff1.length()-1);

// Parse the String into an integer. We divide by 4 because
// analog inputs go from 0 to 1023 while colors in Processing
// only go from 0 to 255.

val = (Integer.parseInt(buff1)/10+oldval)/2;


//println("test");
// Clear the value of "buff"
buff1 = "";

// Shift over the existing values to make room for the new one.
for (int i = 0; i < 63; i++)
values[i] = values[i + 1];

// Add the received value to the array.
values[63] = val;
oldval=val;


mode=0;

}
}
Page Index Toggle Pages: 1