Advertisement
Guest User

teste millis

a guest
Nov 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. const int carGreen = 5;
  2. const int carYellow = 6;
  3. const int carRed = 7;
  4. const int pedGreen = 3;
  5. const int pedRed = 4;
  6. const int button = 2;
  7.  
  8. const int TIME_ATRASO = 2000; //MILI SEGUNDOS
  9. const int TIME_AMARELO = 2000;
  10. const int TIME_ESPERA = 1000;
  11. const int TIME_VERDE_P = 6000;
  12. const int TIME_VERDE_P_FIM = 2000;
  13.  
  14. const int TIME_TOTAL = TIME_ATRASO + TIME_AMARELO + TIME_ESPERA * 2 + TIME_VERDE_P + TIME_VERDE_P_FIM;
  15.  
  16. long millis_referencia = 0;
  17.  
  18. void setup() {
  19. pinMode(carGreen, OUTPUT);
  20. pinMode(carYellow, OUTPUT);
  21. pinMode(carRed, OUTPUT);
  22. pinMode(pedGreen, OUTPUT);
  23. pinMode(pedRed, OUTPUT);
  24. pinMode(button, INPUT);
  25. }
  26.  
  27. void loop() {
  28.  
  29. if (apertou_botao() )
  30. { semaforoPedestre(); }
  31. else
  32. { semaforoCarro(); }
  33. }
  34.  
  35. int apertou_botao(){
  36.  
  37. if ( (millis_referencia == 0 ) && digitalRead(button) ) { millis_referencia = millis(); }
  38. if ( (millis_referencia > 0 ) && ( millis() < (millis_referencia + TIME_TOTAL) ) ) { return true; }
  39.  
  40. millis_referencia = 0;
  41. return false;
  42. }
  43.  
  44. void piscaLed(int pin, int timeOn, int timeOff, int atraso, long millis_referencia = 0){
  45. long ajuste_referencia = millis_referencia % (timeOn + timeOff);
  46. long resto = (millis() + timeOn + timeOff - ajuste_referencia - atraso) % (timeOn + timeOff);
  47.  
  48. digitalWrite(pin, resto < timeOn ? HIGH : LOW);
  49. }
  50.  
  51. int verificaTime(int time1, int time2, int atraso, long millis_referencia=0){
  52. long ajuste_referencia = millis_referencia % (time1 + time2);
  53. long resto = (millis() + time1 + time2 - ajuste_referencia - atraso) % (time1 + time2);
  54.  
  55. return (resto < time1 ? 1 : 2 );
  56. }
  57.  
  58. void semaforoCarro(){
  59. digitalWrite(carGreen, HIGH);
  60. digitalWrite(carYellow, LOW);
  61. digitalWrite(carRed, LOW);
  62. digitalWrite(pedGreen, LOW);
  63. digitalWrite(pedRed, HIGH);
  64. }
  65.  
  66. void semaforoPedestre(){
  67.  
  68. piscaLed(carGreen, TIME_ATRASO, TIME_TOTAL-
  69. TIME_ATRASO, 0, millis_referencia );
  70.  
  71. piscaLed(carYellow, TIME_AMARELO, TIME_TOTAL-
  72. TIME_AMARELO, TIME_ATRASO, millis_referencia );
  73.  
  74. piscaLed(carRed, TIME_ESPERA*2+
  75. TIME_VERDE_P+
  76. TIME_VERDE_P_FIM, TIME_TOTAL-
  77. TIME_ATRASO-
  78. TIME_AMARELO, TIME_ATRASO+
  79. TIME_AMARELO, millis_referencia );
  80.  
  81. int t;
  82. t = verificaTime( TIME_TOTAL-
  83. TIME_VERDE_P_FIM, TIME_VERDE_P_FIM, TIME_TOTAL-
  84. TIME_ESPERA, millis_referencia );
  85.  
  86. if (t==1) {
  87. piscaLed(pedGreen, TIME_VERDE_P, TIME_TOTAL-
  88. TIME_VERDE_P, TIME_ATRASO+
  89. TIME_AMARELO+
  90. TIME_ESPERA, millis_referencia );
  91. } else {
  92. piscaLed(pedRed, 100, 100, 100, millis_referencia );
  93. }
  94.  
  95.  
  96. piscaLed(pedRed, TIME_TOTAL-
  97. TIME_VERDE_P-
  98. TIME_VERDE_P_FIM, TIME_VERDE_P+
  99. TIME_VERDE_P_FIM, TIME_TOTAL-
  100. TIME_ESPERA, millis_referencia );
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement