Advertisement
RuiViana

Botao+2Leds

Oct 18th, 2016
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. /*
  2.   Preciso ligar um led quando botão high,
  3.   se esta low desliga,
  4.   se apertar outra vez, liga o outro led,
  5.   se soltar desliga, e segue nesse loop
  6. */
  7. #define botao 2         // Botao
  8. #define LedA 12         // Leda par uma vez
  9. #define LedB 13         // Led final
  10. byte Flag = 0;          // Flag de controle
  11. byte debouc = 5;        // Valor de delay para debouncing
  12. //------------------------------
  13. void setup()
  14. {
  15.   pinMode(botao, INPUT_PULLUP);             // Define port de botao como entrada
  16.   pinMode(LedB, OUTPUT);                    // Define port LedA como saida
  17.   pinMode(LedB, OUTPUT);                    // Define port LedB como saida
  18. }
  19. //------------------------------
  20. void loop()
  21. {
  22.   if (Flag == 0)                              // Fase 0 acender LedA
  23.   {
  24.     if (digitalRead(botao) == HIGH)           // Se o botao esta liberado
  25.     {
  26.       digitalWrite(LedA, HIGH);               // LedA acesso
  27.       Flag = 1;                               // Informa LedA acesso
  28.     }
  29.   }
  30.   if (Flag == 1)                              // Fase 1 apagar LedA
  31.   {
  32.     while (digitalRead(botao) == LOW)         // Enquanto o botao estiver apertado
  33.     {
  34.       delay(debouc);
  35.       while (digitalRead(botao) == LOW)       // Enquanto o botao continua apertado
  36.       {
  37.         delay(debouc);
  38.         digitalWrite(LedA, LOW);              // LedA apagado
  39.         Flag = 2;                             // Informa LedA apagado
  40.       }
  41.       delay(debouc);
  42.     }
  43.   }
  44.   if (Flag == 2)                                // Fase 2 acender LedB
  45.   {
  46.     {
  47.       while (digitalRead(botao) == LOW)         // Enquanto o botao estiver apertado
  48.       {
  49.         delay(debouc);
  50.         while (digitalRead(botao) == LOW)       // Enquanto o botao continua apertado
  51.         {
  52.           delay(debouc);
  53.           digitalWrite(LedB, HIGH);             // LedB aceso
  54.           if (digitalRead(botao) == HIGH)       // Se o botao foi liberado
  55.           {
  56.             Flag = 3;                           // Informa LedB acesso
  57.           }
  58.         }
  59.       }
  60.     }
  61.   }
  62.   if (Flag == 3)                                // Fase 3 apagar LedB
  63.   {
  64.     if (digitalRead(botao) == HIGH)             // Se o botao estiver liberado
  65.     {
  66.       delay(debouc);
  67.       if (digitalRead(botao) == HIGH)           // Se o botao estiver liberado
  68.       {
  69.         digitalWrite(LedB, LOW);                // Apaga LedB
  70.         Flag = 0;                               // Informa LedB apagado
  71.       }
  72.     }
  73.   }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement