我正在做一个学生项目。我正在尝试录制一个有节奏的构图,并根据它绘制一条垂直线条。它看起来像是在一个木箱(arduino;标准的firmata)上敲一辆电车pam-pam。然后处理需要映射此记录的时间和屏幕的宽度 - 并在敲击的位置绘制垂直线。
请帮助,在哪里查看此时间,然后将其映射到屏幕。
到目前为止,我有这段代码。但是当有屏幕空间时,它只会在敲击时绘制线条;并保存为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 :(得分: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);
}
}