Pulse Input Displayed to Processing Window

edited September 2016 in Arduino

Hello! First time poster, new to Processing.

I was following the following tutorial on making a Pulse Sensor: https://www.youtube.com/watch?v=psTa5ZrqAyo

I was able to see output from a scope. I can therefore conclude output is being sent to the Arduino.

The code provided does not draw any pulse in Processing 3. I added a few lines, they are:

noFill();
stroke(0,255,0);
strokeWeight(4);

On lines 40-42. The rest is original code from the tutorial.

It is as follows:

    // Based on examples from Arduino's Graphing Tutorial and OscP5 documentation
    import processing.serial.*;
    Serial myPort; // The serial port
    int xPos = 1; // horizontal position of the graph
    float oldHeartrateHeight = 0; // for storing the previous reading


    void setup () {
    // set the window size:
    size(600, 400);
    frameRate(25);

    // List available serial ports.
    println(Serial.list());

    // Setup which serial port to use.
    // This line might change for different computers.
    myPort = new Serial(this, Serial.list()[0], 9600);

    // set inital background:
    background(0);
    }

    void draw () {
    }

    void serialEvent (Serial myPort) {
    // read the string from the serial port.
    String inString = myPort.readStringUntil('\n');

    if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);
    // convert to an int
    println();
    int currentHeartrate = int(inString);

    // draw the Heartrate BPM Graph.
    float heartrateHeight = map(currentHeartrate, 0, 1023, 0, height);
    noFill();
    stroke(0,255,0);
    strokeWeight(4);
    line(xPos - 1, height - oldHeartrateHeight, xPos, height - heartrateHeight);
    oldHeartrateHeight = heartrateHeight;
    // at the edge of the screen, go back to the beginning:
    if (xPos >= width) {
    xPos = 0;
    background(0);
    } else {
    // increment the horizontal position:
    xPos++;

    }
    }
    }

Any ideas what lines I need to append to get the output to display?

Any help is much appreciated, thanks in advance.

Answers

  • Do not "draw" outside the "Animation Thread". In your case, do not "draw" under serialEvent()! [-X

  • edited September 2016

    I'm not exactly sure what that means @GoToLoop

    I have modified the code to be the following:

    Note, last night I had better output on the screen.. today I see output but it looks like a bunch of noise (Will check the circuit again) and occasionally when I run this code it either works, or tells me I have a NulPointerException error on line

        int currentHeartrate = int(inString);
    

    The modified code is as follows:

        // Based on examples from Arduino's Graphing Tutorial and OscP5 documentation
        import processing.serial.*;
        Serial myPort; // The serial port
        int xPos = 1; // horizontal position of the graph
        float oldHeartrateHeight = 0; // for storing the previous reading
    
    
        void setup () {
        // set the window size:
        size(600, 400);
        frameRate(25);
    
        // List available serial ports.
        println(Serial.list());
    
        // Setup which serial port to use.
        // This line might change for different computers.
        myPort = new Serial(this, Serial.list()[0], 9600);
    
        // set inital background:
        background(0);
        }
    
    
        void serialEvent (Serial myPort) {
        // read the string from the serial port.
    
        }
    
        void draw () {
            String inString = myPort.readStringUntil('\n');
    
        if (inString != null) {
        // trim off any whitespace:
        inString = trim(inString);
        // convert to an int
        println();
    
        }
    
        // draw the Heartrate BPM Graph.
        int currentHeartrate = int(inString);
        float heartrateHeight = map(currentHeartrate, 0, 1023, 0, height);
        noFill();
        stroke(0,255,0);
        //strokeWeight(1);
        line(xPos - 1, height - oldHeartrateHeight, xPos, height - heartrateHeight);
        oldHeartrateHeight = heartrateHeight;
        // at the edge of the screen, go back to the beginning:
        if (xPos >= width) {
        xPos = 0;
        background(0);
        } else {
        // increment the horizontal position:
        xPos++;}
    
    
    
        }
    
Sign In or Register to comment.