Sunday, July 28, 2024

Ultrasonic Test.

 

const int trigPin = 9;

const int echoPin = 10;


long duration;

int distance;


void setup() {

  pinMode(trigPin, OUTPUT);

  pinMode(echoPin, INPUT);

  Serial.begin(9600);  // Start the serial communication

}


void loop() {

  // Clear the trigPin

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);


  // Set the trigPin on HIGH state for 10 micro seconds

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);


  // Read the echoPin, return the sound wave travel time in microseconds

  duration = pulseIn(echoPin, HIGH);


  // Calculate the distance

  distance = duration * 0.034 / 2;


  // Print the distance on the Serial Monitor

  Serial.print("Distance: ");

  Serial.print(distance);

  Serial.println(" cm");


  delay(500);

}


No comments:

Post a Comment

MUD Three Mode operation Manual Automatic GPS

 Code for three mode operation: /*   3-Mode Headlight Controller   - Manual mode (driver uses a toggle to pick high/low)   - Auto mode (LDR...