Code only working in Processing 2

edited January 2017 in Raspberry PI

Hello! Why is this code not working in processing 3? I would just run it with processing 2, however I need to run it on a raspberry pi.

What part of the code is incompatible / what has to be changed to make it work?

Thanks!

    /******************************************************************************
    Heart_Rate_Display.ino
    Demo Program for AD8232 Heart Rate sensor.
    Casey Kuhns @ SparkFun Electronics
    6/27/2014
    https://github.com/sparkfun/AD8232_Heart_Rate_Monitor

    The AD8232 Heart Rate sensor is a low cost EKG/ECG sensor.  This example shows
    how to create an ECG with real time display.  The display is using Processing.
    This sketch is based heavily on the Graphing Tutorial provided in the Arduino
    IDE. http://www.arduino.cc/en/Tutorial/Graph

    Resources:
    This program requires a Processing sketch to view the data in real time.

    Development environment specifics:
        IDE: Arduino 1.0.5
        Hardware Platform: Arduino Pro 3.3V/8MHz
        AD8232 Heart Monitor Version: 1.0

    This code is beerware. If you see me (or any other SparkFun employee) at the
    local pub, and you've found our code helpful, please buy us a round!

    Distributed as-is; no warranty is given.
    ******************************************************************************/

    import processing.serial.*;

    Serial myPort;        // The serial port
    int xPos = 1;         // horizontal position of the graph
    float height_old = 0;
    float height_new = 0;
    float inByte = 0;


    void setup () {
      // set the window size:
      size(1000, 400);        

      // List all the available serial ports
      println(Serial.list());
      // Open whatever port is the one you're using.
      myPort = new Serial(this, Serial.list()[2], 9600);
      // don't generate a serialEvent() unless you get a newline character:
      myPort.bufferUntil('\n');
      // set inital background:
      background(0xff);
    }


    void draw () {
      // everything happens in the serialEvent()
    }


    void serialEvent (Serial myPort) {
      // get the ASCII string:
      String inString = myPort.readStringUntil('\n');

      if (inString != null) {
        // trim off any whitespace:
        inString = trim(inString);

        // If leads off detection is true notify with blue line
        if (inString.equals("!")) { 
          stroke(0, 0, 0xff); //Set stroke to blue ( R, G, B)
          inByte = 512;  // middle of the ADC range (Flat Line)
        }
        // If the data is good let it through
        else {
          stroke(0xff, 0, 0); //Set stroke to red ( R, G, B)
          inByte = float(inString); 
         }

         //Map and draw the line for new data point
         inByte = map(inByte, 0, 1023, 0, height);
         height_new = height - inByte; 
         line(xPos - 1, height_old, xPos, height_new);
         height_old = height_new;

          // at the edge of the screen, go back to the beginning:
          if (xPos >= width) {
            xPos = 0;
            background(0xff);
          } 
          else {
            // increment the horizontal position:
            xPos++;
          }

      }
    }

Answers

  • Answer ✓

    Drawing-commands inside of serialEvent() do not work anymore. You can make "inString" global and move everything but the first line from serialEvent() to draw().

      import processing.serial.*;
    
      Serial myPort;        // The serial port
      int xPos = 1;         // horizontal position of the graph
      float height_old = 0;
      float height_new = 0;
      float inByte = 0;
    
      // added here as a global variable
      String inString;
    
      void setup () {
        // set the window size:
        size(1000, 400);        
    
        // List all the available serial ports
        println(Serial.list());
        // Open whatever port is the one you're using.
        myPort = new Serial(this, Serial.list()[2], 9600);
        // don't generate a serialEvent() unless you get a newline character:
        myPort.bufferUntil('\n');
        // set inital background:
        background(0xff);
      }
    
    
      void draw () {
        // everything in here has previously been inside of serialEvent()
    
        if (inString != null) {
          // trim off any whitespace:
          inString = trim(inString);
    
          // If leads off detection is true notify with blue line
          if (inString.equals("!")) { 
            stroke(0, 0, 0xff); //Set stroke to blue ( R, G, B)
            inByte = 512;  // middle of the ADC range (Flat Line)
          }
          // If the data is good let it through
          else {
            stroke(0xff, 0, 0); //Set stroke to red ( R, G, B)
            inByte = float(inString);
          }
    
          //Map and draw the line for new data point
          inByte = map(inByte, 0, 1023, 0, height);
          height_new = height - inByte; 
          line(xPos - 1, height_old, xPos, height_new);
          height_old = height_new;
    
          // at the edge of the screen, go back to the beginning:
          if (xPos >= width) {
            xPos = 0;
            background(0xff);
          } else {
            // increment the horizontal position:
            xPos++;
          }
        }
      }
    
    
      void serialEvent (Serial myPort) {
        // assign serial Message to the global variable "inString"
        inString = myPort.readStringUntil('\n');
      }
    

    The problem with this: You might miss some values if you receive more than one value per frame.

  • @benja Thanks, it now works! However due to the skipped readings the resolution is worse. Also the raspberry pi seems not to be powerful enough to produce a decent framerate.

Sign In or Register to comment.