samuelcarreira

Arduino: Como ligar botões de uma forma “profissional

Sep 4th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * tutorial_botao.ino - Tutorial da utilização da biblioteca
  3.  * Bounce. Controle do brilho de um LED através de dois botões
  4.  *
  5.  * escrito por SamC
  6.  * Versão 1.0.0
  7. */
  8.  
  9. #include <Bounce.h> // biblioteca utilizada
  10.  
  11. // definição dos pinos de ligação
  12. #define LED_PIN 11
  13. #define AUMENTAR_PIN 8
  14. #define DIMINUIR_PIN 7
  15.  
  16. byte brilho = 0; // valor do brilho do LED entre 0 e 255
  17.  
  18. #define DEBOUNCE_TIME 5 // tempo em ms
  19. Bounce aumentar = Bounce(AUMENTAR_PIN, DEBOUNCE_TIME);
  20. Bounce diminuir = Bounce(DIMINUIR_PIN, DEBOUNCE_TIME);
  21.  
  22. void setup()  {
  23.   pinMode(LED_PIN, OUTPUT);
  24.   pinMode(AUMENTAR_PIN, INPUT_PULLUP);
  25.   pinMode(DIMINUIR_PIN, INPUT_PULLUP);
  26.  
  27.   Serial.begin(9600);
  28.   Serial.println("Tutorial Botao v.1.0.0");
  29. }
  30.  
  31.  
  32. void loop()  {
  33.   analogWrite(LED_PIN, brilho);
  34.  
  35.   if (aumentar.update() ) {
  36.     if (aumentar.read() == LOW ) {
  37.       aumentar.rebounce(250);
  38.      
  39.       if (brilho < 255)
  40.         brilho ++;
  41.        
  42.       Serial.print("Aumentar o brilho do LED: ");
  43.       Serial.println(brilho);
  44.     }
  45.   }
  46.  
  47.   if (diminuir.update() ) {
  48.     if (diminuir.read()== LOW ) {
  49.       diminuir.rebounce(250);
  50.      
  51.       if (brilho > 0)
  52.         brilho --;
  53.        
  54.       Serial.print("Diminuir o brilho do LED: ");
  55.       Serial.println(brilho);
  56.     }
  57.   }
  58. }
Add Comment
Please, Sign In to add comment