So I have 7 different sensor I’d like to send to Processing, then use each of their values as variables inside different webcam filters.
I’m going off of this tutorial which has been helpful, but it doesn’t give linear steps – more like different techniques depending on what you’re trying to use it for. In that tutorial, it shows two ways to initialize connection between Serial and Arduino – the Punctuation method and the Call-and-response method. I used the Punctuation method.
To sum up the Punctuation method, you put the sensor values into one string, separated with commas. Once the values are sent over, you split the string and put all of the values into an array. Once in an array, you can individually take out each of the values and turn them into variables that are usable in the draw loop.
My Processing sketch runs perfectly fine sometimes. Otherwise it will give either this error :
Error, disabling serialEvent() for /dev/cu.usbmodem1421
null
Or this one :
Does anyone see anything fishy with the syntax or structure I’m setting up? (I took out all filter functions except the temp -> blur to make debugging easier.)
// get the serial lib :
import processing.serial.*;
// give port a variable :
Serial port;
// get the webcam lib :
import processing.video.*;
// give webcam a variable :
Capture webcam;
// sensors
int cap1, cap2, piezo, temp, axisX, axisY, axisZ;
void setup() {
size(480, 360, P2D);
smooth();
webcam = new Capture(this, width, height, 24);
webcam.start();
String portName = "/dev/cu.usbmodem1421";
port = new Serial(this, portName, 9600);
//port.bufferUntil('\n');
}
void captureEvent(Capture webcam) {
webcam.read();
}
void serialEvent(Serial port) {
String reading = port.readStringUntil('\n');
if (reading != null) {
reading = trim(reading);
int values[] = int(split(reading, ','));
for (int valNum = 0; valNum < values.length; valNum++) {
print(values[valNum] + "|");
}
println();
if (values.length > 1) {
cap1 = values[0];
cap2 = values[1];
piezo = values[2];
temp = values[3];
axisX = values[4];
axisY = values[5];
axisZ = values[6];
}
}
}
void draw() {
// temp
float t;
if (temp >= 100) { //when analog pin unplugged
t = 6;
} else {
t = 0;
}
image(webcam, 0, 0);
filter(BLUR, t);
}
//if down key pressed, saveImage();
void saveImage()
{
saveFrame("images/captured-####.jpg");
}
void keyPressed() {
if (key == CODED) {
if (keyCode == DOWN) {
saveImage();
}
}
}
I’m thinking it’s an issue with memory, or maybe the way I’m sending/receiving the data is shoddy.
Apparently one of the disadvantages to using Punctuation rather than Call-and-response is that it can slow down the program if it contains a lot of graphic work… but why would it work in perfect time sometimes and then not work at all?
Any insight would be amazing!!
print
in SerialEvent print out just before the error? Also, whyif (values.length > 1)
? Seems likeif (values.length > 6)
would work better