极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11859|回复: 2

求!processing如何画出两路波形

[复制链接]
发表于 2016-5-2 17:25:54 | 显示全部楼层 |阅读模式

我有程序可以画一路波形:

import processing.serial.*;
PFont font;
Scrollbar scaleBar;
Serial port;     

int Sensor;      // HOLDS PULSE SENSOR DATA FROM ARDUINO         
int IBI;         // HOLDS TIME BETWEN HEARTBEATS FROM ARDUINO      
int BPM;         // HOLDS HEART RATE VALUE FROM ARDUINO            
int[] RawY;      // HOLDS HEARTBEAT WAVEFORM DATA BEFORE SCALING      
int[] ScaledY;   // USED TO POSITION SCALED HEARTBEAT WAVEFORM         
int[] rate;      // USED TO POSITION BPM DATA WAVEFORM                  
float zoom;      // USED WHEN SCALING PULSE WAVEFORM TO PULSE WINDOW     
float offset;    // USED WHEN SCALING PULSE WAVEFORM TO PULSE WINDOW
color eggshell = color(255, 253, 248);  
int heart = 0;   // This variable times the heart image 'pulse' on screen   
//  THESE VARIABLES DETERMINE THE SIZE OF THE DATA WINDOWS           
int PulseWindowWidth = 490;
int PulseWindowHeight = 512;
int BPMWindowWidth = 180;
int BPMWindowHeight = 340;
boolean beat = false;    // set when a heart beat is detected, then cleared when the BPM graph is advanced

void setup() {
  size(700, 600);  // Stage size          屏大小
  frameRate(100);  
  font = loadFont("Arial-BoldMT-24.vlw");
  textFont(font);
  textAlign(CENTER);
  rectMode(CENTER);
  ellipseMode(CENTER);  
// Scrollbar constructor inputs: x,y,width,height,minVal,maxVal     
  scaleBar = new Scrollbar (400, 575, 180, 12, 0.5, 1.0);  // set parameters for the scale bar  
  RawY = new int[PulseWindowWidth];          // initialize raw pulse waveform array   
  ScaledY = new int[PulseWindowWidth];       // initialize scaled pulse waveform array   
  rate = new int [BPMWindowWidth];           // initialize BPM waveform array         
  zoom = 0.75;                               // initialize scale of heartbeat window   

// set the visualizer lines to 0   
for (int i=0; i<rate.length; i++){
    rate[i] = 555;      // Place BPM graph line at bottom of BPM Window   
   }
for (int i=0; i<RawY.length; i++){
    RawY[i] = height/2; // initialize the pulse window data line to V/2
}

// GO FIND THE ARDUINO   
  println(Serial.list());    // print a list of available serial ports  
  // choose the number between the [] that is connected to the Arduino   
  port = new Serial(this, Serial.list()[0], 115200);  // make sure Arduino is talking serial at this baud rate   
  port.clear();            // flush buffer   
  port.bufferUntil('\n');  // set buffer full flag on receipt of carriage return   
}

void draw() {
  background(0);
  noStroke();
// DRAW OUT THE PULSE WINDOW AND BPM WINDOW RECTANGLES     
  fill(eggshell);  // color for the window background   
  rect(255,height/2,PulseWindowWidth,PulseWindowHeight);
  rect(600,385,BPMWindowWidth,BPMWindowHeight);

// DRAW THE PULSE WAVEFORM   
  // prepare pulse data points     
  RawY[RawY.length-1] = (1023 - Sensor) - 212; // place the new raw datapoint at the end of the array
  zoom = scaleBar.getPos();                      // get current waveform scale value   
  offset = map(zoom,0.5,1,150,0);                // calculate the offset needed at this scale  
  for (int i = 0; i < RawY.length-1; i++) {      // move the pulse waveform by  
    RawY[i] = RawY[i+1];                         // shifting all raw datapoints one pixel left  
    float dummy = RawY[i] * zoom + offset;       // adjust the raw data to the selected scale   
    ScaledY[i] = constrain(int(dummy),44,556);   // transfer the raw data array to the scaled array   
  }
  stroke(250,0,0);                               // red is a good color for the pulse waveform  
  noFill();
  beginShape();                                  // using beginShape() renders fast   
  for (int x = 1; x < ScaledY.length-1; x++) {   
    vertex(x+10, ScaledY[x]);                    //draw a line connecting the data points  
  }
  endShape();

//  DO THE SCROLLBAR THINGS  
  scaleBar.update (mouseX, mouseY);
  scaleBar.display();

//   
}  //end of draw loop  

这个程序是从串口0接受一个传感器的数据,然后画出波形,我现在想不断在两个传感器进行跳转采集出数据  传1  和传2(数据结果为121212121212121212交替从串口0上传),然后同时画出这两个波形。上面这个程序是单一路的,怎样才能传1一路,传2 一路,两路都显示呢?求大神指点。
回复

使用道具 举报

发表于 2016-5-5 17:47:30 | 显示全部楼层
将两个数据放入两个数组,再一起画。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-5-5 19:08:05 | 显示全部楼层
林定祥 发表于 2016-5-5 17:47
将两个数据放入两个数组,再一起画。

嗯嗯,谢谢!我觉得也是,关键我不知道怎么改,你能指出在哪块怎样改一改吗?谢谢。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 05:00 , Processed in 0.061449 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表