Created
July 30, 2013 14:36
-
-
Save buildcircuit/6113471 to your computer and use it in GitHub Desktop.
Ultrasonic range finder experiment: http://www.buildcircuit.com/obstacle-sensor-using-arduino-and-hcsr04/
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
/* | |
Projeto Arduino - Arduino com sensor de proximidade ultrasonico HHC-SRO4 | |
Por Jota | |
---------------------------------------- | |
--=<| www.ComoFazerAsCoisas.com.br |>=-- | |
---------------------------------------- | |
*/ | |
//Incluindo biblioteca Ultrasonic.h | |
#include "Ultrasonic.h" | |
//criando objeto ultrasonic e definindo as portas digitais do Trigger - 6 - e Echo - 7 | |
Ultrasonic ultrasonic(6,7); | |
//Declaração das constantes referentes aos pinos digitais. | |
const int ledgreen = 13; | |
const int ledyellow = 12; | |
const int ledred = 11; | |
long microsec = 0; | |
float distanciaCM = 0; | |
void setup () { | |
Serial.begin(9600); //Inicializando o serial monitor | |
pinMode(ledgreen,OUTPUT); //Definindo pino digital 13 como saída. | |
pinMode(ledyellow,OUTPUT); //Definindo pino digital 12 como saída. | |
pinMode(ledred,OUTPUT); //Definindo pino digital 11 como saída. | |
} | |
void loop () { | |
microsec = ultrasonic.timing(); //Lendo o sensor | |
distanciaCM = ultrasonic.convert(microsec, Ultrasonic::CM); //Convertendo a distância em CM | |
ledDistancia(); | |
Serial.print(distanciaCM); | |
Serial.println(" cm"); | |
delay(1000); | |
} | |
//Método que centraliza o controle de acendimento dos leds. | |
void ledDistancia () { | |
//Apagando todos os leds | |
digitalWrite(ledgreen,LOW); | |
digitalWrite(ledyellow,LOW); | |
digitalWrite(ledred,LOW); | |
//Acendendo o led adequado para a distância lida no sensor | |
if (distanciaCM > 20) { | |
digitalWrite(ledgreen,HIGH); | |
} | |
if (distanciaCM <=20 and distanciaCM >= 10) { | |
digitalWrite(ledyellow,HIGH); | |
} | |
if (distanciaCM < 10) { | |
digitalWrite(ledred,HIGH); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment