sexta-feira, julho 02, 2010

Simulação de um semáforo de trânsito com Arduino



Vídeo demostrando a simulação de um semáforo de trânsito com Arduino.

Hoje dia de jogo da seleção brasileira na copa do mundo da África, dia de folga, dia de arrancar os cabelos com a desclassificação e de aprender mais e mais sobre o meu novo brinquedinho, meu Arduino.
 
Como eu já tinha demonstrado antes aqui no blog, eu já tinha feito algumas brincadeiras com PICs, escrevendo código em Assembly. Mas tem muita coisa que realmente é chato nisso, como ficar trabalhando com os malditos registradores da linguagem, estouro de timer, estouro de variáveis, bit clear, etc. Então fui fazer meu semáforo no Arduino que eu ganho mais ;)
 
A idéia era simular o que vemos nas ruas. Para isso consideremos duas hipotéticas avenidas chamadas A e B.
 
O programa inicia liberando o tráfego de veículos (LED verde) na avenida A e bloqueando-o na avenida B (LED vermelho). O que se segue é o que conhecemos bem: sinal amarelo na avenida A, sinal vermelho na avenida A e verde na B e depois o contrário, amarelo na avenida B, vermelho na avenida B e verde na A novamente.
 
O vídeo está no YouTube para quem quiser ver. Eis a URL: http://www.youtube.com/watch?v=-bUbkyEs3CM
 
E aqui o código fonte:
 

/******************************************************************************;
; Processor: ATMEGA328 ;
; Function : This circuit simulates a typically operation of a traffic light. ;
; There is two sets of LEDs, one representing an hypothetical ;
; avenue "A" and the other set representing an hypothetical avenue ;
; "B". When the program starts the traffic on avenue "A" will be ;
; released and the traffic will be stopped on avenue "B". ;
; After 3s the traffic will be stopped on avenue "A" and will be ;
; released on avenue "B". Before this change, the alert light ;
; (yellow LED) will appear turned on for 1,5s on avenue's "A" ;
; traffic light. After this operation the circle will be reversed. ;
; Hardware : Arduino Duemilanove and an protoboard ;
; Filename : main.pde ;
; Author : Jean J. Míchel ;
; Website : http://www.jeanjmichel.blogspot.com ;
; Credit : ;
;******************************************************************************/

//Traffic light of avenue A
int avenueARed = 12;
int avenueAYellow = 11;
int avenueAGreen = 10;

//Traffic light of avenue B
int avenueBRed = 2;
int avenueBYellow = 3;
int avenueBGreen = 4;

void setup() { //This section will be performed just
pinMode(avenueARed ,OUTPUT); //once after the Arduino be energized
pinMode(avenueAYellow ,OUTPUT); //and sets the orientation of ATmega
pinMode(avenueAGreen ,OUTPUT); //ports

pinMode(avenueBRed ,OUTPUT);
pinMode(avenueBYellow ,OUTPUT);
pinMode(avenueBGreen ,OUTPUT);
}

void openAvenueA() { //Method for release the traffic on
digitalWrite(avenueARed ,LOW); //avenue A
digitalWrite(avenueAYellow ,LOW);
digitalWrite(avenueAGreen ,HIGH);
}

void closeAvenueA() { //Method for stop the traffic on
digitalWrite(avenueARed ,HIGH); //avenue A
digitalWrite(avenueAYellow ,LOW);
digitalWrite(avenueAGreen ,LOW);
}

void prepareToCloseAvenueA() { //Method to display the warning light
digitalWrite(avenueAYellow ,HIGH); //on avenue A
}

void openAvenueB() { //Method for release the traffic on
digitalWrite(avenueBRed ,LOW); //avenue B
digitalWrite(avenueBYellow ,LOW);
digitalWrite(avenueBGreen ,HIGH);
}

void prepareToCloseAvenueB() { //Method to display the warning light
digitalWrite(avenueBYellow ,HIGH); //on avenue B
}

void closeAvenueB() { //Method for stop the traffic on
digitalWrite(avenueBRed ,HIGH); //avenue B
digitalWrite(avenueBYellow ,LOW);
digitalWrite(avenueBGreen ,LOW);
}

void loop() { //This method will be performed over
openAvenueA(); //and over again as long as the
closeAvenueB(); //Arduino has energized

delay(3000);
prepareToCloseAvenueA();
delay(1500);
closeAvenueA();
openAvenueB();

delay(3000);
prepareToCloseAvenueB();
delay(1500);
closeAvenueB();
}

Nenhum comentário: