Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int pingPin = 7; //sensor de proximidad
- void setup() {
- pinMode(9, OUTPUT); //Usamos el pin 9 por ser PWM para escribir analogamente
- Serial.begin(9600); //iniciar comunicacion serial
- }
- void loop()
- {
- // INICIO SENSOR DE PROXIMIDAD
- long duracion, cm;
- pinMode(pingPin, OUTPUT);
- digitalWrite(pingPin, LOW);
- delayMicroseconds(2);
- digitalWrite(pingPin, HIGH);
- delayMicroseconds(5);
- digitalWrite(pingPin, LOW);
- pinMode(pingPin, INPUT);
- duracion = pulseIn(pingPin, HIGH);
- // convertir tiempo a distancia
- cm = microsecondsToCentimeters(duracion);
- Serial.print(cm);
- Serial.print("cm");
- Serial.println();
- delay(100);
- //FIN SENSOR 1/2
- int vol;
- if (cm>200) {vol = 0;} //use 200cms como referencia, ya que de tanto que probamos, nos volvia loco el sonido
- else {vol = (20*cm);} //(0 - 4000 de mas grave a mas agudo) ya que son 200cms => 4000/200 = 20
- analogWrite(9, vol); //Hacer Sonar el Buzzer
- }
- //FIN VOIDLOOP
- long microsecondsToCentimeters(long microseconds) //Tiempo a distancia
- {
- return microseconds / 29 / 2;
- }
- // FIN SENSOR 2/2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement