Sensor de Estacionamento



int bip;
int tempo;
double freq;
#include <NewPing.h>
#include <NewTone.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 50 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  pinMode(9,OUTPUT);
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  //delay(bip);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.print("cm.  freq = ");
  Serial.println(freq);
  tempo = sonar.ping_cm();
  bip = map(tempo,0,50,0,1000);

  while(millis() > freq && bip != 0){
  NewTone(9,500,50);
  freq = bip + millis();
  }
}


*******************Programa com display: **********************************

// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------
int bip;
int tempo;
double freq;
#include <NewPing.h>
#include <NewTone.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 50 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

// PARAMETROS DO LCD ****************************************

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADOR 0x3F // <<
#define BACKLIGHT_PIN 3
#define En 2
#define Rw 1
#define Rs 0
#define D4 4
#define D5 5
#define D6 6
#define D7 7
LiquidCrystal_I2C lcd(I2C_ADOR, En, Rw, Rs, D4, D5, D6, D7);

// **********************************************************


NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  pinMode(9,OUTPUT);
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  lcd.begin (16, 2);
  lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd.setBacklight(HIGH);
}

void loop() {
  //delay(bip);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.print("cm.  freq = ");
  Serial.println(freq);
  tempo = sonar.ping_cm();
  bip = map(tempo,0,50,0,1000);

  lcd.setCursor( 0 , 1); // linha, coluna;
  lcd.print("distancia: ");

  lcd.setCursor( 11 , 1); // linha, coluna;
  lcd.print(tempo);

  lcd.setCursor( 14 , 1); // linha, coluna;
  lcd.print("cm");

if(tempo <= 5 && tempo != 0) NewTone(9,500); // faz o bip ficar continuo quando a distancia for menor que 5 cm.
else if(tempo == 0 ) noNewTone(9);
 
  while(millis() > freq && bip != 0){
  NewTone(9,500,50);
  freq = bip + millis();
  lcd.clear();
}
  }

Nenhum comentário:

Postar um comentário