long duration, inches, cm;
int emissor = 9;
int receptor=10;
void setup() {
Serial.begin (9600);
pinMode(emissor, OUTPUT);
pinMode(receptor, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
delay (250);
int ir_d = analogRead(A1);
int ir_e = analogRead(A2);
int sensor_d = analogRead(A3);
int sensor_e = analogRead(A4);
sonar();
Serial.print(ir_d);
Serial.print(", ");
Serial.print(ir_e);
Serial.print(", ");
Serial.print(sensor_d);
Serial.print(", ");
Serial.println(sensor_e);
Serial.print(", ");
Serial.println(cm);
Serial.println();
}
int sonar(){
digitalWrite(emissor, LOW);
delayMicroseconds(2);
digitalWrite(emissor, HIGH);
delayMicroseconds(10);
digitalWrite(emissor, LOW);
duration = pulseIn(receptor, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
return cm;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
Nenhum comentário:
Postar um comentário