Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int carGreen = 5;
- const int carYellow = 6;
- const int carRed = 7;
- const int pedGreen = 3;
- const int pedRed = 4;
- const int button = 2;
- const int TIME_ATRASO = 2000; //MILI SEGUNDOS
- const int TIME_AMARELO = 2000;
- const int TIME_ESPERA = 1000;
- const int TIME_VERDE_P = 6000;
- const int TIME_VERDE_P_FIM = 2000;
- const int TIME_TOTAL = TIME_ATRASO + TIME_AMARELO + TIME_ESPERA * 2 + TIME_VERDE_P + TIME_VERDE_P_FIM;
- long millis_referencia = 0;
- void setup() {
- pinMode(carGreen, OUTPUT);
- pinMode(carYellow, OUTPUT);
- pinMode(carRed, OUTPUT);
- pinMode(pedGreen, OUTPUT);
- pinMode(pedRed, OUTPUT);
- pinMode(button, INPUT);
- }
- void loop() {
- if (apertou_botao() )
- { semaforoPedestre(); }
- else
- { semaforoCarro(); }
- }
- int apertou_botao(){
- if ( (millis_referencia == 0 ) && digitalRead(button) ) { millis_referencia = millis(); }
- if ( (millis_referencia > 0 ) && ( millis() < (millis_referencia + TIME_TOTAL) ) ) { return true; }
- millis_referencia = 0;
- return false;
- }
- void piscaLed(int pin, int timeOn, int timeOff, int atraso, long millis_referencia = 0){
- long ajuste_referencia = millis_referencia % (timeOn + timeOff);
- long resto = (millis() + timeOn + timeOff - ajuste_referencia - atraso) % (timeOn + timeOff);
- digitalWrite(pin, resto < timeOn ? HIGH : LOW);
- }
- int verificaTime(int time1, int time2, int atraso, long millis_referencia=0){
- long ajuste_referencia = millis_referencia % (time1 + time2);
- long resto = (millis() + time1 + time2 - ajuste_referencia - atraso) % (time1 + time2);
- return (resto < time1 ? 1 : 2 );
- }
- void semaforoCarro(){
- digitalWrite(carGreen, HIGH);
- digitalWrite(carYellow, LOW);
- digitalWrite(carRed, LOW);
- digitalWrite(pedGreen, LOW);
- digitalWrite(pedRed, HIGH);
- }
- void semaforoPedestre(){
- piscaLed(carGreen, TIME_ATRASO, TIME_TOTAL-
- TIME_ATRASO, 0, millis_referencia );
- piscaLed(carYellow, TIME_AMARELO, TIME_TOTAL-
- TIME_AMARELO, TIME_ATRASO, millis_referencia );
- piscaLed(carRed, TIME_ESPERA*2+
- TIME_VERDE_P+
- TIME_VERDE_P_FIM, TIME_TOTAL-
- TIME_ATRASO-
- TIME_AMARELO, TIME_ATRASO+
- TIME_AMARELO, millis_referencia );
- int t;
- t = verificaTime( TIME_TOTAL-
- TIME_VERDE_P_FIM, TIME_VERDE_P_FIM, TIME_TOTAL-
- TIME_ESPERA, millis_referencia );
- if (t==1) {
- piscaLed(pedGreen, TIME_VERDE_P, TIME_TOTAL-
- TIME_VERDE_P, TIME_ATRASO+
- TIME_AMARELO+
- TIME_ESPERA, millis_referencia );
- } else {
- piscaLed(pedRed, 100, 100, 100, millis_referencia );
- }
- piscaLed(pedRed, TIME_TOTAL-
- TIME_VERDE_P-
- TIME_VERDE_P_FIM, TIME_VERDE_P+
- TIME_VERDE_P_FIM, TIME_TOTAL-
- TIME_ESPERA, millis_referencia );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement