差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
p5js:13.grove_beginner_kit [2022/07/05 00:55] – [加速度センサ] baba | p5js:13.grove_beginner_kit [2022/07/05 14:18] (現在) – [加速度センサ] baba | ||
---|---|---|---|
行 309: | 行 309: | ||
//Gravity Acceleration | //Gravity Acceleration | ||
#include " | #include " | ||
+ | #include < | ||
- | #include < | ||
LIS3DHTR< | LIS3DHTR< | ||
#define WIRE Wire | #define WIRE Wire | ||
- | |||
- | |||
void setup() { | void setup() { | ||
Serial.begin(9600); | Serial.begin(9600); | ||
行 332: | 行 330: | ||
Serial.print(LIS.getAccelerationY()); | Serial.print(LIS.getAccelerationY()); | ||
Serial.println(LIS.getAccelerationZ()); | Serial.println(LIS.getAccelerationZ()); | ||
- | |||
delay(500); | delay(500); | ||
} | } | ||
行 429: | 行 426: | ||
Serial.println(LIS.getAccelerationZ()); | Serial.println(LIS.getAccelerationZ()); | ||
- | delay(50); | + | delay(30); |
} | } | ||
行 436: | 行 433: | ||
</ | </ | ||
- | Arduino側のコードはdelayを50[ms]に変更しただけです。少しむずかしいと思いますが、じっくり読んでアルゴリズムを理解しましょう。 | + | Arduino側のコードはdelayを30[ms]に変更しただけです。少しむずかしいと思いますが、じっくり読んでアルゴリズムを理解しましょう。 |
さあ、それではセンサデータがしっかりと取得できたのでこの値で画面上のボールを転がしてみます。400x400のキャンバス上の真ん中からスタートし、x, | さあ、それではセンサデータがしっかりと取得できたのでこの値で画面上のボールを転がしてみます。400x400のキャンバス上の真ん中からスタートし、x, | ||
行 489: | 行 486: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ===== おまけ(マイク入力で丸の大きさも変化させる) ===== | ||
+ | <WRAP group> | ||
+ | <WRAP half column> | ||
+ | <file .pde arduino.pde> | ||
+ | //Gravity Acceleration | ||
+ | #include " | ||
+ | #include < | ||
+ | |||
+ | LIS3DHTR< | ||
+ | #define WIRE Wire | ||
+ | void setup() { | ||
+ | Serial.begin(9600); | ||
+ | while (!Serial) {}; | ||
+ | LIS.begin(WIRE, | ||
+ | delay(100); | ||
+ | LIS.setOutputDataRate(LIS3DHTR_DATARATE_50HZ); | ||
+ | } | ||
+ | void loop() { | ||
+ | if (!LIS) { | ||
+ | Serial.println(" | ||
+ | while (1); | ||
+ | return; | ||
+ | } | ||
+ | //3 axis | ||
+ | Serial.print(LIS.getAccelerationX()); | ||
+ | Serial.print(LIS.getAccelerationY()); | ||
+ | Serial.print(LIS.getAccelerationZ()); | ||
+ | Serial.println(analogRead(2)); | ||
+ | delay(30); | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <WRAP half column> | ||
+ | <file .js sketch.js> | ||
+ | var serial_values = []; | ||
+ | var serial = new Serial(); | ||
+ | var x=0; | ||
+ | var y=0; | ||
+ | var z=0; | ||
+ | var mic = 0; | ||
+ | |||
+ | function setup() { | ||
+ | createCanvas(400, | ||
+ | } | ||
+ | |||
+ | var ball = { | ||
+ | x:0, | ||
+ | y:0, | ||
+ | r:0 | ||
+ | } | ||
+ | |||
+ | function draw() { | ||
+ | background(220); | ||
+ | textSize(18); | ||
+ | textAlign(CENTER, | ||
+ | text(`${x.toFixed(2)}, | ||
+ | | ||
+ | circle(ball.x, | ||
+ | |||
+ | } | ||
+ | |||
+ | function gotSerialValues(values) { | ||
+ | for( let i = 0; i < values.length; | ||
+ | | ||
+ | if( values[i] == 10){ | ||
+ | let result = ""; | ||
+ | for( s of serial_values){ | ||
+ | result += String.fromCharCode(s); | ||
+ | } | ||
+ | const splits = result.split(',' | ||
+ | x = parseFloat(splits[0]); | ||
+ | y = parseFloat(splits[1]); | ||
+ | z = parseFloat(splits[2]); | ||
+ | mic = parseFloat(splits[3]); | ||
+ | serial_values = []; | ||
+ | | ||
+ | ball.x -= y*10; | ||
+ | ball.y -= x*10; | ||
+ | ball.r = mic; | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ |