Serial communication between Arduino and Processing and resetting the sketch problem

Hello, I am a student who is working hard on a project.
I’m currently using Arduino’s heart rate sensor to create a line drawing that changes the thickness of the line in real time.
But I’m facing two problems now.
One problem is that I don’t know where to put Arduino’s sensor value received through serial communication.
And the last problem is that when you press the keyboard, the screen is reset and a new screen comes out. I’ve seen a lot of forums, but if you use the phrase void mouse pressed (or key pressed), you’ll still see a syntax error.

My English is not good enough, but thank you for your help.

  • import cc.arduino.;
    import org.firmata.
    ;
    import processing.serial.;
    import processing.video.
    ;
    Serial port;
    int Sensor;
    PImage myImage;
    int numSteps = 300, counter = 0;
    Canvas myCanvas;
    float zFactor = 0;
    Capture cam;
    boolean showNow=false; //
    void setup(){
    size(640,480);
    frameRate(60);
    frameRate(60);
    myCanvas = new Canvas(new PVector(0, 0));
    noFill();
    String cameras = Capture.list();
    cam = new Capture(this, cameras[1]);
    cam.start();
    background(255);
    println(Serial.list()); //
    port = new Serial(this, Serial.list()[2], 115200); //
    port.clear(); // flush buffer
    port.bufferUntil(‘\n’); // }
    void draw()
    {
    if (cam.available() == true && showNow == false )
    {
    cam.read();
    //image(cam, 0, 0);
    showNow = true;
    int cam_width = 640;
    int cam_height = 480;
    myImage = createImage(640, 480, RGB);
    myImage.copy(cam, 0, 0, cam.width, cam.height, 0, 0, myImage.width, myImage.height);
    myImage.loadPixels();
    }
    if ( showNow == true)
    {
    for(int i = 0; i < numSteps; i++){
    myCanvas.update();
    myCanvas.show();
    zFactor += 0.01;
    }
    println(Sensor);
    }
    }

class Canvas{
PVector previousPos, currentPos, velocity = new PVector(0, 0), force = new PVector(0, 0);
float maxSpeed = 3, surroundMatrix = 3, noiseMultiplier = 1.0;
int drawAlpha = 30, drawWeight = 1, maxCounters= 70;
color drawColor = color(1, 0, 0, drawAlpha);
Canvas(PVector pos){
previousPos = pos.copy();
currentPos = pos.copy();
}
void update(){
previousPos = currentPos.copy();
force.mult(0);
PVector target = new PVector(0, 0);
counter = 0;
for(float i = -floor(surroundMatrix / 2); i < surroundMatrix / 2; i++){
for(float j = -floor(surroundMatrix / 2); j < surroundMatrix / 2; j++){
if (i == 0 && j == 0){
continue;
}
int x = floor(currentPos.x + i);
int y = floor(currentPos.y + j);
if(((myImage.width - 1 - x) | (x - 0) | (myImage.height - 1 - y) | (y - 0)) >= 0) {
color c = color(red(getColor(x, y)), green(getColor(x, y)), blue(getColor(x, y)), alpha(getColor(x, y)));
drawColor = c;
float b = brightness(c); //Get pixel brighness
b = 1 - b / 20.0;
PVector pos = new PVector(i, j);
target.add(pos.div(pos.mag()).copy().mult(b));
counter++;
}
}
}
if(counter != 0){
force.add(target.div(counter));
}
float n = noise(currentPos.x, currentPos.y, zFactor);
n = map(n, 0, 1, 0, 5 * TWO_PI);
PVector p = PVector.fromAngle(n);
if(force.mag() < 0.01){
force.add(p.mult(noiseMultiplier * 5));
}
else{
force.add(p.mult(noiseMultiplier));
}
velocity.add(force);
if (velocity.mag() > maxSpeed) {
velocity.normalize().mult(maxSpeed);
}
currentPos.add(velocity);
if (!((floor(width - currentPos.x) | floor(currentPos.x - 0) | floor(height - currentPos.y) | floor(currentPos.y - 0)) >= 0)) {
reset();
}
}
void reset(){
myImage.updatePixels();
myImage.loadPixels();
counter = 0;
boolean hasFound = false;
while(!hasFound){
currentPos.x = random(1) * width;
currentPos.y = random(1) * height;
color c = color(red(getColor(floor(currentPos.x), floor(currentPos.y))), green(getColor(floor(currentPos.x), floor(currentPos.y))),
blue(getColor(floor(currentPos.x), floor(currentPos.y))), alpha(getColor(floor(currentPos.x), floor(currentPos.y))));
float b = brightness(c);
if(b < 255){
hasFound = true;
}
}
drawColor = color(red(getColor(floor(currentPos.x), floor(currentPos.y))), green(getColor(floor(currentPos.x), floor(currentPos.y))),
blue(getColor(floor(currentPos.x), floor(currentPos.y))), drawAlpha);
previousPos = currentPos.copy();
velocity.mult(0);
}
void show(){
counter++;
if(counter > maxCounters){
reset();
}
stroke(drawColor);
drawColor = color(red(getColor(floor(currentPos.x), floor(currentPos.y))), green(getColor(floor(currentPos.x), floor(currentPos.y))),
blue(getColor(floor(currentPos.x), floor(currentPos.y))), drawAlpha);
strokeWeight(drawWeight);
line(previousPos.x, previousPos.y, currentPos.x, currentPos.y);
fadeLineFromImg(previousPos.x, previousPos.y, currentPos.x, currentPos.y);
}
}

void fadeLineFromImg(float x1, float y1, float x2, float y2){
int xOffset = floor(abs(x1 - x2));
int yOffset = floor(abs(y1 - y2));
int step = xOffset < yOffset ? yOffset : xOffset;
for (int i = 0 ; i < step ; i++) {
int x = floor(x1 + (x2 - x1) * i / step);
int y = floor(y1 + (y2 - y1) * i / step);
color originColor = color(red(getColor(x, y)), green(getColor(x, y)), blue(getColor(x, y)), alpha(getColor(x, y)));
float r = red(originColor);
float g = green(originColor);
float b = blue(originColor);
originColor = color(r + 50 > 255 ? 255 : (r + 50), g + 50 > 255 ? 255 : (g + 50), b + 50 > 255 ? 255 : (b + 50))
setColor(x, y, originColor);
}
}
color getColor(int i, int j) {
int index = j * myImage.width + i;
return color(red(myImage.pixels[index]), green(myImage.pixels[index]), blue(myImage.pixels[index]), alpha(myImage.pixels[index]));
}
void setColor(int i, int j, color c) {
int index = j * myImage.width + i;
myImage.pixels[index] = color(red(c), green(c), blue(c), alpha(c));
}

void serialEvent(Serial port){
String inData = port.readStringUntil(‘\n’);
inData = trim(inData);
if (inData.charAt(0) == ‘S’){
inData = inData.substring(1); //
Sensor = int(inData); //
}
}

Hi @jinjin, I can’t give you the complete answer for your project, but I think you need help to understand a few things, and then you’ll put the pieces together to get it working.

(It would be easier for people to help you if you could edit your post, and select the whole code, and select </> .)

That code is all Processing? You haven’t shown your Arduino code because you are using Firmata?
(I don’t know how to use Firmata with the heartbeat sensor.)

At the moment you have one big project, but I think you should think of it as a few smaller projects, and try other sketches and examples just to see how things work.

  • What kind of Arduino do you have? I’m thinking what options there are for seeing what’s happening. Do you have any LCD or OLED displays for it?

  • Arduino and heartbeat - what is the definition of what comes from that? Is it working?

  • ‘void mousePressed()’ - find that in the help, copy the example and try it.

  • I created an example of Arduino and Processing serial comms. That should be very easy to run on your hardware. One way forward would be for you to substitute your heartbeat value for one of those values, then change the display to what you want to see.

If you can answer the questions and try a few things we can probably get it working.

Richard.