Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define TRIG 13
- #define ECHO 12
- void setup() {
- Serial.begin (9600);
- pinMode(TRIG, OUTPUT);
- pinMode(ECHO, INPUT);
- }
- void loop() {
- long duration, distance;
- digitalWrite(TRIG, LOW);
- delayMicroseconds(2);
- digitalWrite(TRIG, HIGH);
- delayMicroseconds(10);
- digitalWrite(TRIG, LOW);
- duration = pulseIn(ECHO, HIGH);
- distance = (duration/2) / 29.1;
- if (distance >= 400 || distance <= 0){
- Serial.println("Out of range");
- }
- else {
- Serial.print(distance);
- Serial.println(" cm");
- }
- delay(500);
- }
Add Comment
Please, Sign In to add comment