Skip to content

Instantly share code, notes, and snippets.

@swanmatch
Last active August 22, 2021 05:13
Show Gist options
  • Save swanmatch/86e446e542704e1b9db50bc05831f979 to your computer and use it in GitHub Desktop.
Save swanmatch/86e446e542704e1b9db50bc05831f979 to your computer and use it in GitHub Desktop.
get Distance
volatile int distance;
// パルスのエコーで距離を求める
float checkdistance_8_7() {
digitalWrite(8, LOW);
delayMicroseconds(2);
digitalWrite(8, HIGH);
delayMicroseconds(10);
digitalWrite(8, LOW);
float distance = pulseIn(7, HIGH) / 58.00;
delay(10);
return distance;
}
void setup() {
distance = 0;
// Trig
pinMode(8, OUTPUT);
// Echo
pinMode(7, INPUT);
// LED
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
distance = checkdistance_8_7();
Serial.println(distance);
if (distance < 50) {
// 密だったらLED点灯
digitalWrite(LED_BUILTIN, HIGH);
} else {
// 密じゃなかったらLED消灯
digitalWrite(LED_BUILTIN, LOW);
}
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment