Avståndssensor och Busser för Adafruit ESP Feather
Hoppa till navigering
Hoppa till sök
Kodexempel
//Defining the pins and variables const int trigPin = 27; const int echoPin = 13; const int freq = 2000; const int channel = 0; const int resolution = 8; long duration; long distance; void setup() { //Initiate serial monitor Serial.begin (9600); //Specifies the pins behavior pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); ledcSetup(channel, freq, resolution); ledcAttachPin(12, channel); ledcWrite(channel, 10); } void loop() { //Sending a LOW pulse to clear the trigPin //and sets the trigger to a HIGH pulse for 10 microseconds digitalWrite(trigPin, LOW); delayMicroseconds(5); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); //Reads the echoPin and the time it //took for the HIGH pulse to return duration = pulseIn(echoPin, HIGH); //Calculating the distance in cm distance = (duration/2) / 29.1; //Checking to see if the discance is less than 400 //to avoid getting alerts when object is out of range. if(distance < 400){ Serial.print("distance: "); Serial.println(distance); ledcWriteTone(channel, distance); } delay(200); }