プロジェクト26 人体ピアノ
1. 説明
アナログピアノは、開発ボードと超音波センサーを含みます。指の位置を検出することで異なる音色を演奏することができます。したがって、このモジュールはピアノを刺激して音楽や曲を演奏することが可能です。
2. フローチャート

3. 配線図

4. テストコード
/*
keyestudio ESP32 Inventor Learning Kit
Project 26 Human Body Piano
http://www.keyestudio.com
*/
int distance = 0; //Define a variable to receive the distance
int EchoPin = 14; //Connect Echo pin to io14
int TrigPin = 13; //Connect Trig pin to io13
int beeppin = 5;
float checkdistance() { //Acquire distance
// preserve a short low level to ensure a clear high pulse:
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
// Trigger the sensor by a high pulse of 10um or longer
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
// Read the signal from the sensor: a high level pulse
//Duration is detected from the point sending "ping" command to the time receiving echo signal (unit: um).
float distance = pulseIn(EchoPin, HIGH) / 58.00; //Convert into distance
delay(10);
return distance;
}
void setup()
{
Serial.begin(9600);//Set the baud rate to 9600
pinMode(TrigPin, OUTPUT);//Set Trig pin to output
pinMode(EchoPin, INPUT); //Set Echo pin to input
}
void loop()
{
distance = checkdistance();
if(distance < 10)
{
tone(beeppin, 262);//Play DO
delay(1000);
}
if(distance < 20 && distance > 10)
{
tone(beeppin, 294);//Play Re
delay(1000);
}
if(distance < 30 && distance > 20)
{
tone(beeppin, 330);//Play Mi
delay(1000);
}
if(distance < 40 && distance > 30)
{
tone(beeppin, 349);//Play fa
delay(1000);
}
if(distance < 50 && distance > 40)
{
tone(beeppin, 392);//Play So
delay(1000);
}
if(distance < 60 && distance > 50){
tone(beeppin, 440);//Play La
delay(1000);
}
if(distance < 70 && distance > 60)
{
tone(beeppin, 494);//Play Si
delay(1000);
}
Serial.println(distance);
noTone(beeppin);//Stop
}
5. テスト結果
配線を接続し、コードをアップロードしてください。
距離が10未満のときにDoを演奏します。
距離が10〜20の範囲内のときにReを演奏します。
距離が20〜30の範囲内のときにMiを演奏します。
距離が30〜40の範囲内のときにFaを演奏します。
距離が40〜50の範囲内のときにSoを演奏します。
距離が50〜60の範囲内のときにLaを演奏します。
距離が60〜70の範囲内のときにSiを演奏します。