一、实物连接
二、代码实现
const int ledPin = 13; //pin13 built-in led
const int avoidPin = 7; //the ir obstacle sensor attach to pin 7
void setup()
{
pinMode(ledPin, OUTPUT); //set the ledPin as OUTPUT
pinMode(avoidPin, INPUT); //set the avoidPin as INPUT
}
void loop()
{
boolean avoidVal = digitalRead(avoidPin); //read the value of pin7
if(avoidVal == LOW) //if the value is LOW level
{
digitalWrite(ledPin, HIGH); //turn on the led
}
else //else
{
digitalWrite(ledPin, LOW); //turn off the led
}
}