人を検知して音楽を再生させたい
今回は、Arduino+赤外線センサーからシリアルとMaxに送信し、
センサーで動くものを検知したら音楽を再生し、人がいなくなってしばらく経過したら、
音楽を止めるというMaxスケッチを作ったのでメモしておく。
用意するもの
- Max
- Arduino UNO
- 赤外線センサー
- ケーブル3本
- ブレッドボード
配線
真ん中が信号線なので、Arduinoのデジタル2番PINに接続(緑色の線)。
あとは、5V(赤色)とGRD(黒色)を接続する。接続はシンプルですね。
※こちらの赤外線センサー、ブレッドボードに真っ直ぐ刺さらなかったので、ペンチで無理やり曲げました。
Arduino
2番ピンにモーションセンサーからの情報が0 - 1でやってくる。
赤外線センサーの信号が来てる時は、13番ピンでLEDが点灯するようにした。
int motionPin = 2;
int motionStatas;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
String message = "";
motionStatas = digitalRead(motionPin);
if (motionStatas == 1){
message = "status 1";
digitalWrite(13, HIGH);
} else {
message = "status 0";
digitalWrite(13, LOW);
}
Serial.println(message);
delay (200);
}
Max
シリアルを受け取れるようにして、ステータスに応じて、トグルスイッチを再生モードとスタンバイモードにするようにした。最後の検出からの経過時間に応じて、スタンバイモードになるように時間設定できるようにした。
以下をコピーして、Max画面に貼り付けると、今回のスケッチが再現されるはずです。
{
"boxes" : [ {
"box" : {
"maxclass" : "message",
"text" : "status 0",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"id" : "obj-127",
"patching_rect" : [ 90.25, 300.0, 81.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "1",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"id" : "obj-122",
"patching_rect" : [ 325.0, 45.0, 29.5, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "loadbang",
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "bang" ],
"id" : "obj-120",
"patching_rect" : [ 325.0, 15.0, 58.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "button",
"numinlets" : 1,
"numoutlets" : 1,
"parameter_enable" : 0,
"outlettype" : [ "bang" ],
"id" : "obj-117",
"patching_rect" : [ 79.0, 402.0, 24.0, 24.0 ]
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "シリアル受け取りの開始(ON/ OFF)",
"numinlets" : 1,
"numoutlets" : 0,
"id" : "obj-115",
"patching_rect" : [ 392.0, 41.0, 205.0, 20.0 ]
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "一度だけbangを送る",
"numinlets" : 1,
"numoutlets" : 0,
"id" : "obj-113",
"patching_rect" : [ 11.0, 462.0, 139.0, 20.0 ]
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "1",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"id" : "obj-111",
"patching_rect" : [ 19.5, 488.0, 29.5, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "toggle",
"numinlets" : 1,
"numoutlets" : 1,
"parameter_enable" : 0,
"outlettype" : [ "int" ],
"id" : "obj-107",
"patching_rect" : [ 19.5, 518.0, 24.0, 24.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "gate 1 1",
"fontsize" : 12.0,
"numinlets" : 2,
"fontname" : "Arial",
"numoutlets" : 1,
"outlettype" : [ "" ],
"id" : "obj-8",
"patching_rect" : [ 61.5, 488.0, 54.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "t b 0",
"fontsize" : 12.0,
"numinlets" : 1,
"fontname" : "Arial",
"numoutlets" : 2,
"outlettype" : [ "bang", "int" ],
"id" : "obj-108",
"patching_rect" : [ 61.5, 512.0, 34.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "dac~",
"numinlets" : 2,
"numoutlets" : 0,
"id" : "obj-102",
"patching_rect" : [ 61.5, 798.0, 35.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "playlist~",
"pitchcorrection" : 0,
"numinlets" : 1,
"followglobaltempo" : 0,
"quality" : "basic",
"timestretch" : [ 0 ],
"numoutlets" : 5,
"formantcorrection" : 0,
"parameter_enable" : 0,
"mode" : "basic",
"outlettype" : [ "signal", "signal", "signal", "", "dictionary" ],
"originallength" : [ 0.0, "ticks" ],
"originaltempo" : 120.0,
"id" : "obj-98",
"basictuning" : 440,
"patching_rect" : [ 61.5, 759.0, 150.0, 30.0 ],
"data" : {
"clips" : [ {
"absolutepath" : "drumLoop.aif",
"filename" : "drumLoop.aif",
"filekind" : "audiofile",
"id" : "u553010606",
"loop" : 1,
"content_state" : {
"loop" : 1
}
}
]
}
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "このスケッチでは、\n赤外線センサーからの\nデータを取得して\n人がセンサーの前にいる時だけボタンが\nアクティブになるようにしたものです",
"linecount" : 5,
"numinlets" : 1,
"numoutlets" : 0,
"id" : "obj-96",
"patching_rect" : [ 11.0, 6.0, 244.0, 72.0 ]
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "5",
"presentation_rect" : [ 429.0, 304.0, 29.5, 22.0 ],
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"presentation" : 1,
"id" : "obj-92",
"patching_rect" : [ 463.5, 247.0, 29.5, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "3",
"presentation_rect" : [ 389.5, 304.0, 29.5, 22.0 ],
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"presentation" : 1,
"id" : "obj-90",
"patching_rect" : [ 424.0, 247.0, 29.5, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "* 60",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "int" ],
"id" : "obj-88",
"patching_rect" : [ 392.0, 330.0, 30.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "1",
"presentation_rect" : [ 357.5, 304.0, 29.5, 22.0 ],
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"presentation" : 1,
"id" : "obj-87",
"patching_rect" : [ 392.0, 247.0, 29.5, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "* 1000",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "int" ],
"id" : "obj-78",
"patching_rect" : [ 392.0, 360.0, 43.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "number",
"presentation_rect" : [ 357.5, 342.0, 50.0, 22.0 ],
"numinlets" : 1,
"numoutlets" : 2,
"parameter_enable" : 0,
"outlettype" : [ "", "bang" ],
"presentation" : 1,
"id" : "obj-73",
"patching_rect" : [ 392.0, 285.0, 50.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "検出が無くなってから\nスタンバイモードに\n切り替えるまでの時間の設定\n(分)",
"linecount" : 4,
"presentation_linecount" : 4,
"presentation_rect" : [ 357.5, 242.0, 169.0, 60.0 ],
"numinlets" : 1,
"numoutlets" : 0,
"presentation" : 1,
"id" : "obj-71",
"patching_rect" : [ 392.0, 185.0, 169.0, 60.0 ]
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "最後の検出から経過した時間(sec)",
"presentation_linecount" : 2,
"presentation_rect" : [ 337.0, 523.5, 164.0, 33.0 ],
"numinlets" : 1,
"numoutlets" : 0,
"presentation" : 1,
"id" : "obj-69",
"patching_rect" : [ 295.75, 618.5, 260.0, 20.0 ]
}
}
, {
"box" : {
"maxclass" : "flonum",
"presentation_rect" : [ 339.0, 545.5, 80.0, 22.0 ],
"format" : 6,
"numinlets" : 1,
"numoutlets" : 2,
"parameter_enable" : 0,
"outlettype" : [ "", "bang" ],
"presentation" : 1,
"id" : "obj-67",
"patching_rect" : [ 299.75, 642.5, 80.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "/ 1000.",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "float" ],
"id" : "obj-65",
"patching_rect" : [ 299.75, 591.0, 45.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "timer",
"numinlets" : 2,
"numoutlets" : 2,
"outlettype" : [ "float", "" ],
"id" : "obj-62",
"patching_rect" : [ 299.75, 563.0, 35.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "1: 再生モード\n0: スタンバイモード",
"linecount" : 2,
"presentation_linecount" : 2,
"presentation_rect" : [ 140.0, 534.5, 150.0, 33.0 ],
"numinlets" : 1,
"numoutlets" : 0,
"presentation" : 1,
"id" : "obj-61",
"patching_rect" : [ 94.0, 667.0, 150.0, 33.0 ]
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "0",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"id" : "obj-59",
"patching_rect" : [ 100.5, 606.5, 29.5, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "message",
"text" : "1",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "" ],
"id" : "obj-57",
"patching_rect" : [ 61.5, 606.5, 29.5, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "delay 10000",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "bang" ],
"id" : "obj-54",
"patching_rect" : [ 100.5, 577.0, 74.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "toggle",
"presentation_rect" : [ 106.0, 539.0, 24.0, 24.0 ],
"numinlets" : 1,
"numoutlets" : 1,
"parameter_enable" : 0,
"outlettype" : [ "int" ],
"presentation" : 1,
"id" : "obj-53",
"patching_rect" : [ 61.5, 667.0, 24.0, 24.0 ]
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "人がいないとき",
"presentation_linecount" : 2,
"presentation_rect" : [ 188.5, 382.0, 91.0, 32.0 ],
"numinlets" : 1,
"numoutlets" : 0,
"presentation" : 1,
"id" : "obj-47",
"patching_rect" : [ 127.5, 402.0, 110.0, 20.0 ]
}
}
, {
"box" : {
"maxclass" : "comment",
"text" : "人がいる時",
"numinlets" : 1,
"numoutlets" : 0,
"id" : "obj-46",
"patching_rect" : [ 11.0, 402.0, 91.0, 20.0 ]
}
}
, {
"box" : {
"maxclass" : "button",
"presentation_rect" : [ 188.5, 404.0, 24.0, 24.0 ],
"numinlets" : 1,
"numoutlets" : 1,
"parameter_enable" : 0,
"outlettype" : [ "bang" ],
"presentation" : 1,
"id" : "obj-40",
"patching_rect" : [ 220.0, 402.0, 24.0, 24.0 ]
}
}
, {
"box" : {
"maxclass" : "button",
"numinlets" : 1,
"numoutlets" : 1,
"parameter_enable" : 0,
"outlettype" : [ "bang" ],
"id" : "obj-38",
"patching_rect" : [ 61.5, 542.0, 24.0, 24.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "sel 1 0",
"numinlets" : 3,
"numoutlets" : 3,
"outlettype" : [ "bang", "bang", "" ],
"id" : "obj-36",
"patching_rect" : [ 11.0, 360.0, 44.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "route status",
"numinlets" : 2,
"numoutlets" : 2,
"outlettype" : [ "", "" ],
"id" : "obj-35",
"patching_rect" : [ 11.0, 300.0, 71.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "number",
"numinlets" : 1,
"numoutlets" : 2,
"parameter_enable" : 0,
"outlettype" : [ "", "bang" ],
"id" : "obj-18",
"patching_rect" : [ 79.0, 360.0, 51.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "button",
"numinlets" : 1,
"numoutlets" : 1,
"parameter_enable" : 0,
"outlettype" : [ "bang" ],
"id" : "obj-10",
"patching_rect" : [ 319.5, 141.0, 24.0, 24.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "metro 100",
"numinlets" : 2,
"numoutlets" : 1,
"outlettype" : [ "bang" ],
"id" : "obj-17",
"patching_rect" : [ 399.0, 123.0, 63.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "toggle",
"numinlets" : 1,
"numoutlets" : 1,
"parameter_enable" : 0,
"outlettype" : [ "int" ],
"id" : "obj-16",
"patching_rect" : [ 399.0, 62.5, 42.5, 42.5 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "fromsymbol",
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "" ],
"id" : "obj-6",
"patching_rect" : [ 11.0, 272.0, 71.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "itoa",
"numinlets" : 3,
"numoutlets" : 1,
"outlettype" : [ "int" ],
"id" : "obj-5",
"patching_rect" : [ 11.0, 241.0, 40.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "zl group 100",
"numinlets" : 2,
"numoutlets" : 2,
"outlettype" : [ "", "" ],
"id" : "obj-4",
"patching_rect" : [ 11.0, 214.0, 75.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "sel 13 10",
"numinlets" : 3,
"numoutlets" : 3,
"outlettype" : [ "bang", "bang", "" ],
"id" : "obj-3",
"patching_rect" : [ 11.0, 185.0, 57.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "newobj",
"text" : "serial b 9600",
"numinlets" : 1,
"numoutlets" : 2,
"outlettype" : [ "int", "" ],
"id" : "obj-1",
"patching_rect" : [ 11.0, 154.0, 77.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "attrui",
"presentation_rect" : [ 69.0, 43.0, 292.0, 22.0 ],
"attr" : "serport",
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "" ],
"presentation" : 1,
"id" : "obj-2",
"patching_rect" : [ 11.0, 113.0, 292.0, 22.0 ]
}
}
, {
"box" : {
"maxclass" : "attrui",
"presentation_rect" : [ 69.0, 13.0, 150.0, 22.0 ],
"attr" : "baud",
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "" ],
"presentation" : 1,
"id" : "obj-94",
"patching_rect" : [ 11.0, 83.0, 150.0, 22.0 ]
}
}
],
"lines" : [ {
"patchline" : {
"source" : [ "obj-6", 0 ],
"destination" : [ "obj-127", 1 ],
"order" : 0
}
}
, {
"patchline" : {
"source" : [ "obj-107", 0 ],
"destination" : [ "obj-8", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-108", 0 ],
"destination" : [ "obj-107", 0 ],
"order" : 1
}
}
, {
"patchline" : {
"source" : [ "obj-111", 0 ],
"destination" : [ "obj-107", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-122", 0 ],
"destination" : [ "obj-16", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-120", 0 ],
"destination" : [ "obj-122", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-117", 0 ],
"destination" : [ "obj-8", 1 ]
}
}
, {
"patchline" : {
"source" : [ "obj-36", 0 ],
"destination" : [ "obj-117", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-108", 0 ],
"destination" : [ "obj-38", 0 ],
"order" : 0
}
}
, {
"patchline" : {
"source" : [ "obj-40", 0 ],
"destination" : [ "obj-111", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-8", 0 ],
"destination" : [ "obj-108", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-98", 0 ],
"destination" : [ "obj-102", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-53", 0 ],
"destination" : [ "obj-98", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-94", 0 ],
"destination" : [ "obj-1", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-92", 0 ],
"destination" : [ "obj-73", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-90", 0 ],
"destination" : [ "obj-73", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-87", 0 ],
"destination" : [ "obj-73", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-88", 0 ],
"destination" : [ "obj-78", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-73", 0 ],
"destination" : [ "obj-88", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-78", 0 ],
"destination" : [ "obj-54", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-10", 0 ],
"destination" : [ "obj-62", 1 ],
"order" : 0
}
}
, {
"patchline" : {
"source" : [ "obj-65", 0 ],
"destination" : [ "obj-67", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-62", 0 ],
"destination" : [ "obj-65", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-38", 0 ],
"destination" : [ "obj-62", 0 ],
"order" : 0
}
}
, {
"patchline" : {
"source" : [ "obj-38", 0 ],
"destination" : [ "obj-54", 0 ],
"order" : 1
}
}
, {
"patchline" : {
"source" : [ "obj-54", 0 ],
"destination" : [ "obj-59", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-59", 0 ],
"destination" : [ "obj-53", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-57", 0 ],
"destination" : [ "obj-53", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-38", 0 ],
"destination" : [ "obj-57", 0 ],
"order" : 2
}
}
, {
"patchline" : {
"source" : [ "obj-36", 1 ],
"destination" : [ "obj-40", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-35", 0 ],
"destination" : [ "obj-36", 0 ],
"order" : 1
}
}
, {
"patchline" : {
"source" : [ "obj-6", 0 ],
"destination" : [ "obj-35", 0 ],
"order" : 1
}
}
, {
"patchline" : {
"source" : [ "obj-35", 0 ],
"destination" : [ "obj-18", 0 ],
"order" : 0
}
}
, {
"patchline" : {
"source" : [ "obj-3", 2 ],
"destination" : [ "obj-4", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-1", 0 ],
"destination" : [ "obj-3", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-3", 0 ],
"destination" : [ "obj-4", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-16", 0 ],
"destination" : [ "obj-17", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-17", 0 ],
"destination" : [ "obj-10", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-10", 0 ],
"destination" : [ "obj-1", 0 ],
"order" : 1
}
}
, {
"patchline" : {
"source" : [ "obj-2", 0 ],
"destination" : [ "obj-1", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-4", 0 ],
"destination" : [ "obj-5", 0 ]
}
}
, {
"patchline" : {
"source" : [ "obj-5", 0 ],
"destination" : [ "obj-6", 0 ]
}
}
],
"appversion" : {
"major" : 8,
"minor" : 2,
"revision" : 2,
"architecture" : "x64",
"modernui" : 1
}
,
"classnamespace" : "box"
}
まとめ
赤外線センサーで人を感知して、メディアを再生するというのは、
結構汎用的なので、今後、展示やパフォーマンスとかでも何かに役立ちそうな気がします。