Stap 9: Stap: 8 tenslotte de code
Const int pingPin = 7;Const int ledPin = 13;
VOID Setup {}
Serial.begin(9600);
pinMode (ledPin, OUTPUT);
}
void loop {}
lange duur, inch, cm;
pinMode(pingPin,OUTPUT);
digitalWrite(pingPin,LOW);
delayMicroseconds(2);
digitalWrite(pingPin,HIGH);
delayMicroseconds(5);
digitalWrite(pingPin,LOW);
pinMode(pingPin,INPUT);
duur = pulseIn(pingPin,HIGH);
duim = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.Print(inches);
Serial.Print ("in");
Serial.Print(cm);
Serial.Print("cm");
Serial.println();
delay(100);
Als (inch < = 12) {}
digitalWrite (ledPin, hoge);
alarm();
}
else {}
digitalWrite (ledPin, laag);
}
}
lange microsecondsToInches(long microseconds)
{
retourneren van microseconden /74/2;
}
lange microsecondsToCentimeters(long microseconds)
{
retourneren van microseconden /29/2;
}
ongeldig alarm() {}
digitalWrite (ledPin, hoge);
delay(500);
digitalWrite (ledPin, laag);
delay(500);
}