Skip to content

Instantly share code, notes, and snippets.

@yumu19
Created September 12, 2022 07:17
Show Gist options
  • Save yumu19/dfff31e242cf5ce834ffe9015af3aac6 to your computer and use it in GitHub Desktop.
Save yumu19/dfff31e242cf5ce834ffe9015af3aac6 to your computer and use it in GitHub Desktop.
#include<M5Stack.h>
// HC-SR04の値の読み出しと距離への変換は
// "超音波センサHC-SR04とArduinoで距離を測定する - Robo Station"
// https://rb-station.com/blogs/article/hc-sr04-arduino
// のサンプルコードを参考にしました
int TRIG = 22;
int ECHO = 35;
double duration = 0;
double distance = 0;
double speed_of_sound = 331.5 + 0.6 * 25; //25℃における音速
int count = 0;
bool flag = false;
double threshould = 100.0; //閾値の距離(cm)
void setup() {
Serial.begin(9600);
M5.begin();
M5.Power.begin();
M5.Lcd.setBrightness(255);
pinMode(ECHO, INPUT);
pinMode(TRIG, OUTPUT);
}
void loop() {
M5.update();
digitalWrite(TRIG,LOW);
delayMicroseconds(2);
digitalWrite(TRIG,HIGH);
delayMicroseconds(10);
digitalWrite(TRIG,LOW);
duration = pulseIn(ECHO,HIGH);
if (duration > 0) {
duration = duration / 2;
distance = duration * speed_of_sound * 100 / 1000000;
if ((distance < threshould) && !flag){
flag = true;
count++;
} else if ((distance > threshould)){
flag = false;
}
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(100, 100);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setTextSize(7);
M5.Lcd.print(count);
}
if(M5.BtnA.wasPressed())
{
count = 0;
M5.Lcd.clear();
}
delay(200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment