小实验:测距感应灯
所需硬件:1.arduino uno *12. HC-SR04 *1
电路图:
代码:
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);
}
上传至UNO,试一试。:P
感谢分享啊 dsweiliang 发表于 2016-12-8 16:38
感谢分享啊
{:5_198:}{:5_198:}{:5_198:} 感谢分享! studyeda 发表于 2016-12-15 12:05
感谢分享!
:lol:lol:lol 感谢分享 学习~
页:
[1]