我終於完成了這件事。假設,它可以做得更好,更廣泛,但事實是它是有效的。
患得患失的小視頻: 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);
}
}