Skip to content

Instantly share code, notes, and snippets.

@rafalw
Created March 11, 2020 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafalw/b0d2cbe9ccc658d57373e1c910886b22 to your computer and use it in GitHub Desktop.
Save rafalw/b0d2cbe9ccc658d57373e1c910886b22 to your computer and use it in GitHub Desktop.
Obsługa czujnika HC-SR04
#include <LiquidCrystal.h>
const int trigPin = 8;
const int echoPin = 9;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
lcd.begin(16, 2);
lcd.noCursor();
lcd.setCursor(0, 0);
pinMode(trigPin, OUTPUT); //Pin, do którego podłączymy trig jako wyjście
pinMode(echoPin, INPUT); //a echo, jako wejście
}
void loop() {
long czas, dystans;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
czas = pulseIn(echoPin, HIGH);
dystans = czas / 58;
lcd.clear();
lcd.print(dystans);
lcd.print(" cm");
delay(2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment