Mixly声波传感器控制led灯

本次实验使用触摸式开关声波传感器控制LED明亮

硬件:如图

图片: Mixly声波传感器控制led灯_第1张图片

代码片段

volatile float dist;

float checkdistance_A2_A3() {
  digitalWrite(A2, LOW);
  delayMicroseconds(2);
  digitalWrite(A2, HIGH);
  delayMicroseconds(10);
  digitalWrite(A2, LOW);
  float distance = pulseIn(A3, HIGH) / 58.00;
  delay(10);
  return distance;
}

void setup(){
  dist = 0;
  pinMode(A2, OUTPUT);
  pinMode(A3, INPUT);
  Serial.begin(9600);
  pinMode(3, OUTPUT);
}

void loop(){
  dist = checkdistance_A2_A3();
  Serial.print("Distance(cm):");
  Serial.println(dist);
  if (dist < 50) {
    digitalWrite(3,LOW);

  } else {
    digitalWrite(3,HIGH);

  }

}

Mixly声波传感器控制led灯_第2张图片

演示视频

https://v.youku.com/v_show/id_XNDIyNjA2NjQzMg==.html?spm=a2hzp.8244740.0.0

你可能感兴趣的:(Mixly声波传感器控制led灯)