7660| 6
|
[入门] 小实验:测距感应灯 |
所需硬件:1. Arduino uno *1 2. HC-SR04 *1 电路图: 代码: [mw_shl_code=applescript,true]int inputPin = 4; int outputPin = 5; int ledpin = 13; void setup() { Serial.begin(9600); pinMode(ledpin, OUTPUT); pinMode(inputPin, INPUT); pinMode(outputPin, OUTPUT); } void loop() { digitalWrite(outputPin, LOW); delayMicroseconds(2); digitalWrite(outputPin, HIGH); delayMicroseconds(10); digitalWrite(outputPin, LOW); int distance = pulseIn(inputPin, HIGH); distance = distance / 58; // 将脉冲时间转化为距离(单位:厘米) Serial.println(distance); delay(50); //如果距离大于50厘米小灯亮起 if (distance >= 50) { digitalWrite(ledpin, HIGH); } else digitalWrite(ledpin, LOW); }[/mw_shl_code] ultrasound_led.zip 上传至UNO,试一试。:P |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed