0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GR-Citrusでセンサーで音を出そう

Posted at

GR-Citrusで音を出そうの続きです。
https://qiita.com/usashirou/items/5e0775653cf54740d092

今回は、以前使った近接センサーを使って音を鳴らしてみましょう
センサーの範囲内に入ると音が鳴るというシステムです。

ただし、困ったことがあります。
アンプをつなげると、ノイズ音が入ってしまうのです。
このために、動作していない時の方が気に障るという最低な仕様となってしまいました・・・

# include <Arduino.h>
# include <WavMp3p.h>
# define trigPin 8
# define echoPin 9

WavMp3p wavmp3p(44100);

void setup()
{
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
}

void loop()
{
	 int duration, distance;
	digitalWrite(trigPin, HIGH);
	delayMicroseconds(1000);
	digitalWrite(trigPin, LOW);
	duration = pulseIn(echoPin, HIGH);
 	distance = (duration/2) / 29.1;
 
 if (distance >= 200 || distance <= 0){
	Serial.println("Out of range");
 } else {
	wavmp3p.play("audio2.mp3");
	Serial.print(distance);
	Serial.println(" cm");
 }
 delay(500);
}

else以下で動作するようになっており
if (distance >= 200 || distance <= 0)
を変更すれば、反応する範囲を変更可能です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?