zoukankan      html  css  js  c++  java
  • 用Tinkercad学arduino之超声波测距仪

    /*
      Ping))) Sensor
    
      This sketch reads a PING))) ultrasonic
      rangefinder and returns the distance to the
      closest object in range. To do this, it sends a
      pulse to the sensor to initiate a reading, then
      listens for a pulse to return.  The length of
      the returning pulse is proportional to the
      distance of the object from the sensor.
    
      The circuit:
       * +V connection of the PING))) attached to +5V
       * GND connection attached to ground
       * SIG connection attached to digital pin 7
    
      http://www.arduino.cc/en/Tutorial/Ping
    
      This example code is in the public domain.
    */
    
    int inches = 0;
    
    int cm = 0;
    
    long readUltrasonicDistance(int triggerPin, int echoPin)
    {
      pinMode(triggerPin, OUTPUT);  // Clear the trigger
      digitalWrite(triggerPin, LOW);
      delayMicroseconds(2);
      // Sets the trigger pin to HIGH state for 10 microseconds
      digitalWrite(triggerPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(triggerPin, LOW);
      pinMode(echoPin, INPUT);
      // Reads the echo pin, and returns the sound wave travel time in microseconds
      return pulseIn(echoPin, HIGH);
    }
    
    void setup()
    {
      Serial.begin(9600);
    
    }
    
    void loop()
    {
      // measure the ping time in cm
      cm = 0.01723 * readUltrasonicDistance(7, 7);
      // convert to inches by dividing by 2.54
      inches = (cm / 2.54);
      Serial.print(inches);
      Serial.print("in, ");
      Serial.print(cm);
      Serial.println("cm");
      delay(100); // Wait for 100 millisecond(s)
    }
  • 相关阅读:
    springboot+maven+thymeleaf配置实战demo
    报错AbstractStandardExpressionAttributeTagProcessor
    IllegalStateException: Unable to find a @SpringBootConfiguration
    Java装饰模式
    Java容器类解析
    jdk之object源码理解
    osx brew mysql
    java String[] 初始化
    date 常用
    mac mysql
  • 原文地址:https://www.cnblogs.com/meetrice/p/14078046.html
Copyright © 2011-2022 走看看