找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 6837|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

arduino和processing的互動(dòng)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:114320 發(fā)表于 2016-5-8 01:29 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
本帖最后由 51黑專家 于 2016-5-8 01:30 編輯

      先通過(guò)arduino R3 串口輸出字串,然后用processing來(lái)讀取串口數(shù)據(jù),并且繪制折線圖進(jìn)行顯示。
arduino源碼:

void setup(){
   Serial.begin(115200);
}

void loop(){
  int num = random(0,1023);
  Serial.print("S");
  Serial.println(num);
  delay(100);
  Serial.print("B");
  Serial.println(num);
  delay(100);
    Serial.print("Q");
  Serial.println(num);
  delay(100);
}


processing源碼:

  1. /**
  2. THIS PROGRAM WORKS WITH PulseSensorAmped_Arduino-xx ARDUINO CODE
  3. THE PULSE DATA WINDOW IS SCALEABLE WITH SCROLLBAR AT BOTTOM OF SCREEN
  4. PRESS 'S' OR 's' KEY TO SAVE A PICTURE OF THE SCREEN IN SKETCH FOLDER (.jpg)
  5. MADE BY JOEL MURPHY AUGUST, 2012
  6. */


  7. import processing.serial.*;
  8. PFont font;
  9. Scrollbar scaleBar;

  10. Serial port;     

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


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

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

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

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

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

  79. // DRAW THE BPM WAVE FORM
  80. // first, shift the BPM waveform over to fit then next data point only when a beat is found
  81. if (beat == true){   // move the heart rate line over one pixel every time the heart beats
  82.    beat = false;      // clear beat flag (beat flag waset in serialEvent tab)
  83.    for (int i=0; i<rate.length-1; i++){
  84.      rate[i] = rate[i+1];                  // shift the bpm Y coordinates over one pixel to the left
  85.    }
  86. // then limit and scale the BPM value
  87.    BPM = min(BPM,200);                     // limit the highest BPM value to 200
  88.    float dummy = map(BPM,0,200,555,215);   // map it to the heart rate window Y
  89.    rate[rate.length-1] = int(dummy);       // set the rightmost pixel to the new data point value
  90. }
  91. // GRAPH THE HEART RATE WAVEFORM
  92. stroke(250,0,0);                          // color of heart rate graph
  93. strokeWeight(2);                          // thicker line is easier to read
  94. noFill();
  95. beginShape();
  96. for (int i=0; i < rate.length-1; i++){    // variable 'i' will take the place of pixel x position   
  97.    vertex(i+510, rate[i]);                 // display history of heart rate datapoints
  98. }
  99. endShape();

  100. // DRAW THE HEART AND MAYBE MAKE IT BEAT
  101.   fill(250,0,0);
  102.   stroke(250,0,0);
  103.   // the 'heart' variable is set in serialEvent when arduino sees a beat happen
  104.   heart--;                    // heart is used to time how long the heart graphic swells when your heart beats
  105.   heart = max(heart,0);       // don't let the heart variable go into negative numbers
  106.   if (heart > 0){             // if a beat happened recently,
  107.     strokeWeight(8);          // make the heart big
  108.   }
  109.   smooth();   // draw the heart with two bezier curves
  110.   bezier(width-100,50, width-20,-20, width,140, width-100,150);
  111.   bezier(width-100,50, width-190,-20, width-200,140, width-100,150);
  112.   strokeWeight(1);          // reset the strokeWeight for next time


  113. // PRINT THE DATA AND VARIABLE VALUES
  114.   fill(eggshell);                                       // get ready to print text
  115.   text("Pulse Sensor Amped Visualizer 1.1",245,30);     // tell them what you are
  116.   text("IBI " + IBI + "mS",600,585);                    // print the time between heartbeats in mS
  117.   text(BPM + " BPM",600,200);                           // print the Beats Per Minute
  118.   text("Pulse Window Scale " + nf(zoom,1,2), 150, 585); // show the current scale of Pulse Window

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

  122. //   
  123. }  //end of draw loop


  124. //.............................................scalebar (name of file)............................................................................................................


  125. /**
  126.     THIS SCROLLBAR OBJECT IS BASED ON THE ONE FROM THE BOOK "Processing" by Reas and Fry
  127. */

  128. class Scrollbar{
  129. int x,y;               // the x and y coordinates
  130. float sw, sh;          // width and height of scrollbar
  131. float pos;             // position of thumb
  132. float posMin, posMax;  // max and min values of thumb
  133. boolean rollover;      // true when the mouse is over
  134. boolean locked;        // true when it's the active scrollbar
  135. float minVal, maxVal;  // min and max values for the thumb

  136. Scrollbar (int xp, int yp, int w, int h, float miv, float mav){ // values passed from the constructor
  137.   x = xp;
  138.   y = yp;
  139.   sw = w;
  140.   sh = h;
  141.   minVal = miv;
  142.   maxVal = mav;
  143.   pos = x - sh/2;
  144.   posMin = x-sw/2;
  145.   posMax = x + sw/2;  // - sh;
  146. }

  147. // updates the 'over' boolean and position of thumb
  148. void update(int mx, int my) {
  149.    if (over(mx, my) == true){
  150.      rollover = true;            // when the mouse is over the scrollbar, rollover is true
  151.    } else {
  152.      rollover = false;
  153.    }
  154.    if (locked == true){
  155.     pos = constrain (mx, posMin, posMax);
  156.    }
  157. }

  158. // locks the thumb so the mouse can move off and still update
  159. void press(int mx, int my){
  160.    if (rollover == true){
  161.     locked = true;            // when rollover is true, pressing the mouse button will lock the scrollbar on
  162.    }else{
  163.     locked = false;
  164.    }
  165. }

  166. // resets the scrollbar to neutral
  167. void release(){
  168.   locked = false;
  169. }

  170. // returns true if the cursor is over the scrollbar
  171. boolean over(int mx, int my){
  172.   if ((mx > x-sw/2) && (mx < x+sw/2) && (my > y-sh/2) && (my < y+sh/2)){
  173.    return true;
  174.   }else{
  175.    return false;
  176.   }
  177. }

  178. // draws the scrollbar on the screen
  179. void display (){

  180.   noStroke();
  181.   fill(255);
  182.   rect(x, y, sw, sh);      // create the scrollbar
  183.   fill (250,0,0);
  184.   if ((rollover == true) || (locked == true)){            
  185.    stroke(250,0,0);
  186.    strokeWeight(8);           // make the scale dot bigger if you're on it
  187.   }
  188.   ellipse(pos, y, sh, sh);     // create the scaling dot
  189.   strokeWeight(1);            // reset strokeWeight
  190. }

  191. // returns the current value of the thumb
  192. float getPos() {
  193.   float scalar = sw / sw;  // (sw - sh/2);
  194.   float ratio = (pos-(x-sw/2)) * scalar;
  195.   float p = minVal + (ratio/sw * (maxVal - minVal));
  196.   return p;
  197. }
  198. }

  199. //...........................................serialevent (name of file)..........................................................................................................



  200. void serialEvent(Serial port){
  201.    String inData = port.readStringUntil('\n');
  202.    inData = trim(inData);                 // cut off white space (carriage return)
  203.    
  204.    if (inData.charAt(0) == 'S'){          // leading 'S' for sensor data
  205.      inData = inData.substring(1);        // cut off the leading 'S'

  206.      Sensor = int(inData);       // convert the string to usable int
  207.    }
  208.    
  209.    if (inData.charAt(0) == 'B'){          // leading 'B' for BPM data
  210.      inData = inData.substring(1);        // cut off the leading 'B'
  211.      BPM = int(inData);                   // convert the string to usable int
  212.      beat = true;                         // set beat flag to advance heart rate graph
  213.      heart = 20;                          // begin heart image 'swell' timer
  214.    }
  215. if (inData.charAt(0) == 'Q'){            // leading 'Q' means IBI data
  216.      inData = inData.substring(1);        // cut off the leading 'Q'
  217.      IBI = int(inData);                   // convert the string to usable int
  218.    }
  219. }
復(fù)制代碼


瞧瞧結(jié)果


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂1 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:134699 發(fā)表于 2016-7-23 09:36 | 只看該作者
請(qǐng)問(wèn)樓主,這個(gè)顯示圖形的軟件去哪里下載啊
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表