diff --git a/20241015/1015_1-2.pdf b/20241015/1015_1-2.pdf new file mode 100644 index 0000000..93281c9 Binary files /dev/null and b/20241015/1015_1-2.pdf differ diff --git a/20241015/1015_1_pwm/1015_1_pwm.ino b/20241015/1015_1_pwm/1015_1_pwm.ino new file mode 100644 index 0000000..16a7b6d --- /dev/null +++ b/20241015/1015_1_pwm/1015_1_pwm.ino @@ -0,0 +1,57 @@ +/* 1015_1_pwm.ino */ + +// Raspberry Pi Pico W: GPIO16番ピンを _SW1 という名前に、GPIO15番ピンを _LED1 という名前に +// Arduino UNO: 7番ピンを _SW1 という名前に、9番ピンを _LED1 という名前に +#if (defined(PICO_RP2040)) +#define _SW1 16 +#define _LED1 15 +#else +#define _SW1 7 +#define _LED1 9 +#endif + +// デジタル入力読み取り用の変数を設定 +int swState = 0; + +// PWMのデューティー比(= LEDの明るさ)を記憶する変数を設定 +int brightness = 0; + +// 経過時間計測用の変数を設定 +unsigned long lastPushed; + +void setup() { + pinMode(_SW1, INPUT_PULLUP); + pinMode(_LED1, OUTPUT); + + lastPushed = millis(); + + Serial.begin(19200); +} + +void loop() { + int tmp_s = !digitalRead(_SW1); + + // デジタル入力に変化があった時だけ更新 + if (swState != tmp_s) { + swState = tmp_s; + } + + // タクトスイッチが押されてる経過時間が200ミリ秒以上の時だけ実行する + if (swState && (millis() - lastPushed) >= 200) { + + // デューティー比の値をインクリメント(5ずつ足して、255で余剰演算) + // 0 ~ 254 の間で変化する + brightness = (brightness + 5) % 255; + + Serial.print("_SW1_State:"); + Serial.print(swState); + Serial.print(",brightness:"); + Serial.println(brightness); + + // タクトスイッチ押下の経過時間をアップデート + lastPushed = millis(); + } + + // PWM + analogWrite(_LED1, brightness); +} diff --git a/20241015/1015_2_pwm_fade/1015_2_pwm_fade.ino b/20241015/1015_2_pwm_fade/1015_2_pwm_fade.ino new file mode 100644 index 0000000..765aed2 --- /dev/null +++ b/20241015/1015_2_pwm_fade/1015_2_pwm_fade.ino @@ -0,0 +1,74 @@ +/* 1015_2_pwm_fade.ino */ + +// Raspberry Pi Pico W: GPIO16番ピンを _SW1 という名前に、GPIO15番ピンを _LED1 という名前に +// Arduino UNO: 7番ピンを _SW1 という名前に、9番ピンを _LED1 という名前に +#if (defined(PICO_RP2040)) +#define _SW1 16 +#define _LED1 15 +#else +#define _SW1 7 +#define _LED1 9 +#endif + +// デジタル入力読み取り用の変数を設定 +int swState = 0; + +// 角度をを記憶する変数を設定 +float deg = 0; + +// 角速度を記憶する変数を設定 +int speed = 1; + +// 経過時間計測用の変数を設定 +unsigned long elapsed; + +// 経過時間計測(タクトスイッチ押下)用の変数を設定 +unsigned long lastPushed; + +void setup() { + pinMode(_SW1, INPUT_PULLUP); + pinMode(_LED1, OUTPUT); + + lastPushed = millis(); + elapsed = micros(); + + Serial.begin(19200); +} + +void loop() { + int tmp_s = !digitalRead(_SW1); + + // デジタル入力に変化があった時だけ更新 + if (swState != tmp_s) { + swState = tmp_s; + } + + // タクトスイッチが押されてる経過時間が200ミリ秒以上の時だけ実行する + if (swState && (millis() - lastPushed) >= 200) { + + // ビットシフトを利用した角速度の増加 + // 1, 3, 7, ...512, 1023 の間で変化する + speed = (speed << 1) | speed; + if (speed > 1024) { speed = 1; } + + Serial.print("_SW1_State:"); + Serial.print(swState); + Serial.print(",speed:"); + Serial.println(speed); + + // タクトスイッチ押下の経過時間をアップデート + lastPushed = millis(); + } + + // 500マイクロ秒に一回実行 + if ((micros() - elapsed) >= 500) { + // cos() 関数を使った周期的なフェードイン・フェードアウト + deg += (speed / 1023.0); + int arc = abs((255 * cos(deg * M_PI / 180.0))); + + // PWM + analogWrite(_LED1, arc); + + elapsed = micros(); + } +} diff --git a/20241015/1015_3.pdf b/20241015/1015_3.pdf new file mode 100644 index 0000000..edcf1ed Binary files /dev/null and b/20241015/1015_3.pdf differ diff --git a/20241015/1015_3_pwm_aio/1015_3_pwm_aio.ino b/20241015/1015_3_pwm_aio/1015_3_pwm_aio.ino new file mode 100644 index 0000000..4e53e29 --- /dev/null +++ b/20241015/1015_3_pwm_aio/1015_3_pwm_aio.ino @@ -0,0 +1,59 @@ +/* 1015_3_pwm_aio.ino */ + +// Raspberry Pi Pico W: GPIO16番ピンを _SW1 という名前に、GPIO15番ピンを _LED1 という名前に +// Arduino UNO: 7番ピンを _SW1 という名前に、9番ピンを _LED1 という名前に +#if (defined(PICO_RP2040)) +#define _CDS1 A0 +#define _LED1 15 +#else +#define _CDS1 A0 +#define _LED1 9 +#endif + +// アナログ入力読み取り用のk各種変数(CdSセルの状態を記憶する領域)を設定する +// ノイズ対策のために、10サンプルの平均値を取る方法を採用 +const int bufSize = 10; +int ainBuf[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; +int bIndex = 0; +int ainTotal = 0; + +// 経過時間計測用の変数を設定 +unsigned long elapsed; + +void setup() { + pinMode(_CDS1, INPUT); + pinMode(_LED1, OUTPUT); + + elapsed = millis(); + + Serial.begin(19200); +} + +void loop() { + // 直近10サンプルの平均値を取る + ainTotal = ainTotal - ainBuf[bIndex]; + + ainBuf[bIndex] = analogRead(_CDS1); + + ainTotal = ainTotal + ainBuf[bIndex]; + + bIndex = (bIndex + 1) % bufSize; + + int avg = ainTotal / bufSize; + + // PWM + // map() 関数を使って、マッピングする + // 周囲の環境が明るいほど明るく点灯 + analogWrite(_LED1, map(avg, 5, 900, 0, 254)); + + // 反転版。周囲の環境が暗いほど明るく点灯 + // analogWrite(_LED1, map(avg, 5, 900, 254, 0)); + + // 50ミリ秒に一回実行 + if ((millis() - elapsed) >= 50) { + Serial.println(avg); + elapsed = millis(); + } + + delay(1); +} diff --git a/20241015/1015_4_serial_ide/1015_4_serial_ide.ino b/20241015/1015_4_serial_ide/1015_4_serial_ide.ino new file mode 100644 index 0000000..56b44af --- /dev/null +++ b/20241015/1015_4_serial_ide/1015_4_serial_ide.ino @@ -0,0 +1,33 @@ +/* 1015_4_serial_ide.ino */ + +// Raspberry Pi Pico W: GPIO16番ピンを _SW1 という名前に、GPIO15番ピンを _LED1 という名前に +// Arduino UNO: 7番ピンを _SW1 という名前に、9番ピンを _LED1 という名前に +#if (defined(PICO_RP2040)) +#define _LED1 15 +#else +#define _LED1 9 +#endif + +int swState = 0; +int brightness = 0; + +void setup() { + pinMode(_LED1, OUTPUT); + + Serial.begin(115200); +} + +void loop() { + // シリアルポートへデータが送られてきているかをチェック + if (Serial.available()) { + // シリアルバッファにあるデータを String(文字列)として取り出す + String s = Serial.readString(); + + // 文字列を int(数値)として変換 + brightness = constrain(s.toInt(), 0, 254); + + Serial.println(brightness); + } + + analogWrite(_LED1, brightness); +} diff --git a/20241015/1015_5.pdf b/20241015/1015_5.pdf new file mode 100644 index 0000000..ad1a7ef Binary files /dev/null and b/20241015/1015_5.pdf differ diff --git a/20241015/1015_n_serial_app/1015_n_serial_app.ino b/20241015/1015_n_serial_app/1015_n_serial_app.ino new file mode 100644 index 0000000..19e1e0d --- /dev/null +++ b/20241015/1015_n_serial_app/1015_n_serial_app.ino @@ -0,0 +1,110 @@ +/* 1015_n_serial.ino */ + +// Raspberry Pi Pico W: GPIO16番ピンを _SW1 という名前に、GPIO15番ピンを _LED1 という名前に +// Arduino UNO: 7番ピンを _SW1 という名前に、9番ピンを _LED1 という名前に +#if (defined(PICO_RP2040)) +#define _LED1 14 +#define _LED2 15 +#else +#define _LED1 9 +#define _LED2 10 +#endif + +unsigned long tick = 0; +int brightness = 0; + +const byte numChars = 64; +char receivedChars[numChars]; + +bool newData = false; + +void setup() { + pinMode(_LED1, OUTPUT); + + Serial.begin(57600); +} + +void loop() { + recvWithEndMarker(); + + if (newData == true) { + String tmp_str = String(receivedChars); + String tmp_perms[2] = { "", "" }; + int sidx = 0, pidx = 0; + + while (true) { + int fidx = tmp_str.indexOf(' ', sidx); + if (fidx != -1) { + String sstr = tmp_str.substring(sidx, fidx); + sidx = fidx + 1; + if (pidx < 2) { + tmp_perms[pidx] = sstr; + pidx ++; + } else { + break; + } + } else { + String rstr = tmp_str.substring(sidx, tmp_str.length()); + if (pidx < 2) { + tmp_perms[pidx] = rstr; + } + break; + } + } + + Serial.println(tmp_str); + + if (tmp_perms[0].length() != 0) { + int _v0 = tmp_perms[0].toInt(); + + Serial.print("Raw[0]:"); + Serial.print(tmp_perms[0]); + Serial.print(",Int[0]:"); + Serial.println(_v0); + + analogWrite(_LED1, constrain(_v0, 0, 1023)); + } + + if (tmp_perms[1].length() != 0) { + int _v1 = tmp_perms[1].toInt(); + Serial.print("Raw[1]:"); + Serial.print(tmp_perms[1]); + Serial.print(",Int[1]:"); + Serial.println(_v1); + + analogWrite(_LED2, constrain(_v1, 0, 1023)); + } + + newData = false; + } + + if ((millis() - tick) > 1000) { + tick = millis(); + Serial.println("tick"); + } +} + +void recvWithEndMarker() { + static byte ndx = 0; + const char endMarker = ';'; + char rc; + + if (Serial.available()) { + while (Serial.available() > 0 && newData == false) { + rc = Serial.read(); + + if (rc != endMarker) { + receivedChars[ndx] = rc; + ndx++; + if (ndx >= numChars) { + ndx = numChars - 1; + } + } + else { + receivedChars[ndx] = '\0'; // terminate the string + ndx = 0; + newData = true; + } + } + } +} diff --git a/20241015/1015_n_serial_app/com_manager.pd b/20241015/1015_n_serial_app/com_manager.pd new file mode 100644 index 0000000..30365bb --- /dev/null +++ b/20241015/1015_n_serial_app/com_manager.pd @@ -0,0 +1,209 @@ +#N canvas 733 172 160 200 8; +#X obj 238 454 fudiparse; +#X obj 226 538 print parsed; +#X obj 476 132 print comport_msg; +#X obj 343 538 print raw; +#X obj 226 502 gate; +#X obj 196 448 tgl 25 0 empty empty empty 17 7 0 10 #e4e4e4 #4d4d4d #373737 0 1; +#X obj 19 178 bng 25 250 50 0 empty empty empty 17 7 0 10 #e4e4e4 #4d4d4d #373737; +#X msg 353 392 clear; +#X obj 268 392 stream 128; +#X obj 258 184 select 13 10; +#X obj 150 280 gate; +#X obj 285 262 tgl 25 0 empty empty empty 17 7 0 10 #e4e4e4 #4d4d4d #373737 0 1; +#X msg 287 224 0; +#X msg 315 224 1; +#X obj 150 340 bag; +#X msg 238 312 flush; +#X obj 150 312 list append 1; +#X obj 238 423 list; +#X obj 238 340 t b; +#X obj 343 504 gate; +#X obj 141 178 tgl 25 0 empty empty empty 17 7 0 10 #e4e4e4 #4d4d4d #373737 0 1; +#X obj 170 538 outlet; +#N canvas 733 172 450 300 inittest 0; +#X msg 224 53 devices; +#X msg 164 53 ports; +#X msg 386 52 open 0; +#X msg 451 139 devicename /dev/ttyACM0; +#X msg 451 172 baud 57600; +#X msg 312 52 close; +#X msg 400 79 open 4; +#X obj 333 266 outlet; +#X msg 485 199 baud 115200; +#X msg 525 283 pollintervall 1; +#X msg 65 53 help; +#X msg 509 439 verbose \$1; +#X text 626 282 set poll interval for read in ms; +#X msg 114 53 info; +#X msg 621 426 hupcl \$1; +#X text 626 315 hang up connection on last close; +#X text 626 345 (not on Windows); +#X text 621 550 how many times to retry a lost connection; +#X msg 78 436 break \$1; +#X obj 89 330 delay 50; +#X obj 56 294 t b b; +#X obj 56 273 bng 15 250 50 0 empty empty empty 17 7 0 10 #fcfcfc #000000 #000000; +#X floatatom 141 296 5 0 0 0 - - - 0; +#X text 95 370 send break; +#X msg 426 485 inputprocess \$1; +#X text 384 523 enable automatic input processing; +#X text 384 549 (not on Windows \, default: disabled); +#X obj 621 377 tgl 25 0 empty empty empty 17 7 0 10 #e4e4e4 #4d4d4d #373737 0 1; +#X obj 466 433 tgl 25 0 empty empty empty 17 7 0 10 #e4e4e4 #4d4d4d #373737 0 1; +#X obj 391 485 tgl 25 0 empty empty empty 17 7 0 10 #e4e4e4 #4d4d4d #373737 0 1; +#X obj 502 100 hradio 25 1 0 12 empty empty empty 0 -8 0 10 #e4e4e4 #4d4d4d #373737 0; +#X msg 427 106 open \$1; +#X msg 621 524 retries 1; +#X msg 325 413 1; +#X obj 322 377 loadbang; +#X msg 548 160 devicename COM7; +#X msg 224 413 lock \$1; +#X obj 224 371 tgl 25 0 empty empty empty 17 7 0 10 #e4e4e4 #4d4d4d #373737 0 1; +#X obj 56 371 tgl 25 0 empty empty empty 17 7 0 10 #e4e4e4 #4d4d4d #373737 0 1; +#X obj 214 266 inlet; +#X connect 0 0 7 0; +#X connect 1 0 7 0; +#X connect 2 0 7 0; +#X connect 3 0 7 0; +#X connect 4 0 7 0; +#X connect 5 0 7 0; +#X connect 6 0 7 0; +#X connect 8 0 7 0; +#X connect 10 0 7 0; +#X connect 11 0 7 0; +#X connect 13 0 7 0; +#X connect 14 0 7 0; +#X connect 18 0 7 0; +#X connect 19 0 38 0; +#X connect 20 0 38 0; +#X connect 20 1 19 0; +#X connect 21 0 20 0; +#X connect 22 0 19 1; +#X connect 24 0 7 0; +#X connect 27 0 14 0; +#X connect 28 0 11 0; +#X connect 29 0 24 0; +#X connect 30 0 31 0; +#X connect 31 0 7 0; +#X connect 32 0 7 0; +#X connect 33 0 29 0; +#X connect 33 0 28 0; +#X connect 33 0 27 0; +#X connect 34 0 33 0; +#X connect 35 0 7 0; +#X connect 36 0 7 0; +#X connect 37 0 36 0; +#X connect 38 0 18 0; +#X connect 39 0 7 0; +#X restore 353 88 pd inittest; +#X obj 747 277 fudiparse; +#X obj 637 169 fudiformat; +#X obj 318 448 tgl 25 0 empty empty empty 17 7 0 10 #e4e4e4 #4d4d4d #373737 0 1; +#X obj 637 277 list split 5; +#X obj 658 311 print striped; +#X obj 637 88 inlet; +#X obj 353 132 comport -1 57600; +#X obj 444 236 route ports; +#X obj 19 46 vradio 25 1 0 5 empty empty empty 0 -8 0 10 #e4e4e4 #4d4d4d #373737 0; +#X obj 407 182 route ports; +#X msg 444 88 ports; +#X obj 444 272 list split 1; +#X obj 407 211 t b; +#X obj 463 438 coll; +#C restore; +#X obj 666 520 print index; +#X msg 476 324 0; +#X obj 463 366 f; +#X obj 503 366 + 1; +#X obj 444 324 t b; +#X obj 463 403 list append; +#X obj 426 553 s \$0-to_com; +#X obj 510 88 r \$0-to_com; +#X obj 426 487 symbol; +#X obj 426 438 t b; +#X msg 426 520 devicename \$1; +#X obj 579 368 del 100; +#X msg 581 403 0; +#X obj 444 58 loadbang; +#X obj 543 520 print devicename; +#X obj 747 311 print fudi; +#X obj 543 554 pack2 a a; +#X obj 543 586 format %d %s; +#X obj 543 617 any2symbol; +#X obj 49 46 cnv 15 88 125 empty \$0-connected 0\ COM7 10 12 0 14 #b7b7b7 #373737 0; +#X obj 543 679 s \$0-connected; +#X msg 543 649 label \$1; +#X connect 0 0 4 1; +#X connect 4 0 1 0; +#X connect 4 0 21 0; +#X connect 5 0 4 0; +#X connect 6 0 15 0; +#X connect 7 0 8 0; +#X connect 8 0 17 1; +#X connect 9 0 12 0; +#X connect 9 1 6 0; +#X connect 9 1 12 0; +#X connect 9 2 13 0; +#X connect 10 0 16 0; +#X connect 11 0 10 0; +#X connect 12 0 11 0; +#X connect 13 0 11 0; +#X connect 14 0 8 0; +#X connect 15 0 14 0; +#X connect 15 0 18 0; +#X connect 16 0 14 0; +#X connect 17 0 0 0; +#X connect 18 0 17 0; +#X connect 18 0 7 0; +#X connect 19 0 3 0; +#X connect 20 0 5 0; +#X connect 20 0 25 0; +#X connect 22 0 29 0; +#X connect 22 0 32 0; +#X connect 23 0 52 0; +#X connect 24 0 23 0; +#X connect 24 0 26 0; +#X connect 25 0 19 0; +#X connect 26 1 27 0; +#X connect 26 1 29 0; +#X connect 28 0 24 0; +#X connect 29 0 9 0; +#X connect 29 0 10 1; +#X connect 29 0 19 1; +#X connect 29 1 2 0; +#X connect 29 1 30 0; +#X connect 30 0 34 0; +#X connect 31 0 36 0; +#X connect 31 0 46 0; +#X connect 32 0 35 0; +#X connect 33 0 29 0; +#X connect 33 0 32 0; +#X connect 34 0 41 0; +#X connect 34 1 42 1; +#X connect 35 0 38 0; +#X connect 35 0 48 0; +#X connect 36 0 45 1; +#X connect 36 0 51 0; +#X connect 36 0 53 1; +#X connect 36 1 37 0; +#X connect 36 1 53 0; +#X connect 38 0 39 1; +#X connect 39 0 40 0; +#X connect 39 0 42 0; +#X connect 40 0 39 1; +#X connect 41 0 39 0; +#X connect 42 0 36 0; +#X connect 44 0 29 0; +#X connect 45 0 47 0; +#X connect 46 0 45 0; +#X connect 47 0 43 0; +#X connect 48 0 49 0; +#X connect 49 0 36 0; +#X connect 49 0 46 0; +#X connect 50 0 33 0; +#X connect 53 0 54 0; +#X connect 54 0 55 0; +#X connect 55 0 58 0; +#X connect 58 0 57 0; +#X coords 0 0 1 1 181 180 1 0 27; diff --git a/20241015/1015_n_serial_app/patch_1015_n_serial_app.pd b/20241015/1015_n_serial_app/patch_1015_n_serial_app.pd new file mode 100644 index 0000000..e56f23d --- /dev/null +++ b/20241015/1015_n_serial_app/patch_1015_n_serial_app.pd @@ -0,0 +1,31 @@ +#N struct 1060-point float x0 float y0 float xs float ys float fg float in float gridx float gridy; +#N canvas 827 239 527 327 12; +#X declare -path .; +#X obj 48 264 cnv 15 180 19 empty 2d_xy X:596\ Y:386 5 12 0 14 #ffffff #373737 0; +#X obj 47 299 unpack f f; +#X obj 47 326 i; +#X obj 93 326 i; +#X obj 48 85 slider2d 180 180 0 1023 0 1023 1 255 255 255 0 0 0 1 1 0 596.75 386.467 empty empty; +#X msg 163 49 jump 1; +#X obj 163 26 loadbang; +#X msg 47 443 label \$1; +#X obj 47 354 pack2 i i; +#X obj 47 414 any2symbol; +#X obj 47 384 format X:%d Y:%d; +#X floatatom 232 247 5 0 0 0 - - - 12; +#X floatatom 48 63 5 0 0 0 - - - 12; +#X obj 47 475 s 2d_xy; +#X obj 308 49 declare -path .; +#X obj 308 85 com_manager; +#X connect 1 0 2 0; +#X connect 1 1 3 0; +#X connect 2 0 8 0; +#X connect 3 0 8 1; +#X connect 4 0 1 0; +#X connect 5 0 4 0; +#X connect 6 0 5 0; +#X connect 7 0 13 0; +#X connect 8 0 10 0; +#X connect 8 0 15 0; +#X connect 9 0 7 0; +#X connect 10 0 9 0; diff --git a/20241015/1015_n_serial_app/sketch_1015_n_serial_app/sketch_1015_n_serial_app.pde b/20241015/1015_n_serial_app/sketch_1015_n_serial_app/sketch_1015_n_serial_app.pde new file mode 100644 index 0000000..3fb1eec --- /dev/null +++ b/20241015/1015_n_serial_app/sketch_1015_n_serial_app/sketch_1015_n_serial_app.pde @@ -0,0 +1,19 @@ +import processing.serial.*; + +Serial port; + +void setup() { + size(1023, 1023); + + println("Available serial ports:"); + println(Serial.list()); + + port = new Serial(this, Serial.list()[0], 57600); +} + +void draw() { + + String data = str(mouseX) + " " + str(mouseY) + ";\r"; + + port.write(data); +} diff --git a/20241015/fritzing/1015_1-2.fzz b/20241015/fritzing/1015_1-2.fzz new file mode 100644 index 0000000..f35e4ea Binary files /dev/null and b/20241015/fritzing/1015_1-2.fzz differ diff --git a/20241015/fritzing/1015_3.fzz b/20241015/fritzing/1015_3.fzz new file mode 100644 index 0000000..69d7080 Binary files /dev/null and b/20241015/fritzing/1015_3.fzz differ diff --git a/20241015/fritzing/1015_5.fzz b/20241015/fritzing/1015_5.fzz new file mode 100644 index 0000000..e9ce609 Binary files /dev/null and b/20241015/fritzing/1015_5.fzz differ