|
|
Ligne 53 : |
Ligne 53 : |
| |Step_Content=<nowiki>#</nowiki>include <Servo.h> | | |Step_Content=<nowiki>#</nowiki>include <Servo.h> |
| | | |
− | /*
| |
− |
| |
− | * Code d'exemple pour un capteur à ultrasons HC-SR04.
| |
− |
| |
− | */
| |
− |
| |
− | /* Constantes pour les broches */
| |
− |
| |
− | const byte TRIGGER_PIN = 2; // Broche TRIGGER
| |
− |
| |
− | const byte ECHO_PIN = 3; // Broche ECHO
| |
− |
| |
− | const int ledPin = 13; // pin that the LED is attached to
| |
− |
| |
− | /* Constantes pour le timeout */
| |
− |
| |
− | const unsigned long MEASURE_TIMEOUT = 25000UL; // 25ms = ~8m à 340m/s
| |
− |
| |
− | /* Vitesse du son dans l'air en mm/us */
| |
− |
| |
− | const float SOUND_SPEED = 340.0 / 1000;
| |
− |
| |
− | Servo myservo;
| |
− |
| |
− | int pos;
| |
− |
| |
− | /** Fonction setup() */
| |
− |
| |
− | void setup() {
| |
− |
| |
− | /*{
| |
− |
| |
− | myservo.attach(9);
| |
− |
| |
− | myservo.writeMicroseconds(2000); // CW
| |
− |
| |
− | delay(5000);
| |
− |
| |
− | myservo.writeMicroseconds(1000); // CCW
| |
− |
| |
− | delay(3000);
| |
− |
| |
− | myservo.writeMicroseconds(1500); // stop
| |
− |
| |
− | delay(100);
| |
− |
| |
− | }*/
| |
− |
| |
− | // initialize the switch pin as an input:
| |
− |
| |
− | // pinMode(switchPin, INPUT);
| |
− |
| |
− | // initialize the LED pin as an output:
| |
− |
| |
− | pinMode(ledPin, OUTPUT);
| |
− |
| |
− | /* Initialise le port série */
| |
− |
| |
− | Serial.begin(115200);
| |
− |
| |
− | /* Initialise les broches */
| |
− |
| |
− | pinMode(TRIGGER_PIN, OUTPUT);
| |
− |
| |
− | digitalWrite(TRIGGER_PIN, LOW); // La broche TRIGGER doit être à LOW au repos
| |
− |
| |
− | pinMode(ECHO_PIN, INPUT);
| |
− |
| |
− | }
| |
− |
| |
− | /** Fonction loop() */
| |
− |
| |
− | void loop() {
| |
− |
| |
− | /* 1. Lance une mesure de distance en envoyant une impulsion HIGH de 10µs sur la broche TRIGGER */
| |
− |
| |
− | digitalWrite(TRIGGER_PIN, HIGH);
| |
− |
| |
− | delayMicroseconds(10);
| |
− |
| |
− | digitalWrite(TRIGGER_PIN, LOW);
| |
− |
| |
− | /* 2. Mesure le temps entre l'envoi de l'impulsion ultrasonique et son écho (si il existe) */
| |
− |
| |
− | long measure = pulseIn(ECHO_PIN, HIGH, MEASURE_TIMEOUT);
| |
− |
| |
− | /* 3. Calcul la distance à partir du temps mesuré */
| |
− |
| |
− | float distance_mm = measure / 2.0 * SOUND_SPEED;
| |
− |
| |
− | /* Affiche les résultats en mm, cm et m */
| |
− |
| |
− | Serial.print(F("Distance: "));
| |
− |
| |
− | Serial.print(distance_mm);
| |
− |
| |
− | Serial.print(F("mm ("));
| |
− |
| |
− | Serial.print(distance_mm / 10.0, 2);
| |
− |
| |
− | Serial.print(F("cm, "));
| |
− |
| |
− | Serial.print(distance_mm / 1000.0, 2);
| |
− |
| |
− | Serial.println(F("m)"));
| |
− |
| |
− | /* Délai d'attente pour éviter d'afficher trop de résultats à la seconde */
| |
− |
| |
− | delay(50);
| |
− |
| |
− | // si la distance est < 15cm, turn on the LED und motor:
| |
− |
| |
− | if (distance_mm < 150) {
| |
− |
| |
− | digitalWrite(ledPin, HIGH);
| |
− |
| |
− | } else {
| |
− |
| |
− | digitalWrite(ledPin, LOW);
| |
− |
| |
− | }
| |
− |
| |
− | if (distance_mm < 150) {
| |
− |
| |
− | myservo.attach(9);
| |
− |
| |
− | myservo.writeMicroseconds(2000); // CW
| |
− |
| |
− | delay(1110.75);
| |
− |
| |
− | myservo.writeMicroseconds(1500);
| |
− |
| |
− | delay(10000);
| |
− |
| |
− | }
| |
− |
| |
− | else {
| |
− |
| |
− | myservo.writeMicroseconds(1500); // stop
| |
− |
| |
− | delay(100);
| |
− |
| |
− | }
| |
| }} | | }} |
| {{ {{tntn|Tuto Step}} | | {{ {{tntn|Tuto Step}} |