Processing 과 Arduino 송송송
PowerPoint
ProcessingArduino
- 1 -
https://processing.org/Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts.
Processing
2001 MIT JVM - 2 -
Processing IDE
Setup() , draw() Arduino IDE Java - 3 -
Processing
: https://processing.org Download - 4 -
ConsoleHello World
setup() draw() print() println() (new line)- 5 -
Drawing
Size
fullScreen size
background ( )
fill
rect
- 6 -
Draw Frames
rect(x, 100, 200, 200); //
draw() x += add; // : add = -add;
- 7 -
Draw Framesvoid setup() { size(640, 480); //W, H}
int r_x = 0;int add = 5;int e_x = 20, e_y = 20;int e_add_x = 3, e_add_y = 3;
void draw() { background(200, 200, 200); //R, G, B noStroke(); fill(255, 0, 0); //R, G, B rect(r_x, 400, 100, 30); //X, Y, W, H stroke(0, 0, 0); fill(0, 255, 0); ellipse(e_x, e_y, 20, 20); // X, Y, RX, RY e_x += e_add_x; e_y += e_add_y; if(e_x < 10 || e_x > 640-10) e_add_x = -e_add_x; if(e_y < 10 || e_y > 480-10) e_add_y = -e_add_y; if(e_x > r_x && e_x < r_x+100 && e_y > 400 && e_y < 400+30) e_add_y = -e_add_y; r_x += add; if(r_x < 0 || r_x > 640-100) add = -add;}
eclipse(x, y, rx, ry) , (x,y), (rx), (ry)
: e_x, e_y : e_add_x, e_add_y
: r_x : add
- 8 -
sampleint count = 200;int snowx[]= new int[count];int snowy[]= new int[count];int speed[]= new int[count];int size[]= new int[count];
void setup() { size(320, 200); for(int i=0; i= 0) { line(p_x, p_y, mouseX, mouseY); }}
void mousePressed() { if(p_x >= 0 && p_y >= 0) { LineElement l = new LineElement(); l.x = p_x; l.y = p_y; l.x2 = mouseX; l.y2 = mouseY; lines.add(l); } p_x = mouseX; p_y = mouseY;}
Mouse move Mouse Press (LineElement) LineElement (ArrayList) (1) : background(0)(2) : for(LineElement l : lines)- 13 -
Keyboard Eventvoid setup() {}
void draw() { if (keyPressed) { if (key == 'b' || key == 'B') { background(0); } else { background(255); } } }int value = 0;
void draw() { background(value);}
void keyPressed() { if (key == 'b' || key == 'B') { value = 0; } else { value = 255; }}void draw() {}
void keyPressed() { println("pressed " + key + "(" + keyCode + ")");}
void keyReleased() { println("released " + key + "(" + keyCode + ")");}
void keyTyped() { println("typed " + key + "(" + keyCode + ")");}Key PressedPressed Mouse Buttonpressed a(65)typed a(0)released a(65)
keyPressed keyPressed() keyCode
keyPressed()keyReleased()keyTypes()- 14 -
samplevoid setup() { size(640, 480); //W, H}
int r_x = 0;int e_x = 20, e_y = 20;int e_add_x = 3, e_add_y = 3;
void draw() { background(200, 200, 200); //R, G, B noStroke(); fill(255, 0, 0); //R, G, B rect(r_x, 400, 100, 30); //X, Y, W, H stroke(0, 0, 0); fill(0, 255, 0); ellipse(e_x, e_y, 20, 20); // X, Y, RX, RY e_x += e_add_x; e_y += e_add_y; if(e_x < 10 || e_x > 640-10) e_add_x = -e_add_x; if(e_y < 10 || e_y > 480-10) e_add_y = -e_add_y; if(e_x > r_x && e_x < r_x+100 && e_y > 400 && e_y < 400+30) e_add_y = -e_add_y;}void keyPressed() { if(keyCode == LEFT) { r_x -= 10; if(r_x < 0) r_x = 0; } else if(keyCode == RIGHT) { r_x += 10; if(r_x > 640-100) r_x = 640-100; }}
: e_x, e_y : e_add_x, e_add_y
: r_x :
- 15 -
SerialArduino PC Serial.begin(9600);Serial.print(command);Serial.read()serial = new Serial();serial.readChar();serial.writeChar();
Aruino Serial Serial Arduino Serial C/C++JavaPythonJavascript (Node.js)Processing Serial - 16 -
Arduino Side
void setup() { pinMode(8, OUTPUT); Serial.begin(9600);}
void loop() { if (Serial.available() > 0) { char ch = Serial.read(); if(ch == '1') digitalWrite(8, HIGH); else digitalWrite(8, LOW); }}
Serial Monitor
Arduino Serial
Serial Serial.println : Serial.read : Serial.available :
Serial
1 LED , LED - 17 -
Processing Sideimport processing.serial.*;
Serial serial;int pMouseX = 0;
void setup() { size(320, 200); serial = new Serial(this, Serial.list()[0], 9600);}
void draw() { fill(0); rect(0, 0, 160, 200); fill(255); rect(160, 0, 160, 200); if(pMouseX < 160 && mouseX >= 160) { serial.write('1'); } else if(pMouseX >= 160 && mouseX < 160) { serial.write('0'); } pMouseX = mouseX;}
Processing Serial
Serial Serial.list() Serial Arduino Serial.write - 18 -
Arduino Side
int pButtonState = 0;
void setup() { pinMode(8, INPUT); Serial.begin(9600);}
void loop() { int buttonPressed = digitalRead(8); if(pButtonState == LOW && buttonPressed == HIGH) { Serial.print('1'); } pButtonState = buttonPressed;}Serial Monitor
Arduino Serial
Serial Serial.println :
1
- 19 -
Processing Side
import processing.serial.*;
Serial serial;
void setup() { size(320, 200); serial = new Serial(this, Serial.list()[0], 9600); background(0);}
void draw() { if(serial.available() > 0) { int ch = serial.read(); if(ch == '1') { int red = (int)random(255); int green = (int)random(255); int blue = (int)random(255); int x = (int)random(320); int y = (int)random(200); int size = (int)random(30, 100); fill(red, green, blue, 180); ellipse(x, y, size, size); } }}
Processing Serial
Serial.read : Serial.available :
1 ( )
- 20 -
sample
Arduino Side
void setup() { Serial.begin(9600);}
void loop() { int value = analogRead(A1); Serial.println(value); delay(100);}
PC ( )
analogRead (0 ~ 1023 )
- 21 -
sample
Processing Sidevoid draw() { background(200, 200, 200); //R, G, B noStroke(); fill(255, 0, 0); //R, G, B rect(r_x, 400, 100, 30); //X, Y, W, H stroke(0, 0, 0); fill(0, 255, 0); ellipse(e_x, e_y, 20, 20); // X, Y, RX, RY if(serial.available() > 0) { String val = serial.readStringUntil(lf); if(val != null) { val = val.trim(); if(val.length() > 0) { int val2 = Integer.parseInt(val.trim()); r_x = val2 * (640-100) / 1024; } } } e_x += e_add_x; e_y += e_add_y; if(e_x < 10 || e_x > 640-10) e_add_x = -e_add_x; if(e_y < 10 || e_y > 480-10) e_add_y = -e_add_y; if(e_x > r_x && e_x < r_x+100 && e_y > 400 && e_y < 400+30) e_add_y = -e_add_y;}import processing.serial.*;
int lf = 10;Serial serial;
int r_x = 0;int e_x = 20, e_y = 20;int e_add_x = 3, e_add_y = 3;
void setup() { size(640, 480); serial = new Serial(this, Serial.list()[0], 9600);}
PC (PC )
Serial.read Serial Bar
- 22 -
sample
Arduino Sideint pinTrig = 7;int pinEcho = 6;
void setup() { Serial.begin(9600); pinMode(pinTrig, OUTPUT); pinMode(pinEcho, INPUT);}
void loop() { int value = distance(); Serial.println(value); delay(300);}
long distance() { digitalWrite(pinTrig, LOW); delayMicroseconds(2); digitalWrite(pinTrig, HIGH); delayMicroseconds(10); digitalWrite(pinTrig, LOW);
long val= pulseIn(pinEcho, HIGH) * 17 / 100;
return val;}
PC ( )
PC
- 23 -
sample
Processing Sideimport processing.serial.*;
int lf = 10;float zoom = 0.1;Serial serial;PImage img;
void setup() { size(800, 600); img = loadImage("image.jpg"); serial = new Serial(this, Serial.list()[1], 9600);}
void draw() { background(0);
if(serial.available() > 0) { String val = serial.readStringUntil(lf); if(val != null) { val = val.trim(); if(val.length() > 0) { float val2 = Float.parseFloat(val.trim()); zoom = max(min(val2 / 100, 1.0), 0.1); } } } float imageWidth = img.width * zoom; float imageHeight = img.height * zoom; image(img, (800 - imageWidth)/2, (600 - imageHeight)/2, imageWidth, imageHeight);}
image.jpg
PC (PC )
Pimage image.jpg Image image( , x, y, , ) (zoom)min(A, B) : A, B max(A, B) : A, B max( min( ZOOM , 1.0 ) , 0.1 ) : ZOOM 0.1 ~ 1.0
- 24 -
https://youtu.be/jjvy_jzGlAQ + +
PC
PC PC
Processing
import processing.sound.*;SoundFile file;
file = new SoundFile(this, "sample.mp3");file.play();- 25 -