Created
June 24, 2019 07:25
-
-
Save likersacademia/b75fbe85fc1b69adb9cee2fbb2b334c0 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
// 距離センサー、LEDの組み合わせで、プログラミングを学ぶ | |
#define echoPin 19 | |
#define trigPin 18 | |
#define ledPin 5 | |
#define ON LOW | |
#define OFF HIGH | |
double Duration = 0; | |
double Distance = 0; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(ledPin, OUTPUT); | |
pinMode(echoPin,INPUT); | |
pinMode(trigPin,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"); | |
} | |
//プログラムはここからかえます// | |
if (Distance > 15 ) { //数字をシリアルモニターを参考にして数字をかえよう | |
digitalWrite(ledPin, ON); | |
delay(100); // 1) 数字をかえてみよう | |
digitalWrite(ledPin, OFF); | |
delay(100); // 1) 数字をかえてみよう | |
digitalWrite(ledPin, ON); | |
delay(100); // 1) 数字をかえてみよう | |
digitalWrite(ledPin, OFF); | |
delay(100); // 1) 数字をかえてみよう | |
} else { | |
digitalWrite(ledPin, OFF); | |
delay(200); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment