Rewriting(Problem with AD8232)

edited September 2017 in Arduino

Hello, my name is Vinicius and I have a big problem with my project, I've been working with the AD8232 interface, the main objective is simulate an ECG(Electrocardiogram)program, to turn the layout of my console more realistic as every ECG must be I've added an image of ECG background, however in always moments that I add this image the line of ECG simply disappear, I've tried many types of changes but without success, please I need to send this project in 2 weeks and I don't have idea how can I solve this bloke, I'II lead the code below, thank everyone.

import processing.serial.*;

Serial myPort;        // The serial port

PImage ecg;
int xPos = 1;         // horizontal position of the graph
float height_old = 0;
float height_new = 0;
float inByte = 0;
float x=0;
float y=0;

float spacing=50;


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, "COM3", 9600);
  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');
  // set inital background:
  background(238,223,204);
  ecg=loadImage("ECG-full (1).jpg");
}

void draw () {
  // everything happens in the serialEvent()
 image(ecg,100,0);
}

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(#390606); //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, 1200, 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(238,223,204);
      } 
      else {
        // increment the horizontal position:
        xPos++;
      }

  }
}

Sem título

Answers

  • SerialEvent and the draw() function are two different threads. When you draw into your sketch, you should limit all modification of your sketch area in draw. In the serial event, you do only data handling. Here is my attempt although keep in mind it is untested code.

    Kf

    import processing.serial.*;
    
    final float FLAT_LINE=512;
    final float MINVALUE=0;
    final float MAXVALUE=1200;
    
    
    PImage ecg;
    int xPos = 1;         // horizontal position of the graph
    
    float[] ecgData;
    Serial myPort;        // The serial port
    
    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, "COM3", 9600);
      // don't generate a serialEvent() unless you get a newline character:
      myPort.bufferUntil('\n');
      // set inital background:
      background(238, 223, 204);
      ecg=loadImage("ECG-full (1).jpg");
      ecgData=new float[width];
    
      //Init data to flat line
      for (int i=0; i<width; i++)
        ecgData[i]=FLAT_LINE;
    
      stroke(0);
    }
    
    void draw () {
      // everything happens in the serialEvent()
      image(ecg, 100, 0);
    
      //Draw all data
      for (int i=0; i<width-1; i++)
        line(i, ecgData[i], i+1, ecgData[i+1]);
    }
    
    void serialEvent (Serial myPort) {
      // get the ASCII string:
      String inString = myPort.readStringUntil('\n');
      float inByte = 0;
    
      if (inString != null) {
        // trim off any whitespace:
        inString = trim(inString);
    
        // If leads off detection is true notify with blue line
        if (inString.equals("!")) { 
          stroke(#390606); //Set stroke to blue ( R, G, B)
          inByte = FLAT_LINE;  // 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);
        }
    
        ecgData[xPos]=map(inByte, MINVALUE, MAXVALUE, height, 0);
        xPos=(xPos+1)%width;   //Ensure value is increased and limited into the range of 0 to width
      }
    }
    
Sign In or Register to comment.