Клод
Мастер
(1384)
3 недели назад
Вот:
int trigPin = 10;
int echoPin = 11;
int rLed = 12;
int yLed = 8;
int bLed = 9;
int buzz = 7;
void setup() {
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(bLed, OUTPUT);
pinMode(yLed, OUTPUT);
pinMode(rLed, OUTPUT);
pinMode(buzz, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration / 58;
if (distance > 20) {
digitalWrite(bLed, HIGH);
digitalWrite(yLed, LOW);
digitalWrite(rLed, LOW);
digitalWrite(buzz, LOW);
}
else if (distance <= 20 && distance > 10) {
digitalWrite(yLed, HIGH);
digitalWrite(bLed, LOW);
digitalWrite(rLed, LOW);
digitalWrite(buzz, HIGH);
}
else if (distance < 10) {
digitalWrite(rLed, HIGH);
digitalWrite(yLed, LOW);
digitalWrite(bLed, LOW);
digitalWrite(buzz, HIGH);
}
delay(100);
}
код:
int trigPin = 10;
int echoPin = 11;
int rLed = 12;
int yLed = 8;
int bLed = 9;
int buzz = 7;
int main()
void setup(){
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(bLed, OUTPUT);
pinMode(yLed, OUTPUT);
pinMode(rLed, OUTPUT);
pinMode(buzz, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration / 58;
}
if (distance > 20) {
digitalWrite(bLed, HIGH);
digitalWrite(yled, LOW);
digitalWrite(rLed, LOW);
digitalWrite(buzz, LOW);
}
else if (distance <= 20 && distance > 10) {
digitalWrite(yLed, HIGH);
digitalWrite(bLed, LOW);
digitalWrite(rLed, LOW);
digitalWrite(buzz, HIGH);
}
else if (distance < 10) {
digitalWrite(rLed, HIGH);
digitalWrite(yLed, LOW);
digitalWrite(bLed, LOW);
digitalWrite(buzz, HIGH);
}
delay(100):
}