Last active
November 25, 2019 10:11
-
-
Save likersacademia/9ecd0de464906be36e47058e799d8d02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//03_distance_speaker | |
//近づいて5秒後に音が鳴り、その前に離れたら鳴らない | |
//SchooMyとかいてあるところまですすもう | |
#define echoPin 19 | |
#define trigPin 18 | |
#define SPEAKER 10 | |
#define do 262 | |
#define re 294 | |
#define mi 330 | |
#define fa 349 | |
#define so 392 | |
#define ra 440 | |
#define shi 494 | |
#define do2 523 | |
double Duration = 0; | |
double Distance = 0; | |
int count = 0; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(echoPin, INPUT); | |
pinMode(trigPin, OUTPUT); | |
pinMode(SPEAKER, OUTPUT); | |
} | |
void loop() { | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); //超音波を出力 | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
Duration = pulseIn(echoPin, HIGH); //センサからの入力 | |
if (Duration > 0) { | |
Duration = Duration / 2; //往復距離を半分 | |
Distance = Duration * 340 * 100 / 1000000; //音速を340m/sに設定 | |
Serial.print("Distance:"); | |
Serial.print(Distance); | |
Serial.println("cm"); | |
} | |
//SchooMy | |
//プログラムはここからかえます | |
if (Distance < 10) { //このエリアになったら | |
count++; //カウントする | |
Serial.print(count); //表示する | |
Serial.println("秒"); //表示する | |
} else { | |
count = 0; //カウントをゼロにする | |
noTone(SPEAKER); //鳴らない | |
delay(20); //待つ | |
} | |
if (count >= 5) { //この時間がたったら | |
tone(SPEAKER, do); //鳴る | |
delay(20); //待つ | |
} else { | |
noTone(SPEAKER); //鳴らない | |
delay(20); //待つ | |
} | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment