2015-04-22 107 views
0

我在做一個學生項目。我正在嘗試錄製一個有節奏的作品,並以此爲基礎繪製一條垂直線條。它看起來像是在一個木箱(arduino;標準的冷藏箱)上敲一輛電車帕姆帕姆。然後處理需要映射該記錄的時間和屏幕的寬度 - 並在敲打的地方畫垂直線。處理/節奏記錄

請幫助,在哪裏查看記錄這一次,然後將其映射到屏幕上。

到目前爲止,我有這樣的代碼。但是當屏幕出現空間時,它只會在敲門時畫出線條;並以pdf格式保存。

import processing.serial.*; 
import cc.arduino.*; 
import processing.pdf.*; 

Arduino arduino; 

Serial myPort;  

int x = 0; 

void setup() { 
    size(500, 500); 
    background(#ffffff); 

    println(Arduino.list()); 

    arduino = new Arduino(this, "/dev/tty.usbmodem1411", 57600); 

    //Set the Arduino digital pins as inputs. 
    arduino.pinMode(0, Arduino.INPUT); 

    beginRecord(PDF, "everything.pdf"); 
} 
void draw() { 
    stroke(0); 

    for (int i = 0; i <= 0; i++) { 
     if (arduino.analogRead(i)>0) { 
      line(x, 0, x, height); 
     } 
     else { 
       x +=1; 
     } 
    } 
} 
void keyPressed() { 
    endRecord(); 
    codexit(); 
} 

回答

0

我終於完成了這件事。假設,它可以做得更好,更廣泛,但事實是它是有效的。

患得患失的小視頻: https://www.dropbox.com/s/1dp5tqqx16zp4l7/Abramova-5FCC0022-video.mov?dl=0

,代碼:

import processing.serial.*; 
import cc.arduino.*; 

Arduino arduino; 
PrintWriter output; 

// The serial port: 
Serial myPort;  

int t = millis(); 
int[] time; 

void setup() { 
size(500, 500); 
background(#ffffff); 

// Prints out the available serial ports. 
println(Arduino.list()); 


// Modify this line, by changing the "0" to the index of the serial 
// port corresponding to your Arduino board (as it appears in the list 
// printed by the line above). 
arduino = new Arduino(this, "/dev/tty.usbmodem1411", 57600); 

// Alternatively, use the name of the serial port corresponding to your 
// Arduino (in double-quotes), as in the following line. 
//arduino = new Arduino(this, "/dev/tty.usbmodem621", 57600); 

// Set the Arduino digital pins as inputs. 
arduino.pinMode(0, Arduino.INPUT); 

// Creates the output for the time, dedicated to the beats. 
output = createWriter("time.txt"); 

} 

void draw() { 

// when arduino sends signal, store the current 
// time in milliseconds since the program started 

if (arduino.analogRead(0)>30) { 

// grabs the time, passed before the beat from start 

String numbers = "millis()"; 
delay(100); 
output.print (millis() + ","); 

} 
} 

void keyPressed() { 
output.flush(); // Writes the remaining data to the file 
output.close(); // Finishes the file 

} 

void keyReleased() { 

// Interprets the string from the saved beat sequence 
String[] numbers = loadStrings("time.txt"); 
time = int(split(numbers[0],',')); 

stroke(0); 
strokeWeight(5); 

// Draws lines, based on a string 

for (int i = 1; i < time.length; i++) { 
int c = time[i]-time[0]; 
int d = time[time.length - 2]; 
int e = time[0]; 
int f = d-e; 

// Vertical lines 
line(c*500/f, 0 , c*500/f , height); 

// Horisontal lines 
line(0, c*500/f, width, c*500/f); 

// Drawing rects 

// Yellow 
fill (255, 255, 0); 
int m = (time [1] - time [0])*500/f; 
int n = (time [2] - time [0])*500/f; 
rect (m, m, n-m, n-m); 

// Blue 
fill (0, 0, 255); 
int o = (time [8] - time [0])*500/f; 
int p = (time [4] - time [0])*500/f; 
rect (o, m, p-o, p-o); 

// Red 
fill(255, 0, 0); 
int v = (time [3] - time [0])*500/f; 
int k = (time [6] - time [0])*500/f; 
int z = (time [8] - time [0])*500/f; 
rect(v, p, z-v, k-v); 

} 

}