fork download
  1. // Projeto 4 – Semáforo interativo
  2. int carRed = 12; // estabelece o semáforo para carros
  3. int carYellow = 11;
  4. int carGreen = 10;
  5. int pedRed = 9; // estabelece o semáforo para pedestres
  6. int pedGreen = 8;
  7. int button = 2; // pino do botão
  8. int crossTime = 5000; // tempo para que os pedestres atravessem
  9. unsigned long changeTime; // tempo desde que o botão foi pressionado
  10. void setup() {
  11. pinMode(carRed, OUTPUT);
  12. pinMode(carYellow, OUTPUT);
  13. pinMode(carGreen, OUTPUT);
  14. pinMode(pedRed, OUTPUT);
  15. pinMode(pedGreen, OUTPUT);
  16. pinMode(button, INPUT); // botão no pino 2
  17. // acende a luz verde
  18. digitalWrite(carGreen, HIGH);
  19. digitalWrite(pedRed, HIGH);
  20. }
  21. void loop() {
  22. int state = digitalRead(button);
  23. /* verifica se o botão foi pressionado e se transcorreram 5 segundos desde a última vez que
  24.  isso ocorreu */
  25. if (state == HIGH && (millis() - changeTime) > 5000) {
  26.  
  27. // Chama a função para alterar as luzes
  28. changeLights();
  29. }
  30. }
  31. void changeLights() {
  32. digitalWrite(carGreen, LOW); // apaga o verde
  33. digitalWrite(carYellow, HIGH); // acende o amarelo
  34. delay(2000); // espera 2 segundos
  35. digitalWrite(carYellow, LOW); // apaga o amarelo
  36. digitalWrite(carRed, HIGH); // acende o vermelho
  37. delay(1000); // espera 1 segundo, por segurança
  38. digitalWrite(pedRed, LOW); // apaga o vermelho dos pedestres
  39. digitalWrite(pedGreen, HIGH); // acende o verde dos pedestres
  40. delay(crossTime); // espera por um intervalo de tempo predefinido
  41. // pisca o verde dos pedestres
  42. for (int x=0; x<10; x++) {
  43. digitalWrite(pedGreen, HIGH);
  44. delay(250);
  45. digitalWrite(pedGreen, LOW);
  46. delay(250);
  47. }
  48. // acende o vermelho dos pedestres
  49. digitalWrite(pedRed, HIGH);
  50. delay(500);
  51. digitalWrite(carYellow, HIGH); // acende o amarelo
  52. digitalWrite(carRed, LOW); // apaga o vermelho
  53. delay(1000);
  54. digitalWrite(carGreen, HIGH); // acende o verde
  55. digitalWrite(carYellow, LOW); // apaga o amarelo
  56. // registra o tempo desde a última alteração no semáforo
  57. changeTime = millis();
  58. // depois retorna para o loop principal do programa
  59. }
  60.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘setup’:
prog.c:11:1: warning: implicit declaration of function ‘pinMode’ [-Wimplicit-function-declaration]
 pinMode(carRed, OUTPUT);
 ^~~~~~~
prog.c:11:17: error: ‘OUTPUT’ undeclared (first use in this function)
 pinMode(carRed, OUTPUT);
                 ^~~~~~
prog.c:11:17: note: each undeclared identifier is reported only once for each function it appears in
prog.c:16:17: error: ‘INPUT’ undeclared (first use in this function)
 pinMode(button, INPUT); // botão no pino 2
                 ^~~~~
prog.c:18:1: warning: implicit declaration of function ‘digitalWrite’ [-Wimplicit-function-declaration]
 digitalWrite(carGreen, HIGH);
 ^~~~~~~~~~~~
prog.c:18:24: error: ‘HIGH’ undeclared (first use in this function)
 digitalWrite(carGreen, HIGH);
                        ^~~~
prog.c: In function ‘loop’:
prog.c:22:13: warning: implicit declaration of function ‘digitalRead’ [-Wimplicit-function-declaration]
 int state = digitalRead(button);
             ^~~~~~~~~~~
prog.c:25:14: error: ‘HIGH’ undeclared (first use in this function)
 if (state == HIGH && (millis() - changeTime) > 5000) {
              ^~~~
prog.c:25:23: warning: implicit declaration of function ‘millis’ [-Wimplicit-function-declaration]
 if (state == HIGH && (millis() - changeTime) > 5000) {
                       ^~~~~~
prog.c:28:1: warning: implicit declaration of function ‘changeLights’ [-Wimplicit-function-declaration]
 changeLights();
 ^~~~~~~~~~~~
prog.c: At top level:
prog.c:31:6: warning: conflicting types for ‘changeLights’
 void changeLights() {
      ^~~~~~~~~~~~
prog.c:28:1: note: previous implicit declaration of ‘changeLights’ was here
 changeLights();
 ^~~~~~~~~~~~
prog.c: In function ‘changeLights’:
prog.c:32:24: error: ‘LOW’ undeclared (first use in this function)
 digitalWrite(carGreen, LOW); // apaga o verde
                        ^~~
prog.c:33:25: error: ‘HIGH’ undeclared (first use in this function)
 digitalWrite(carYellow, HIGH); // acende o amarelo
                         ^~~~
prog.c:34:1: warning: implicit declaration of function ‘delay’ [-Wimplicit-function-declaration]
 delay(2000); // espera 2 segundos
 ^~~~~
stdout
Standard output is empty