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

    项目地址:https://www.tinkercad.com/things/j0k4YgzXoDF-ultrasonic-distance-sensor-led-bar-graph-blocks

    int distanceThreshold = 0;
    
    int cm = 0;
    
    int inches = 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);
    
      pinMode(2, OUTPUT);
      pinMode(3, OUTPUT);
      pinMode(4, OUTPUT);
    }
    
    void loop()
    {
      // set threshold distance to activate LEDs
      distanceThreshold = 350;
      // 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(cm);
      Serial.print("cm, ");
      Serial.print(inches);
      Serial.println("in");
      if (cm > distanceThreshold) {
        digitalWrite(2, LOW);
        digitalWrite(3, LOW);
        digitalWrite(4, LOW);
      }
      if (cm <= distanceThreshold && cm > distanceThreshold - 100) {
        digitalWrite(2, HIGH);
        digitalWrite(3, LOW);
        digitalWrite(4, LOW);
      }
      if (cm <= distanceThreshold - 100 && cm > distanceThreshold - 250) {
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
        digitalWrite(4, LOW);
      }
      if (cm <= distanceThreshold - 250 && cm > distanceThreshold - 350) {
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
        digitalWrite(4, HIGH);
      }
      if (cm <= distanceThreshold - 350) {
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
        digitalWrite(4, HIGH);
      }
      delay(100); // Wait for 100 millisecond(s)
    }
  • 相关阅读:
    数据库表中批量替换某个字段的方法
    css清除浮动方法大全
    IE6中的常见BUG与相应的解决办法
    [转]Oracle 12c多租户特性详解:PDB 的创建、克隆与维护
    Oracle 数据库导入导出 dmp文件
    Tomcat 服务应用
    Perforce 与Source Insight, Visual Studio集成
    Oracle 11g必须开启的服务及服务详细介绍
    Python学习笔记8-单元测试(1)
    Python学习笔记7-高级迭代器
  • 原文地址:https://www.cnblogs.com/meetrice/p/14083015.html
Copyright © 2011-2022 走看看