Paso 5: Programa de software de Arduino (boceto)
El programa de Arduino hará básicamente los siguientes pasos:
- Activar pin Trig
- Enviar un pulso de anchura 10 microsegundos
- Apagar pin theTrig
- Imprimir el tiempo total de viaje del pulso en el monitor serie.
----------------------------------------------------------------
int trigPin = 13; // set pin 13 as trig pin int echoPin = 7; // set pin 7 as echo pin int time;
void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); // set trig pin as output, we send pulse through this pinMode(echoPin,INPUT); // set echo pin as input, we detect echo through this pin }
void loop() { //sending 10 microsecond width pulses, frequency ~ 40KHz digitalWrite(trigPin, LOW); delayMicroseconds(2000); digitalWrite(trigPin,HIGH); //trig pin delayMicroseconds(10); //pulse width 10 microseconds digitalWrite(trigPin, LOW); //trig pin off time = pulseIn(echoPin, HIGH); //pulseIn(), function return time in microseconds //print time on the serial monitor Serial.print(" Time taken for the pulse to travel: "); Serial.print(time); Serial.println(" microseconds"); delay(2000); }