Last active
November 25, 2019 10:17
-
-
Save likersacademia/c80f3c09f30929f095163a32bd574301 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
//04_distance_led | |
//近づいたら1回だけ光る | |
//SchooMyとかいてあるところまですすもう | |
#define echoPin 19 | |
#define trigPin 18 | |
#define ledPin 10 //OUTB | |
#define ON LOW | |
#define OFF HIGH | |
int count = 0; | |
double Duration = 0; | |
double Distance = 0; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(echoPin, INPUT); | |
pinMode(trigPin, OUTPUT); | |
pinMode(ledPin, 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++; //カウントする | |
} else { //それ以外は | |
count = 0; //カウントをゼロにする | |
digitalWrite(ledPin, OFF); //光らない | |
} | |
if (count > 0 && count <= 1) { //この時間だけ | |
digitalWrite(ledPin, ON); //光る | |
} else if (count > 1) { //この時間は | |
digitalWrite(ledPin, OFF); //光らない | |
Serial.println(" Stay "); //表示する | |
} | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment