Created
October 19, 2019 10:05
-
-
Save likersacademia/55ac170a121eed4685292e7571307cc3 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
//設定した時間内に行われた動作をカウントし、条件を満たしていると音がなる | |
//30秒以内に人が5回以上通ったら、音が鳴る | |
#define echoPin 19 | |
#define trigPin 18 | |
#define SPEAKER 10 | |
double Duration = 0; | |
double Distance = 0; | |
#define do 262 | |
#define re 294 | |
#define mi 330 | |
#define fa 349 | |
#define so 392 | |
#define ra 440 | |
#define shi 494 | |
#define do2 523 | |
int count=0; | |
int stay=0; | |
int start_time=0; | |
int current_time=0; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(SPEAKER, OUTPUT); | |
pinMode(echoPin,INPUT); | |
pinMode(trigPin,OUTPUT); | |
} | |
long get_Distance (){ | |
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"); | |
} | |
delay(100); | |
} | |
long get_time(){ | |
if(stay==0){ | |
int num = millis(); | |
start_time = num / 1000; | |
stay++; | |
} | |
int now = millis(); | |
current_time = (now / 1000) - start_time; | |
} | |
void loop() { | |
get_Distance(); | |
get_time(); | |
if(current_time<=30){ //時間の設定 | |
if(Distance<20){ //カウントの設定 | |
noTone(SPEAKER) ; | |
delay(200); | |
count++; | |
Serial.print(count); | |
Serial.println("回"); | |
while(Distance<20){ | |
get_Distance(); | |
} | |
} | |
}else{ | |
Serial.println("結果"); | |
if(count>5){ | |
Serial.println("YES"); | |
tone(SPEAKER,do) ; //なる | |
delay(200); | |
noTone(SPEAKER); //ならない | |
delay(200); | |
count=0; //0に戻す | |
stay=0; //0に戻す | |
}else{ //そうでない場合 | |
Serial.println("NO"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment