Arduino UNO R4 - Feu de circulation

Dans ce guide, nous apprendrons comment contrôler un module de feux de circulation en utilisant l'Arduino UNO R4. Nous aborderons :

Feu de circulation Arduino UNO R4

À propos du module de feux de circulation

Schéma des broches

Un module de feux de circulation possède 4 broches :

  • Broche GND : C'est la broche de masse. Reliez-la à la masse sur l'Arduino UNO R4.
  • Broche R : Cette broche commande la lumière rouge. Reliez-la à une sortie numérique sur l'Arduino UNO R4.
  • Broche Y : Cette broche commande la lumière jaune. Reliez-la à une sortie numérique sur l'Arduino UNO R4.
  • Broche G : Cette broche commande la lumière verte. Reliez-la à une sortie numérique sur l'Arduino UNO R4.
Schéma des broches du feu de circulation

Comment cela fonctionne

Diagramme de câblage

Schéma de câblage du feu de circulation Arduino UNO R4

Cette image a été créée avec Fritzing. Cliquez pour agrandir l'image.

Voir Comment alimenter l'Arduino UNO R4..

Comment programmer le module de feux de circulation

  • Configurez les broches d'un Arduino UNO R4 comme sorties numériques en utilisant la fonction pinMode().
pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT);
  • Programme pour activer la lumière rouge en utilisant la fonction digitalWrite():
digital-Write(PIN_RED, HIGH); // Set the RED LED to on digitalWrite(PIN_YELLOW, LOW); // Set the YELLOW LED to off digitalWrite(PIN_GREEN, LOW); // Set the GREEN LED to off delay(RED_TIME); // Maintain RED LED on state for the duration defined by RED_TIME

Code Arduino UNO R4

/* * Ce code Arduino UNO R4 a été développé par newbiely.fr * Ce code Arduino UNO R4 est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/arduino-uno-r4/arduino-uno-r4-traffic-light */ #define PIN_RED 2 // The Arduino UNO R4 pin connected to R pin of traffic light module #define PIN_YELLOW 3 // The Arduino UNO R4 pin connected to Y pin of traffic light module #define PIN_GREEN 4 // The Arduino UNO R4 pin connected to G pin of traffic light module #define RED_TIME 4000 // RED time in millisecond #define YELLOW_TIME 4000 // YELLOW time in millisecond #define GREEN_TIME 4000 // GREEN time in millisecond void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); } // the loop function runs over and over again forever void loop() { // red light on digitalWrite(PIN_RED, HIGH); // turn on digitalWrite(PIN_YELLOW, LOW); // turn off digitalWrite(PIN_GREEN, LOW); // turn off delay(RED_TIME); // keep red light on during a period of time // yellow light on digitalWrite(PIN_RED, LOW); // turn off digitalWrite(PIN_YELLOW, HIGH); // turn on digitalWrite(PIN_GREEN, LOW); // turn off delay(YELLOW_TIME); // keep yellow light on during a period of time // green light on digitalWrite(PIN_RED, LOW); // turn off digitalWrite(PIN_YELLOW, LOW); // turn off digitalWrite(PIN_GREEN, HIGH); // turn on delay(GREEN_TIME); // keep green light on during a period of time }

Étapes rapides

Suivez ces instructions étape par étape :

  • Si c'est la première fois que vous utilisez l'Arduino Uno R4 WiFi/Minima, reportez-vous au tutoriel sur Arduino UNO R4 - Installation du logiciel..
  • Connectez le module de feu de circulation à l'Arduino Uno R4 selon le diagramme fourni.
  • Connectez la carte Arduino Uno R4 à votre ordinateur à l'aide d'un câble USB.
  • Lancez l'IDE Arduino sur votre ordinateur.
  • Sélectionnez la carte Arduino Uno R4 appropriée (par exemple, Arduino Uno R4 WiFi) et le port COM.
  • Copiez le code et ouvrez-le dans l'IDE Arduino.
  • Cliquez sur le bouton Upload dans l'IDE Arduino pour téléverser le code sur Arduino UNO R4.
  • Découvrez le module de feu de circulation.

Les feux de circulation fonctionnent différemment selon leur conception, qui varie selon les zones. Les informations données ici donnent une idée générale de la manière dont les feux de circulation contribuent à réguler le trafic.

Le code ci-dessus vous permet de contrôler chaque lumière séparément. Maintenant, nous allons améliorer le code pour le faire fonctionner mieux.

Optimisation du code Arduino UNO R4

  • Améliorons le code en ajoutant une fonction pour contrôler la lumière.
/* * Ce code Arduino UNO R4 a été développé par newbiely.fr * Ce code Arduino UNO R4 est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/arduino-uno-r4/arduino-uno-r4-traffic-light */ #define PIN_RED 2 // The Arduino UNO R4 pin connected to R pin of traffic light module #define PIN_YELLOW 3 // The Arduino UNO R4 pin connected to Y pin of traffic light module #define PIN_GREEN 4 // The Arduino UNO R4 pin connected to G pin of traffic light module #define RED_TIME 2000 // RED time in millisecond #define YELLOW_TIME 1000 // YELLOW time in millisecond #define GREEN_TIME 2000 // GREEN time in millisecond #define RED 0 // Index in array #define YELLOW 1 // Index in array #define GREEN 2 // Index in array const int pins[] = { PIN_RED, PIN_YELLOW, PIN_GREEN }; const int times[] = { RED_TIME, YELLOW_TIME, GREEN_TIME }; void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); } // the loop function runs over and over again forever void loop() { // red light on trafic_light_on(RED); delay(times[RED]); // keep red light on during a period of time // yellow light on trafic_light_on(YELLOW); delay(times[YELLOW]); // keep yellow light on during a period of time // green light on trafic_light_on(GREEN); delay(times[GREEN]); // keep green light on during a period of time } void trafic_light_on(int light) { for (int i = RED; i <= GREEN; i++) { if (i == light) digitalWrite(pins[i], HIGH); // turn on else digitalWrite(pins[i], LOW); // turn off } }
  • Nous pouvons améliorer le code en utilisant une boucle for.
/* * Ce code Arduino UNO R4 a été développé par newbiely.fr * Ce code Arduino UNO R4 est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/arduino-uno-r4/arduino-uno-r4-traffic-light */ #define PIN_RED 2 // The Arduino UNO R4 pin connected to R pin of traffic light module #define PIN_YELLOW 3 // The Arduino UNO R4 pin connected to Y pin of traffic light module #define PIN_GREEN 4 // The Arduino UNO R4 pin connected to G pin of traffic light module #define RED_TIME 2000 // RED time in millisecond #define YELLOW_TIME 1000 // YELLOW time in millisecond #define GREEN_TIME 2000 // GREEN time in millisecond #define RED 0 // Index in array #define YELLOW 1 // Index in array #define GREEN 2 // Index in array const int pins[] = {PIN_RED, PIN_YELLOW, PIN_GREEN}; const int times[] = {RED_TIME, YELLOW_TIME, GREEN_TIME}; void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); } // the loop function runs over and over again forever void loop() { for (int light = RED; light <= GREEN; light ++) { trafic_light_on(light); delay(times[light]); // keep light on during a period of time } } void trafic_light_on(int light) { for (int i = RED; i <= GREEN; i ++) { if (i == light) digitalWrite(pins[i], HIGH); // turn on else digitalWrite(pins[i], LOW); // turn off } }
  • Améliorons le code en utilisant la fonction millis() plutôt que delay().
/* * Ce code Arduino UNO R4 a été développé par newbiely.fr * Ce code Arduino UNO R4 est mis à disposition du public sans aucune restriction. * Pour des instructions complètes et des schémas de câblage, veuillez visiter: * https://newbiely.fr/tutorials/arduino-uno-r4/arduino-uno-r4-traffic-light */ #define PIN_RED 2 // The Arduino UNO R4 pin connected to R pin of traffic light module #define PIN_YELLOW 3 // The Arduino UNO R4 pin connected to Y pin of traffic light module #define PIN_GREEN 4 // The Arduino UNO R4 pin connected to G pin of traffic light module #define RED_TIME 2000 // RED time in millisecond #define YELLOW_TIME 1000 // YELLOW time in millisecond #define GREEN_TIME 2000 // GREEN time in millisecond #define RED 0 // Index in array #define YELLOW 1 // Index in array #define GREEN 2 // Index in array const int pins[] = { PIN_RED, PIN_YELLOW, PIN_GREEN }; const int times[] = { RED_TIME, YELLOW_TIME, GREEN_TIME }; unsigned long last_time = 0; int light = RED; // start with RED light void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT); trafic_light_on(light); last_time = millis(); } // the loop function runs over and over again forever void loop() { if ((millis() - last_time) > times[light]) { light++; if (light >= 3) light = RED; // new circle trafic_light_on(light); last_time = millis(); } // TO DO: your other code } void trafic_light_on(int light) { for (int i = RED; i <= GREEN; i++) { if (i == light) digitalWrite(pins[i], HIGH); // turn on else digitalWrite(pins[i], LOW); // turn off } }

Vidéo

Références des fonctions

※ NOS MESSAGES

  • N'hésitez pas à partager le lien de ce tutoriel. Cependant, veuillez ne pas utiliser notre contenu sur d'autres sites web. Nous avons investi beaucoup d'efforts et de temps pour créer ce contenu, veuillez respecter notre travail !