challenge2026
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| challenge2026 [2026/03/01 12:42] – créée mistert2 | challenge2026 [2026/03/16 11:49] (Version actuelle) – mistert2 | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | |||
| {{ :: | {{ :: | ||
| + | {{ :: | ||
| + | |||
| + | **PROGRAMME POUR LA BARRIERE** | ||
| + | |||
| + | <code C++> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | Servo myservo; | ||
| + | #define SCREEN_WIDTH 128 | ||
| + | #define SCREEN_HEIGHT 64 | ||
| + | #define OLED_RESET | ||
| + | #define SCREEN_ADDRESS 0x3C | ||
| + | |||
| + | Adafruit_SSD1306 display(SCREEN_WIDTH, | ||
| + | |||
| + | unsigned long prochainDeclenchement = 0; | ||
| + | int pos = 0; | ||
| + | // Capteurs IR | ||
| + | const byte CAPT1 = 2; | ||
| + | const byte CAPT2 = 3; | ||
| + | const byte FEUVERT = 12; | ||
| + | const byte FEUROUGE = 11; | ||
| + | // Distance entre capteurs | ||
| + | const float DISTANCE_M = 20; | ||
| + | |||
| + | // Variables ISR | ||
| + | volatile unsigned long t1 = 0; | ||
| + | volatile unsigned long t2 = 0; | ||
| + | volatile bool capteur1Declenche = false; | ||
| + | volatile bool mesurePrete = false; | ||
| + | |||
| + | // Anti-rebond | ||
| + | volatile unsigned long lastTrig1 = 0; | ||
| + | volatile unsigned long lastTrig2 = 0; | ||
| + | const unsigned long antiRebondUs = 5000; // 5 ms | ||
| + | |||
| + | // Timeout de mesure | ||
| + | const unsigned long timeoutMesureUs = 3000000UL; // 3 s | ||
| + | |||
| + | // Filtrage dt | ||
| + | const unsigned long dtMinUs = 5000UL; | ||
| + | const unsigned long dtMaxUs = 2000000UL; | ||
| + | |||
| + | // Résultats | ||
| + | float vitesse_ms = 0.0; | ||
| + | float vitesse_kmh = 0.0; | ||
| + | |||
| + | // Gestion affichage temporisé | ||
| + | unsigned long affichageJusqua = 0; | ||
| + | bool afficherResultat = false; | ||
| + | |||
| + | void ISR_capteur1() { | ||
| + | unsigned long now = micros(); | ||
| + | |||
| + | if (now - lastTrig1 < antiRebondUs) return; | ||
| + | lastTrig1 = now; | ||
| + | |||
| + | t1 = now; | ||
| + | capteur1Declenche = true; | ||
| + | mesurePrete = false; | ||
| + | } | ||
| + | |||
| + | void ISR_capteur2() { | ||
| + | unsigned long now = micros(); | ||
| + | |||
| + | if (now - lastTrig2 < antiRebondUs) return; | ||
| + | lastTrig2 = now; | ||
| + | |||
| + | if (capteur1Declenche && now > t1) { | ||
| + | t2 = now; | ||
| + | mesurePrete = true; | ||
| + | capteur1Declenche = false; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | void afficheAttente() { | ||
| + | display.clearDisplay(); | ||
| + | display.setTextColor(SSD1306_WHITE); | ||
| + | |||
| + | display.setTextSize(1); | ||
| + | display.setCursor(0, | ||
| + | display.println(" | ||
| + | |||
| + | display.setTextSize(2); | ||
| + | display.setCursor(10, | ||
| + | display.println(" | ||
| + | |||
| + | display.display(); | ||
| + | } | ||
| + | |||
| + | void afficheVitesse(float vms, float t) { | ||
| + | display.clearDisplay(); | ||
| + | display.setTextColor(SSD1306_WHITE); | ||
| + | |||
| + | display.setTextSize(1); | ||
| + | display.setCursor(0, | ||
| + | display.println(" | ||
| + | |||
| + | display.setTextSize(2); | ||
| + | display.setCursor(0, | ||
| + | display.print(vms, | ||
| + | display.println(" | ||
| + | |||
| + | display.setTextSize(2); | ||
| + | display.setCursor(0, | ||
| + | display.print(t, | ||
| + | display.println(" | ||
| + | |||
| + | display.display(); | ||
| + | } | ||
| + | |||
| + | void barriere() { | ||
| + | digitalWrite(FEUVERT, | ||
| + | digitalWrite(FEUROUGE, | ||
| + | | ||
| + | Serial.println(" | ||
| + | for (int i=90; | ||
| + | myservo.write(i); | ||
| + | delay(15); | ||
| + | } | ||
| + | | ||
| + | Serial.println(" | ||
| + | for (int i=0; | ||
| + | myservo.write(i); | ||
| + | delay(15); | ||
| + | } | ||
| + | |||
| + | | ||
| + | digitalWrite(FEUROUGE, | ||
| + | digitalWrite(FEUVERT, | ||
| + | } | ||
| + | |||
| + | void setup() { | ||
| + | randomSeed(analogRead(A0)); | ||
| + | prochainDeclenchement = millis() + random(4000, | ||
| + | Serial.begin(9600); | ||
| + | myservo.attach(6); | ||
| + | myservo.write(0); | ||
| + | delay(500); | ||
| + | |||
| + | | ||
| + | pinMode(CAPT1, | ||
| + | pinMode(CAPT2, | ||
| + | pinMode(FEUVERT, | ||
| + | pinMode(FEUROUGE, | ||
| + | digitalWrite(FEUVERT, | ||
| + | digitalWrite(FEUROUGE, | ||
| + | | ||
| + | if (!display.begin(SSD1306_SWITCHCAPVCC, | ||
| + | while (1) {} | ||
| + | } | ||
| + | |||
| + | afficheAttente(); | ||
| + | | ||
| + | attachInterrupt(digitalPinToInterrupt(CAPT1), | ||
| + | attachInterrupt(digitalPinToInterrupt(CAPT2), | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | bool calculer = false; | ||
| + | unsigned long local_t1 = 0; | ||
| + | unsigned long local_t2 = 0; | ||
| + | bool local_capteur1Declenche = false; | ||
| + | |||
| + | | ||
| + | if (mesurePrete) { | ||
| + | local_t1 = t1; | ||
| + | local_t2 = t2; | ||
| + | mesurePrete = false; | ||
| + | calculer = true; | ||
| + | } | ||
| + | local_capteur1Declenche = capteur1Declenche; | ||
| + | unsigned long local_t1_en_cours = t1; | ||
| + | interrupts(); | ||
| + | |||
| + | | ||
| + | |||
| + | // Timeout si capteur 2 ne vient jamais | ||
| + | if (local_capteur1Declenche) { | ||
| + | if (micros() - local_t1_en_cours > timeoutMesureUs) { | ||
| + | noInterrupts(); | ||
| + | capteur1Declenche = false; | ||
| + | interrupts(); | ||
| + | |||
| + | Serial.println(" | ||
| + | afficheAttente(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | if (calculer) { | ||
| + | unsigned long dt_us = local_t2 - local_t1; | ||
| + | |||
| + | if (dt_us >= dtMinUs && dt_us <= dtMaxUs) { | ||
| + | float dt_s = dt_us / 1000000.0; | ||
| + | vitesse_ms = DISTANCE_M / dt_s; | ||
| + | vitesse_kmh = vitesse_ms * 3.6; | ||
| + | |||
| + | Serial.print(" | ||
| + | Serial.print(local_t1); | ||
| + | Serial.print(" | ||
| + | Serial.print(local_t2); | ||
| + | Serial.print(" | ||
| + | Serial.print(dt_us); | ||
| + | Serial.println(" | ||
| + | |||
| + | Serial.print(" | ||
| + | Serial.print(vitesse_ms, | ||
| + | Serial.print(" | ||
| + | | ||
| + | |||
| + | afficheVitesse(vitesse_ms, | ||
| + | afficherResultat = true; | ||
| + | affichageJusqua = millis() + 2000; | ||
| + | } else { | ||
| + | Serial.print(" | ||
| + | Serial.println(dt_us); | ||
| + | afficheAttente(); | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | |||
| + | if (afficherResultat && millis() >= affichageJusqua) { | ||
| + | afficherResultat = false; | ||
| + | afficheAttente(); | ||
| + | if (millis() >= prochainDeclenchement) { | ||
| + | |||
| + | barriere(); | ||
| + | prochainDeclenchement = millis() + random(4000, | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
challenge2026.1772368939.txt.gz · Dernière modification : de mistert2
