add week 8

This commit is contained in:
Tomoya Matsuura(MacBookPro) 2023-06-05 18:09:48 +09:00
parent 260dd3372a
commit 0912532389
69 changed files with 508 additions and 4 deletions

View File

@ -1,6 +1,6 @@
--- ---
title: "Code and Design 2023 class 7 Slides" title: "Code and Design 2023 class 7 Slides"
date: 2023-05-19 date: 2023-05-26
bookHidden: true bookHidden: true
--- ---

View File

@ -1,6 +1,6 @@
--- ---
title: "コードとデザイン 2023年 第7回 スライド" title: "コードとデザイン 2023年 第7回 スライド"
date: 2023-05-19 date: 2023-05-26
bookHidden: true bookHidden: true
--- ---

View File

@ -0,0 +1,152 @@
---
title: Week 7
date: 2023-05-26
weight: 7
---
# 2023 Tokyo University of the Arts Art media Center「Code and Design」
## Slides (in Japanese)
{{< button href="2023-code-and-design-7.pdf">}}SlidesPDF{{< /button >}}
{{< button href="./slides">}}SlidesHTML{{< /button >}}
## Python Scripts for Mouse Automation
https://github.com/tomoyanonymous/py-serial-automouse
{{< button href="https://github.com/tomoyanonymous/py-serial-automouse/archive/refs/heads/main.zip">}} DL Zip {{< /button >}}
## Arduino Example Codes
### ADCTouch_monitor.ino
{{< button href="./ADCTouch_monitor/ADCTouch_monitor.ino">}}DL{{< /button >}}
```cpp
#include <ADCTouch.h>
void setup() {
pinMode(A0, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
int val = ADCTouch.read(A0, 100);
Serial.println(val);
delay(40);
}
```
### ADCTouch_minimal.ino
{{< button href="./ADCTouch_minimal/ADCTouch_minimal.ino">}}DL{{< /button >}}
```cpp
#include <ADCTouch.h>
//setup()とloop()で共通して使う値はグローバル変数(関数の外側)で定義しておく
int ref = 0;
void setup() {
pinMode(A0, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
//繋げた導体の状態で初期値が変わるので、それを保存しておく必要がある
ref = ADCTouch.read(A0, 100); //(使用するピン番号,平均を取るサンプル数)
}
void loop() {
int val = ADCTouch.read(A0, 100) - ref;
if (val > 40) {
digitalWrite(LED_BUILTIN, HIGH);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
Serial.println(val);
delay(40);
}
```
### ADCTouch_Mouse
{{< button href="./ADCTouch_Mouse/ADCTouch_Mouse.ino">}}DL{{< /button >}}
```cpp
#include <ADCTouch.h>
const int upButton = A0;
const int downButton = A1;
const int leftButton = A2;
const int rightButton = A3;
const int mouseButton = A4;
int ref_up = 0;
int ref_down = 0;
int ref_left = 0;
int ref_right = 0;
int ref_mouse = 0;
int range = 10; // マウスカーソルの移動幅
void setup() {
pinMode(upButton, INPUT);
pinMode(downButton, INPUT);
pinMode(leftButton, INPUT);
pinMode(rightButton, INPUT);
pinMode(mouseButton, INPUT);
ref_up = ADCTouch.read(upButton, 100);
ref_down = ADCTouch.read(downButton, 100);
ref_left = ADCTouch.read(leftButton, 100);
ref_right = ADCTouch.read(rightButton, 100);
ref_mouse = ADCTouch.read(mouseButton, 100);
Serial.begin(9600);
}
//マウス移動に関する処理をひとまとめに
void moveMouse(int x, int y, int mouse_state) {
Serial.print(x);
Serial.print(" ");
Serial.print(y);
Serial.print(" ");
Serial.println(mouse_state);
}
void loop() {
// ボタンの検出
int v1 = ADCTouch.read(upButton, 100) - ref_up;
int v2 = ADCTouch.read(downButton, 100) - ref_down;
int v3 = ADCTouch.read(leftButton, 100) - ref_left;
int v4 = ADCTouch.read(rightButton, 100) - ref_right;
int v5 = ADCTouch.read(mouseButton, 100) - ref_mouse;
//40以上ならクリックしたとみなし、1を、40以下なら0を
// A ? B : C;はif (A) { return B;} else {return C;} の略記(三項演算子)
int upState = v1 > 40 ? 1 : 0;
int downState = v2 > 40 ? 1 : 0;
int rightState = v3 > 40 ? 1 : 0;
int leftState = v4 > 40 ? 1 : 0;
int clickState = v5 > 40 ? 1 : 0;
// 移動距離を算出(左右同時押しも考慮)
int xDistance = (leftState - rightState) * range;
int yDistance = (upState - downState) * range;
//移動もクリックもしてないならシリアル送信は冗長なので、しない
// &&  AND、!はNOT
if (!(xDistance == 0 && yDistance == 0 && clickState == 0)) {
moveMouse(xDistance, yDistance, clickState);
}
delay(5);
}
```
### DrunkMouse(Add Randomness)
{{< button href="./DrunkMouse/DrunkMouse.ino">}}DL{{< /button >}}
### ADCTouch_Mouse_HID.ino (Only works for Leonardo and Pro Micro)
{{< button href="./ADCTouch_Mouse_HID/ADCTouch_Mouse_HID.ino">}}DL{{< /button >}}
### ADCTouch_Mouse_scroll.ino(works only with `move_relative_scroll.command`)
{{< button href="./ADCTouch_Mouse_scroll/ADCTouch_Mouse_scroll.ino">}}DL{{< /button >}}

