Usamos o HC-SR04 Ultrasonic Distance Measuring Sensor Module para medir distâncias e imprimi-las no monitor. Veja a Aula 17 deste curso: http://www.toptechboy.com/arduino-lessons/
int echoPin=11; //Sensor Echo pin connected to Arduino pin 11
float pingTime;
float targetDistance;
int speed= 345; //sound speed in m/s at 26C at see level
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
pingTime = pulseIn(echoPin, HIGH); //pingTime is presented in microceconds
targetDistance = speed*pingTime/1000000/2;
Serial.print("the distance is: ");
Serial.print(targetDistance);
Serial.println(" m");
delay(1000);
}
Para medir a velocidade do som, partimos de uma distância bem determinada (digamos, 20cm) e modificamos o código acima do seguinte modo:
int trigPin=13; //Sensor Trip pin connected to Arduino pin 13
int echoPin=11; //Sensor Echo pin connected to Arduino pin 11
float pingTime;
float speedOfSound;
float targetDistance=0.2; //Distance to Target in m
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
pingTime = pulseIn(echoPin, HIGH); //pingTime is presented in microceconds
speedOfSound = (targetDistance*2)/pingTime*1000000;
Serial.print("The Speed of Sound is: ");
Serial.print(speedOfSound);
Serial.println(" m/s");
delay(1000);
}
No comments:
Post a Comment