# 20220929 arduino ## 練習1 ``` void setup() { size(400, 400); } void draw() { line(mouseX, mouseY, width/2, height/2); strokeWeight(millis()/1000); } void mouseClicked() { println("mouseClick"); saveFrame("image###.jpg"); background(random(255)); stroke(random(255), random(255), random(255)); } ``` ## 練習1圖片 --- ## Arduino Serial ``` void setup() { Serial.begin(9600); } void loop() { Serial.println(1); delay(1000); Serial.println(2); delay(1000); } ``` ### processing code: arduino 1,2 => processing =>background ``` import processing.serial.*; Serial myPort; int val; void setup() { size(500, 500); myPort = new Serial(this, "COM4", 9600); } void draw() { if ( myPort.available() > 0) { // If data is available, val = myPort.read(); // read it and store it in val println(val+":"+char(val)); if (val == 49) background(255, 0, 0, 0); if (val == 50) background(0, 255, 0); } } ``` ### 練習 循序圖 ```plantuml @startuml user -> button: 按下按鈕 button -> arduino: 改變電路的high gnd arduino -> serial: 1,2 serial -> processing: 1,2 processing -> display: ellipse2rect @enduml ``` ### arduino button -> 1,2 ``` void setup() { Serial.begin(9600); pinMode(2, INPUT_PULLUP); } void loop() { delay(300); if (digitalRead(2) == HIGH) { Serial.println(1); } if (digitalRead(2) == LOW) { Serial.println(2); } } ```