---
# System prepended metadata

title: Carro sensor ultrassônico

---

# Carro sensor ultrassônico

**Código**

#include <Ultrasonic.h>;            

#define TRIGGER_PIN 4               
#define ECHO_PIN    5               

Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN); 

#define IN1 2 //6                       
#define IN2 7                       
#define IN3 8                       
#define IN4 9                        
#define ENA 10                      
#define ENB 6 //11
#define TON 3
#define LUX 12                       

void setup() 
{
  pinMode(IN1,OUTPUT);              
  pinMode(IN2,OUTPUT);              
  pinMode(IN3,OUTPUT);             
  pinMode(IN4,OUTPUT);              
  pinMode(ENA,OUTPUT);              
  pinMode(ENB,OUTPUT);
  pinMode(LUX,OUTPUT);              
  analogWrite(ENA,100);             
  analogWrite(ENB,100);             
  delay(1000);                    
} 
 
void loop()                         
{
  robo_frente();                    
  float dist_cm = distancia();     
 
    if(dist_cm < 25)                
    {
      decisao();
    } 
 
delay(100);                           
} 
 
float distancia()                   
{
  float cmMsec;
  long microsec = ultrasonic.timing();
  cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
  return(cmMsec);                   
delay(10);
} 
 
void robo_frente()                  
{
  digitalWrite(IN1,HIGH);
  digitalWrite(IN2,LOW);
  digitalWrite(IN3,HIGH);
  digitalWrite(IN4,LOW);
} 
 
void robo_esquerda()             
{
  digitalWrite(IN1,HIGH);
  digitalWrite(IN2,LOW);
  digitalWrite(IN3,LOW);
  digitalWrite(IN4,HIGH); 
} 
 
void robo_parado()                  
{
 
  digitalWrite(IN1,LOW);
  digitalWrite(IN2,LOW);
  digitalWrite(IN3,LOW);
  digitalWrite(IN4,LOW);
  
} 
 
void decisao()                      
{
  robo_parado();
  tone(TON,800,600);
  digitalWrite(LUX,HIGH);   
  delay(700);
  digitalWrite(LUX,LOW);  
  robo_esquerda();
  delay(400);                       
  robo_parado();
  delay(500);
  //noTone(12);
}