Guest User

Untitled

a guest
Jun 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. #include <M5Stack.h>
  2. int trig = 22; // Trig
  3. int echo = 21; // Echo
  4. double duration = 0;
  5. double distance = 0;
  6.  
  7. // the setup routine runs once when M5Stack starts up
  8. void setup() {
  9. pinMode(trig,OUTPUT);
  10. pinMode(echo,INPUT);
  11.  
  12. // initialize the M5Stack object
  13. M5.begin();
  14. // text print
  15. M5.Lcd.fillScreen(BLACK);
  16. M5.Lcd.setCursor(0, 0);
  17. M5.Lcd.setTextColor(WHITE);
  18. M5.Lcd.setTextSize(4);
  19.  
  20. }
  21.  
  22. // the loop routine runs over and over again forever
  23. void loop(){
  24. digitalWrite(trig,LOW);
  25. delayMicroseconds(1);
  26. digitalWrite(trig,HIGH);
  27. delayMicroseconds(11);
  28. digitalWrite(trig,LOW);
  29. duration = pulseIn(echo,HIGH);
  30. distance = duration*0.017;// = 340 * 100 / 1000000 / 2
  31. M5.Lcd.setCursor(0, 0);
  32. M5.Lcd.fillScreen(BLACK);
  33. M5.Lcd.printf("%2.2f cm\n",distance);
  34. delay(500);
  35. M5.update();
  36. }
Add Comment
Please, Sign In to add comment