p5js:13.grove_beginner_kit

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン 前のリビジョン
p5js:13.grove_beginner_kit [2022/07/05 13:11] – [加速度センサ] babap5js:13.grove_beginner_kit [2022/07/05 14:18] (現在) – [加速度センサ] baba
行 486: 行 486:
 } }
 </file> </file>
 +
 +===== おまけ(マイク入力で丸の大きさも変化させる) =====
 +<WRAP group>
 +<WRAP half column>
 +<file .pde arduino.pde>
 +//Gravity Acceleration
 +#include "LIS3DHTR.h"
 +#include <Wire.h>
 +
 +LIS3DHTR<TwoWire> LIS;           //Hardware I2C
 +#define WIRE Wire
 +void setup() {
 +  Serial.begin(9600);
 +  while (!Serial) {};
 +  LIS.begin(WIRE, 0x19); //IIC init
 +  delay(100);
 +  LIS.setOutputDataRate(LIS3DHTR_DATARATE_50HZ);
 +}
 +void loop() {
 +  if (!LIS) {
 +    Serial.println("LIS3DHTR didn't connect.");
 +    while (1);
 +    return;
 +  }
 +  //3 axis
 +  Serial.print(LIS.getAccelerationX()); Serial.print(",");
 +  Serial.print(LIS.getAccelerationY()); Serial.print(",");
 +  Serial.print(LIS.getAccelerationZ()); Serial.print(",");
 +  Serial.println(analogRead(2));
 +  delay(30);
 +}
 +</file>
 +</WRAP>
 +
 +<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, 400);
 +}
 +
 +var ball = {
 +  x:0,
 +  y:0,
 +  r:0
 +}
 +
 +function draw() {
 +  background(220);
 +  textSize(18);
 +  textAlign(CENTER, CENTER);
 +  text(`${x.toFixed(2)}, ${y.toFixed(2)}, ${z.toFixed(2)}`, width/2, height/2);
 +  
 +  circle(ball.x, ball.y, ball.r/10);
 +
 +}
 + 
 +function gotSerialValues(values) {
 +  for( let i = 0; i < values.length; i++ ){
 +     serial_values.push(values[i]);
 +    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;
 +      
 +    }
 +  } 
 +}
 +</file>
 +</WRAP>
 +</WRAP>
 +
 +
  • /home/users/2/lolipop.jp-4404d470cd64c603/web/ws/data/pages/p5js/13.grove_beginner_kit.txt
  • 最終更新: 2022/07/05 14:18
  • by baba