Arduino UNO R4 - Semáforo

Neste guia, vamos aprender a controlar um módulo de semáforo usando o Arduino UNO R4. Vamos abordar:

Arduino UNO R4 semáforo

Sobre o Módulo de Semáforo

Pinagem

Um módulo de semáforo tem 4 pinos:

  • Pino GND: Este é o pino de terra. Conecte-o ao GND do Arduino UNO R4.
  • Pino R: Este pino controla a luz vermelha. Conecte-o a uma saída digital do Arduino UNO R4.
  • Pino Y: Este pino controla a luz amarela. Conecte-o a uma saída digital do Arduino UNO R4.
  • Pino G: Este pino controla a luz verde. Conecte-o a uma saída digital do Arduino UNO R4.
Pinagem do Semáforo

Como Funciona

Diagrama de Ligação

Diagrama de Ligação do semáforo com Arduino UNO R4

This image is created using Fritzing. Click to enlarge image

Veja A melhor forma de alimentar o Arduino Uno R4 e outros componentes.

Como Programar o Módulo de Semáforo

  • Configure os pinos de um Arduino UNO R4 como saídas digitais usando a função pinMode().
pinMode(PIN_RED, OUTPUT); pinMode(PIN_YELLOW, OUTPUT); pinMode(PIN_GREEN, OUTPUT);
  • Programe para acionar a luz vermelha usando a função 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

Código Arduino UNO R4

/* * Este código de Arduino UNO R4 foi desenvolvido por pt.newbiely.com * Este código de Arduino UNO R4 é disponibilizado ao público sem nenhuma restrição. * Para tutoriais completos e diagramas de ligação, visite: * https://pt.newbiely.com/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 }

Passos Rápidos

Siga estas instruções passo a passo:

  • Se esta é a primeira vez que você usa o Arduino Uno R4 WiFi/Minima, consulte o tutorial sobre como configurar o ambiente para o Arduino Uno R4 WiFi/Minima na Arduino IDE.
  • Conecte o módulo de semáforo ao Arduino Uno R4 de acordo com o diagrama fornecido.
  • Conecte a placa Arduino Uno R4 ao seu computador usando um cabo USB.
  • Abra a Arduino IDE no seu computador.
  • Selecione a placa Arduino Uno R4 apropriada (por exemplo, Arduino Uno R4 WiFi) e a porta COM.
  • Copie o código e abra-o na Arduino IDE.
  • Clique no botão Upload na Arduino IDE para fazer upload do código para o Arduino UNO R4.
  • Confira o módulo de semáforo.

Os semáforos funcionam de maneira diferente dependendo do projeto usado em cada região. As informações fornecidas aqui dão uma ideia básica de como os semáforos ajudam a controlar o trânsito.

O código mostrado acima permite controlar cada luz separadamente. Agora, vamos melhorar o código para que ele funcione melhor.

Otimização do Código Arduino UNO R4

  • Vamos melhorar o código adicionando uma função para controlar a luz.
/* * Este código de Arduino UNO R4 foi desenvolvido por pt.newbiely.com * Este código de Arduino UNO R4 é disponibilizado ao público sem nenhuma restrição. * Para tutoriais completos e diagramas de ligação, visite: * https://pt.newbiely.com/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 } }
  • Podemos melhorar o código usando um loop for.
/* * Este código de Arduino UNO R4 foi desenvolvido por pt.newbiely.com * Este código de Arduino UNO R4 é disponibilizado ao público sem nenhuma restrição. * Para tutoriais completos e diagramas de ligação, visite: * https://pt.newbiely.com/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 } }
  • Vamos melhorar o código usando a função millis() em vez de delay().
/* * Este código de Arduino UNO R4 foi desenvolvido por pt.newbiely.com * Este código de Arduino UNO R4 é disponibilizado ao público sem nenhuma restrição. * Para tutoriais completos e diagramas de ligação, visite: * https://pt.newbiely.com/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 } }

Vídeo

Referências de Funções

※ NOSSAS MENSAGENS

  • Fique à vontade para compartilhar o link deste tutorial. Porém, por favor, não use nosso conteúdo em outros sites. Investimos muito esforço e tempo para criar este conteúdo, por favor respeite nosso trabalho!