Last active
November 25, 2019 10:21
-
-
Save likersacademia/9ff769c2a482e48fefafa2aae994df8a 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
//05_distance_led | |
//近づいた回数をカウントして5回ごとに、光る | |
//SchooMyとかいてあるところまですすもう | |
#define echoPin 19 | |
#define trigPin 18 | |
#define ledPin 10 //OUTB | |
#define ON LOW | |
#define OFF HIGH | |
double Duration = 0; //受信した間隔 | |
double Distance = 0; //距離 | |
int count = 0; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(echoPin, INPUT); | |
pinMode(trigPin, OUTPUT); | |
pinMode(ledPin, 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); | |
} | |
void loop() { | |
get_Distance(); | |
//SchooMy | |
//プログラムはここからかえます | |
if (Distance < 20) { //このエリアになったら | |
count++; //カウントする | |
Serial.print(count); //表示する | |
Serial.println("回"); //表示する | |
if (count == 5) { //この数になったら | |
digitalWrite(ledPin, ON); //光る | |
count = 0; //カウントをゼロにする | |
} | |
while (Distance < 20) { | |
get_Distance(); | |
} | |
} else { //それ以外は | |
digitalWrite(ledPin, OFF); //光らない | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment