Ultrasonic distance meter w LCD

Поделиться
HTML-код
  • Опубликовано: 6 авг 2024
  • Using Arduino code on Teensey 3.1 for Ultrasonic Distance metering and LCD display.
  • НаукаНаука

Комментарии • 1

  • @creator4X6YS
    @creator4X6YS  10 лет назад

    #include
    LiquidCrystal lcd(12, 11, 5, 4, 3, 2);; // Arduino PinOuts to LCD left to right (RS,E,D4,D5,D6,D7)
    // use the pin configuration for the LCD shoed in the example arduino.cc/en/Tutorial/LiquidCrystal
    // SONAR PIN Configuration
    int trigPin = 8; //Trig - at SONAR for DigPin 8
    int echoPin = 9; //Echo - at SONAR for DigPin 9
    long duration, cm ;
    void setup() {
    Serial.begin (9600);
    //Define inputs and outputs for the SONAR
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    // LCD first row displays constantely
    lcd.begin(16,2);
    lcd.print("Distance in cm");
    }
    void loop()
    {
    // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
    // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
    digitalWrite(trigPin, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    // // Read the signal from the sensor: a HIGH pulse whose
    // // duration is the time (in microseconds) from the sending
    // // of the ping to the reception of its echo off of an object.
    pinMode(echoPin, INPUT);
    duration = pulseIn(echoPin, HIGH);
    // convert the time into a distance
    cm = (duration/2) / 29.1;
    Serial.println(cm);
    //now I want to display it on the LCD
    if ( cm