Arduino 8 - ultrazvuk

From DT^2
Revision as of 10:14, 30 July 2019 by Palo (talk | contribs) (Created page with "<syntaxhighlight lang="C++"> #define TRIG 4 #define ECHO 5 void setup() { Serial.begin(9600); pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT); } int meraj() { digitalWri...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
#define TRIG 4
#define ECHO 5

void setup() {
  Serial.begin(9600);
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
}

int meraj() {
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  while (digitalRead(ECHO) == 0) { }
  long cas1 = micros();
  while (digitalRead(ECHO) == 1) { }
  long cas2 = micros();
  return (cas2 - cas1) / 58;
}

void loop() {
  Serial.println(meraj());
  delay(200);  
}