View File

@ -0,0 +1,183 @@
---
title: 第8週
date: 2023-06-02
weight: 8
---
# 2023年 東京藝術大学 芸術情報センター開設科目 「コードとデザイン」 第8回
## スライド
{{< button href="2023-code-and-design-8.pdf">}}スライドPDF{{< /button >}}
{{< button href="slides">}}スライドHTML{{< /button >}}
## Processingサンプルコード
### minimal_pong.pde
{{< button href="examples/minimal_pong/minimal_pong.pde">}}DL{{< /button >}}
```java
class Ball {
float x;
float y;
float vel_x;
float vel_y;
Ball() {
this.x = width/2;
this.y = height/2;
this.vel_x = random(0, 1.0) > 0.5 ? -3 : 3;
this.vel_y = random(0, 1.0) > 0.5 ? -3 : 3;
}
public void update_pos() {
this.x += this.vel_x;
this.y += this.vel_y;
if (this.x <0 || this.x > width) {
this.reflect_horizontal();
}
if (this.y < 0 ) {
this.reflect_vertical();
}
}
void reflect_horizontal() {
this.vel_x = -this.vel_x*1.1;
}
public void reflect_vertical() {
this.vel_y = -this.vel_y*1.1;
}
}
final float bar_width = 100;
final float bar_y = 550;
float bar_pos = 0.;
boolean gameover = false;
Ball ball = new Ball();
void setup() {
size(600, 600);
textMode(CENTER);
rectMode(CENTER);
}
void draw() {
ball.update_pos();
if (ball.y>height) {
gameover = true;
}
if (gameover) {
textSize(24);
text("game over", width/2, height/2);
} else {
bar_pos = mouseX;
if (ball.y > bar_y &&
ball.x > bar_pos - bar_width/2 &&
ball.x < bar_pos + bar_width/2) {
ball.reflect_vertical();
}
background(255);
fill(0);
rect(bar_pos, bar_y, bar_width, 30);
ellipse(ball.x, ball.y, 10, 10);
}
}
void mousePressed() {
if (gameover) {
gameover =false;
ball = new Ball();
}
}
```
### minimal_pong_firmata.pde
{{< button href="examples/minimal_pong_firmata/minimal_pong_firmata.pde">}}DL{{< /button >}}
```java
import processing.serial.*;
import cc.arduino.*;
import org.firmata.*;
class Ball {
float x;
float y;
float vel_x;
float vel_y;
Ball() {
this.x = width/2;
this.y = height/2;
this.vel_x = random(0, 1.0)> 0.5 ? -5:5;
this.vel_y = random(0, 1.0)> 0.5 ? -5:5;
}
public void update_pos() {
this.x += this.vel_x;
this.y += this.vel_y;
if (this.x <0 || this.x > width) {
this.reflect_horizontal();
}
if (this.y < 0 ) {
this.reflect_vertical();
}
}
void reflect_horizontal() {
this.vel_x = -this.vel_x*1.1;
}
public void reflect_vertical() {
this.vel_y = -this.vel_y*1.1;
}
}
final float bar_width = 100;
final float bar_y = 550;
float bar_pos = 0.;
boolean gameover = false;
Ball ball = new Ball();
Arduino arduino;
void setup() {
size(600, 600);
rectMode(CENTER);
textAlign(CENTER);
println(Arduino.list());
//リストアップされたもののうち、"tty.usbmodem"を含む番号を選択(not cu.usbmodem)
arduino = new Arduino(this, Arduino.list()[5], 57600);
arduino.pinMode(2,Arduino.INPUT);
}
void draw() {
bar_pos = map(arduino.analogRead(0), 0., 1024., 0., width);
if (arduino.digitalRead(2) == Arduino.HIGH){
buttonPressed();
}
ball.update_pos();
if (ball.y>height) {
gameover = true;
}
if (gameover) {
textSize(24);
text("game over", width/2, height/2);
} else {
if (ball.y > bar_y &&
ball.x > bar_pos - bar_width/2 &&
ball.x < bar_pos + bar_width/2) {
ball.reflect_vertical();
}
background(255);
fill(0);
rect(bar_pos, bar_y, bar_width, 30);
ellipse(ball.x, ball.y, 10, 10);
}
}
void buttonPressed() {
if (gameover) {
gameover = false;
ball = new Ball();
}
}
```

View File

@ -0,0 +1,68 @@
class Ball {
float x;
float y;
float vel_x;
float vel_y;
Ball() {
this.x = width/2;
this.y = height/2;
this.vel_x = random(0, 1.0) > 0.5 ? -3 : 3;
this.vel_y = random(0, 1.0) > 0.5 ? -3 : 3;
}
public void update_pos() {
this.x += this.vel_x;
this.y += this.vel_y;
if (this.x <0 || this.x > width) {
this.reflect_horizontal();
}
if (this.y < 0 ) {
this.reflect_vertical();
}
}
void reflect_horizontal() {
this.vel_x = -this.vel_x*1.1;
}
public void reflect_vertical() {
this.vel_y = -this.vel_y*1.1;
}
}
final float bar_width = 100;
final float bar_y = 550;
float bar_pos = 0.;
boolean gameover = false;
Ball ball = new Ball();
void setup() {
size(600, 600);
textMode(CENTER);
rectMode(CENTER);
}
void draw() {
ball.update_pos();
if (ball.y>height) {
gameover = true;
}
if (gameover) {
textSize(24);
text("game over", width/2, height/2);
} else {
bar_pos = mouseX;
if (ball.y > bar_y &&
ball.x > bar_pos - bar_width/2 &&
ball.x < bar_pos + bar_width/2) {
ball.reflect_vertical();
}
background(255);
fill(0);
rect(bar_pos, bar_y, bar_width, 30);
ellipse(ball.x, ball.y, 10, 10);
}
}
void mousePressed() {
if (gameover) {
gameover =false;
ball = new Ball();
}
}

View File

