Ultrasonic sensor SRF04/05 - mcworkshop

MC   WORKSHOP
Vai ai contenuti

Menu principale:

Ultrasonic sensor SRF04/05

/* Sensore ultrasuoni
 Created september 2019 by MCWorkshop.
 This example code is in the public domain
 pin 12 Arduino to Trig
 pin 13 Arduino to Echo
 GND to GND
 5V to Vcc
 pin OUT HY-SRF05 = 5Vcc
 Oltre i 3 metri scrive: Fuori campo
*/
const int TRIG_PIN = 12;
const int ECHO_PIN = 13;
void setup() {
 Serial.begin(9600);
 pinMode(TRIG_PIN, OUTPUT);
 pinMode(ECHO_PIN, INPUT);
}
void loop()
{
 long duration;
 long distance;
 digitalWrite(TRIG_PIN, LOW);
 delayMicroseconds(2);
 digitalWrite(TRIG_PIN, HIGH);
 delayMicroseconds(10);
 digitalWrite(TRIG_PIN, LOW);
 duration = pulseIn(ECHO_PIN, HIGH);
 distance = duration / 29.1 / 2 ;
 if (distance >= 300) {
   Serial.println("Fuori campo");
 }
 else {
   Serial.print(distance);
   Serial.print(" cm");
   Serial.println();
 }
 delay(500);
}
 
Copyright 2015. All rights reserved.
Torna ai contenuti | Torna al menu