Created
May 26, 2015 12:23
-
-
Save dodops/f60f90f8258ab64c1554 to your computer and use it in GitHub Desktop.
Arduino ultrasonic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Ultrasonic.h> | |
#define echoPin 13 | |
#define triPin 12 | |
#define ledPin 9 | |
Ultrasonic ultrasonic(12,13); | |
void setup() | |
{ | |
Serial.begin(9000); | |
pinMode(echoPin, INPUT); | |
pinMode(triPin, OUTPUT); | |
} | |
void loop() | |
{ | |
digitalWrite(ledPin, LOW); | |
int val = method_distancia(); | |
if(val <= 15) | |
{ | |
digitalWrite(ledPin, HIGH); | |
delay(100); | |
} | |
delay(500); | |
} | |
int method_distancia() | |
{ | |
int distancia = (ultrasonic.Ranging(CM)); | |
digitalWrite(triPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(triPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(triPin, LOW); | |
Serial.print("Distância em CM: "); | |
Serial.println(distancia); | |
return distancia; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment