Last active
November 25, 2019 10:28
-
-
Save likersacademia/23f5bf098b44f1d9b0b605e1799ca556 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
//06_distance_led | |
//速さを測定して速かったら、光る | |
//SchooMyとかいてあるところまですすもう | |
#define echoPin 19 | |
#define trigPin 18 | |
#define ledPin 10 | |
#define ON LOW | |
#define OFF HIGH | |
double Duration = 0; //受信した間隔 | |
double Distance1 = 0; //距離 | |
double Distance2 = 0; | |
double Speed = 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; //往復距離を半分 | |
Distance1 = Duration * 340 * 100 / 1000000; //音速を340m/sに設定 | |
//Serial.print("スタート:"); | |
//Serial.print(Distance1); | |
//Serial.println("cm"); | |
} | |
delay(200); | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); //超音波を出力 | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
Duration = pulseIn(echoPin, HIGH); //センサからの入力 | |
if (Duration > 0) { | |
Duration = Duration / 2; //往復距離を半分 | |
Distance2 = Duration * 340 * 100 / 1000000; //音速を340m/sに設定 | |
//Serial.print("フィニッシュ:"); | |
//Serial.print(Distance2); | |
//Serial.println("cm"); | |
} | |
delay(100); | |
Speed = (Distance1 - Distance2) * 5; | |
Serial.print("Speed:"); | |
Serial.print(Speed); | |
Serial.println("cm/s"); | |
delay(10); | |
//SchooMy | |
//プログラムはここからかえます | |
if ((abs(Speed) > 20) && (abs(Speed) < 100)) { //この速さの間の時は | |
digitalWrite(ledPin, ON); //光る | |
delay(20); //待つ | |
} else { //それ以外は | |
digitalWrite(ledPin, OFF); //光らない | |
delay(20); //待つ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment