Tuesday, March 7, 2017

19. Medindo distâncias com som

Para que o sistema seja autônomo, uma bateria de 9V pode ser usada para alimentar o arduíno e um LCD (1602 16x2 Character LCD Display Module HD44780 Controller Blue Arduino) é usado como mostrador. 

Veja as Aulas 19 e 20 deste curso: http://www.toptechboy.com/arduino-lessons/




#include <LiquidCrystal.h> 

// initialize the library with the numbers of the interface pins
LiquidCrystal LCD(7,8,9,10,11,12);
int trigPin=13; //Sensor Trip pin connected to Arduino pin 13
int echoPin=3;  //Sensor Echo pin connected to Arduino pin 11
float pingTime;
float targetDistance;
int speed= 345; //speed of sound at 26C, 1atm, in m/s

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly: 
  
  digitalWrite(trigPin, LOW); //Set trigger pin low
  delayMicroseconds(2000); //Let signal settle
  digitalWrite(trigPin, HIGH); //Set trigPin high
  delayMicroseconds(10); //Delay in high state
  digitalWrite(trigPin, LOW); //ping has now been sent
  delayMicroseconds(10); //Delay in high state

     pingTime = pulseIn(echoPin, HIGH);  //pingTime is presented in microceconds
  
  targetDistance = speed*pingTime/1000000/2; 

 LCD.setCursor(0,1);  //Set cursor to first column of second row
  LCD.print("                "); //Print blanks to clear the row
  LCD.setCursor(0,1);   //Set Cursor again to first column of second row
  LCD.print(targetDistance); //Print measured distance
  LCD.print(" m");  //Print your units.
  delay(250); //pause to let things settle
}

No comments:

Post a Comment