|
本帖最后由 topdog 于 2021-4-4 11:06 编辑
任务二:
[pre]long item;
long off_time;
void setup()
{
item = 0;
off_time = 0;
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(4, INPUT);
pinMode(A0,INPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop()
{
item = checkdistance();
while (analogRead(A0) < 300) {
if (item > 20) {
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
} else if (item < 20 && item > 10) {
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
} else if (item == 10) {
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
digitalWrite(7,LOW);
} else if (item < 10 && item > 0) {
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
} else if (item == 0) {
off_time = millis();
if (millis() - off_time > 5000) {
digitalWrite(7,LOW);
}
}
}
}
float checkdistance() {
digitalWrite(3, LOW);
delayMicroseconds(2);
digitalWrite(3, HIGH);
delayMicroseconds(10);
digitalWrite(3, LOW);
float distance = pulseIn(4, HIGH) / 58.00;
delay(10);
return distance;
}[/pre] |
|