Created
October 19, 2019 09:57
-
-
Save likersacademia/2b2fab1b21cf9ca9a1098d6cbfd6c85c 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 ledPin 10 | |
#define ON LOW | |
#define OFF HIGH | |
double Duration = 0; | |
double Distance = 0; | |
int count=0; | |
int stay=0; | |
int start_time=0; | |
int current_time=0; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(ledPin, 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){ //カウントの設定 | |
digitalWrite(ledPin, OFF); | |
delay(200); | |
count++; | |
Serial.print(count); | |
Serial.println("回"); | |
while(Distance<20){ | |
get_Distance(); | |
} | |
} | |
}else{ | |
Serial.println("結果"); | |
if(count>5){ //5回より多い場合 | |
Serial.println("OK"); | |
digitalWrite(ledPin, ON); //光る | |
delay(200); | |
digitalWrite(ledPin, OFF); //消える | |
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