Arduino Traffic Light with PWM

Asked

Viewed 271 times

0

Hello,

I created an Arduino code to represent the functioning of a semaphore, however I am also using same code to (at the same time) send PWM signals to another device.

The code is working, but I noticed that the control of the semaphore colors are not flowing naturally as it should.

So I would like to know what could be done to solve this problem.

OBS.: I tried to change the value of the variables tempoVermelhoand tempoVerde, but I still couldn’t get the traffic light flowing.

// Definição dos pinos de saída para os LEDs do semáforo
#define VERMELHO 10
#define AMARELO 9
#define VERDE 8

// Definindo um valor máximo para a repetição de cada procedimento no Loop
#define MAX_LOOP 40000

// Variavel responsável por realizar a contagem dos procedimentos realizados no Loop
int interacao = 0;

// Variáveis de controle do tempo gasto em cada cor do semáforo
int tempoVermelho = MAX_LOOP * 0.4;
int tempoVerde =  MAX_LOOP * 0.8;

void setup() {

  // Definição dos pinos dos LEDs como saídas digitais
  pinMode(VERMELHO, OUTPUT);
  pinMode(AMARELO, OUTPUT);
  pinMode(VERDE, OUTPUT);

  // Inicialização da comunicação serial
  Serial.begin(9600);

  // Chamando a função que altera o PWM
  pwmSetup(); 
}

void loop() {

  analogWrite(3, 34);
  analogWrite(11, map(analogRead(A1), 0, 1023, 0, 255));

  // Laço responsável por deixar o sinal fechado enquanto "interacao" for menor que a metade de "MAX_LOOP"
  if(interacao >= 0 && interacao < (tempoVermelho)){
    
    semaforoFecahdo();
    
  }
  
  // Laço responsável por deixar o sinal aberto enquanto "interacao" for menor que 75% de "MAX_LOOP"
  else if(interacao >= (tempoVermelho) && interacao < (tempoVerde)){
    
    semaforoAberto();
    
  }

  // Laço responsável por deixar o sinal fechado enquanto "interacao" for menor que "MAX_LOOP"
  else if(interacao >= (tempoVerde) && interacao < MAX_LOOP){
    
    semaforoAtencao();
    
  }

  // Incrementando o valor da variável "interacao"
  interacao++;

  // Condição responsável por zera a variável "interacao" quando a mesma atinge o valor da variável "MAX_LOOP"
  if(interacao == MAX_LOOP){
    interacao = 0;
  }
}

// Método chamado no Setup para definir o PWM para 31khz de frequência
void pwmSetup() {
  
  pinMode(3 , OUTPUT); //OCR2B 3 e 11 são pinos de PWM do Arduino
  pinMode(11, OUTPUT); //OCR2A
  
  // Alterando os registradores para que a frequencia do PWM seja de 31250hz
  TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20);
  TCCR2B = _BV(CS20);
  OCR2A = 0;
  OCR2B = 0;
  
}

// Métodos auxiliares para o controle de acionamento dos LEDs do semáforo
void semaforoFecahdo(){
  digitalWrite(VERMELHO, HIGH);
  digitalWrite(AMARELO, LOW);  
  digitalWrite(VERDE, LOW);
}

void semaforoAberto(){
  digitalWrite(VERMELHO, LOW);
  digitalWrite(AMARELO, LOW);  
  digitalWrite(VERDE, HIGH);
}

void semaforoAtencao(){
  digitalWrite(VERMELHO, LOW);
  digitalWrite(AMARELO, HIGH);  
  digitalWrite(VERDE, LOW);  
}


  • it would be nice if you quote if ever made any attempt to solve the problem. This helps in community response.

2 answers

1

I don’t know if it’s interesting to you, but there is a concept of Thread, where you can perform "several things at the same time", in fact you run things (threads) in time spaces giving the impression of happening together! So for example runs a thread to control the semaphore and sends the PWM after each color change.. or a time interval! I hope I was clear!

Follow a video of a very good guy on the subject Ivan Seidel (he created a library for this)

Part 1 https://www.youtube.com/watch?v=oeP_NiajWME

Part 2 https://www.youtube.com/watch?v=0SyutmiLzj0

In the video description is his Github with the library file and the example!

I hope I helped! A hug!

1


From what I understand, the problem is that you’re counting calls from the loop function. They are not at equal intervals as the Adian does a number of extra things that can delay execution, and with this gives the impression that it is not "flowing". You need to use functions where you take milliseconds/seconds/etc and use them as a reference in your logic.

On the official website there is a example, but there is one simpler here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.