@ -0,0 +1,81 @@
import processing.serial.*;
import cc.arduino.*;
import org.firmata.*;
class Ball {
float x;
float y;
float vel_x;
float vel_y;
Ball() {
this.x = width/2;
this.y = height/2;
this.vel_x = random(0, 1.0)> 0.5 ? -5:5;
this.vel_y = random(0, 1.0)> 0.5 ? -5:5;
}
public void update_pos() {
this.x += this.vel_x;
this.y += this.vel_y;
if (this.x <0 || this.x > width) {
this.reflect_horizontal();
}
if (this.y < 0 ) {
this.reflect_vertical();
}
}
void reflect_horizontal() {
this.vel_x = -this.vel_x*1.1;
}
public void reflect_vertical() {
this.vel_y = -this.vel_y*1.1;
}
}
final float bar_width = 100;
final float bar_y = 550;
float bar_pos = 0.;
boolean gameover = false;
Ball ball = new Ball();
Arduino arduino;
void setup() {
size(600, 600);
rectMode(CENTER);
textAlign(CENTER);
println(Arduino.list());
//リストアップされたもののうち、"tty.usbmodem"を含む番号を選択(not cu.usbmodem)
arduino = new Arduino(this, Arduino.list()[5], 57600);
arduino.pinMode(2,Arduino.INPUT);
}
void draw() {
bar_pos = map(arduino.analogRead(0), 0., 1024., 0., width);
if (arduino.digitalRead(2) == Arduino.HIGH){
buttonPressed();
}
ball.update_pos();
if (ball.y>height) {
gameover = true;
}
if (gameover) {
textSize(24);
text("game over", width/2, height/2);
} else {
if (ball.y > bar_y &&
ball.x > bar_pos - bar_width/2 &&
ball.x < bar_pos + bar_width/2) {
ball.reflect_vertical();
}
background(255);
fill(0);
rect(bar_pos, bar_y, bar_width, 30);
ellipse(ball.x, ball.y, 10, 10);
}
}
void buttonPressed() {
if (gameover) {
gameover = false;
ball = new Ball();
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 318 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 299 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 222 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 443 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 494 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 748 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 852 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 240 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1013 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 263 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 177 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 441 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 423 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 329 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 444 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 340 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 165 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 437 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 465 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 556 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 286 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 298 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 460 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 497 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 386 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 449 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 496 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 296 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 561 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 344 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 583 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 250 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 412 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 968 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 604 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 269 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 219 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 296 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 347 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 965 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 247 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 337 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 310 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 508 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 500 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 822 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 368 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 598 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 718 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 479 KiB

View File

@ -0,0 +1,10 @@
---
title: "Code and Design 2023 class 8 Slides"
date: 2023-06-02
bookHidden: true
---
# Slides
{{< slides_jpg >}}

View File

@ -0,0 +1,10 @@
---
title: "コードとデザイン 2023年 第8回 スライド"
date: 2023-06-02
bookHidden: true
---
# スライド
{{< slides_jpg >}}

View File

@ -40,7 +40,7 @@ In addition, by learning how to convert data and materials into each other throu
5. [(5/12) Handmade Computer(DIWO Full Adder)](./5) 5. [(5/12) Handmade Computer(DIWO Full Adder)](./5)
6. [(5/19) Software and Algorithm - Let's try using Arduino](./6) 6. [(5/19) Software and Algorithm - Let's try using Arduino](./6)
7. [(5/26) Considering Inputs (Make Unreasonable Mouse)](./7) 7. [(5/26) Considering Inputs (Make Unreasonable Mouse)](./7)
8. (6/2) Considering Output (motor, solenoid, relay) 8. [(6/2) Considering Inputs 2 - Combining Processing and Arduino, OOP (PONG for One)](./8)
9. (6/9) Cartesian Robot and Digital Fabrication (Plotter, Laser Cutter) 9. (6/9) Cartesian Robot and Digital Fabrication (Plotter, Laser Cutter)
10. (6/16) Cartesian Robot and Digital Fabrication (3D Printer and G-code Hack) 10. (6/16) Cartesian Robot and Digital Fabrication (3D Printer and G-code Hack)
11. (6/23) Studio Work 1. Consultation 11. (6/23) Studio Work 1. Consultation

View File

@ -41,7 +41,7 @@ bookCollapseSection: true
5. [(5/12) Handmade Computerみんなでつくる全加算器](./5) 5. [(5/12) Handmade Computerみんなでつくる全加算器](./5)
6. [(5/19) ソフトウェアとアルゴリズムーArduinoを使ってみよう](./6) 6. [(5/19) ソフトウェアとアルゴリズムーArduinoを使ってみよう](./6)
7. [(5/26) 入力を考える(不条理なマウスを作る)](./7) 7. [(5/26) 入力を考える(不条理なマウスを作る)](./7)
8. (6/2) 出力を考える(モーター、ソレノイド、リレー) 8. [(6/2) 入力を考える2 - ProcessingとArduinoの連携、オブジェクト指向プログラミング一人用PONG](./8)
9. (6/9) Cartesian Robotとデジタルファブリケーションプロッター、レーザーカッター 9. (6/9) Cartesian Robotとデジタルファブリケーションプロッター、レーザーカッター
10. (6/16) Cartesian Robotとデジタルファブリケーション (3DプリンターとGコードハック) 10. (6/16) Cartesian Robotとデジタルファブリケーション (3DプリンターとGコードハック)
11. (6/23) 制作①相談 11. (6/23) 制作①相談