Etape 1: Le programme
<p>//StudentBuilds Arduino Parking Sensor</p><p>//http://studentbuilds.blogspot.com</p><p> // defines pins numbers#define IN4 4const int trigPin = 5;const int echoPin = 6;// defines variableslong duration;int distance = 0;void setup() {pinMode(trigPin, OUTPUT); // Sets the trigPin as an OutputpinMode(echoPin, INPUT); // Sets the echoPin as an InputpinMode(4, OUTPUT);Serial.begin(9600); // Starts the serial communication}</p><p>void loop() {if ((distance <50)&&(distance > 0))// If distance to object is less than 50cm, run the loop { digitalWrite(IN4, HIGH); // turn the Buzzer on (HIGH is the voltage level)}else if ((distance > 51)&&(distance < 150)) { digitalWrite(IN4, HIGH); // turn the c on (HIGH is the voltage level) delay(10*(distance-50)); digitalWrite(IN4, LOW); // turn the Buzzer on (HIGH is the voltage level)</p><p> delay(10*(distance-50));// wait for a set time }else if ((distance > 151)&&(distance < 200)) { digitalWrite(IN4, HIGH); // turn the Buzzer on (HIGH is the voltage level)</p><p> delay(1000); digitalWrite(IN4, LOW); // turn the Buzzer on (HIGH is the voltage level)</p><p> delay(1000);}else if (distance > 200){ digitalWrite(IN4, LOW);}</p><p>delayMicroseconds(10);// Clears the trigPindigitalWrite(trigPin, LOW);delayMicroseconds(2);// Sets the trigPin on HIGH state for 10 micro secondsdigitalWrite(trigPin, HIGH);delayMicroseconds(10);digitalWrite(trigPin, LOW);// Reads the echoPin, returns the sound wave travel time in microsecondsduration = pulseIn(echoPin, HIGH);// Calculating the distancedistance= duration*0.034/2;// Prints the distance on the Serial MonitorSerial.print("Distance: ");Serial.println(distance); //Returns the integer 'distance' in cm}</p>
Tout le code est juste là dessus. J’ai essayé d’inclure autant de commentaires précis que je peux, mais si vous trouvez que j’ai manqué quelque chose de monumental (ou rien du tout d’ailleurs) qui vous aiderait à comprendre le programme, ou si vous avez des questions en général alors n’hésitez à demander emporté.