Advertisement
RuiViana

Untitled

May 20th, 2015
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. // Liga led com tempos variados V03
  2. // Evita debouncing
  3. // para uso educativo
  4. // Rui 20/05/2015
  5.  
  6. int Botao = 2; // Botão tem que ser no pino 2
  7. int Led[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Leds
  8.  
  9. long Tempo[10]; // tempo de acendimento do LED
  10. int BotaoSeq = 0; // Sequencia de acendimento do LED
  11.  
  12. long debouncing_time = 15; //Debouncing Time in Milliseconds
  13. volatile unsigned long last_micros;
  14.  
  15. // ------------------------------- Seup ---------------------------
  16. void setup()
  17. {
  18. pinMode(Botao,INPUT); // botao entrada
  19. digitalWrite(Botao,HIGH); // Pull-up
  20.  
  21. for(int i=0; i<10; i++)
  22. {
  23. pinMode(Led[i], OUTPUT);
  24. }
  25. attachInterrupt(0, debounceInterrupt, FALLING); // Enable interrupt 0 no pino D2
  26. }
  27. // ------------------------------- Debouncing ---------------------------
  28. void debounceInterrupt()
  29. {
  30. if((long)(micros() - last_micros) >= debouncing_time * 1000)
  31. {
  32. LigaLed();
  33. last_micros = micros();
  34. }
  35. }
  36. // ---------------------------------- LigaLed -------------------------------
  37. void LigaLed() // Rotina de tratamento do itrpt
  38. {
  39. if (BotaoSeq == 10) BotaoSeq = 0; // Se contagem = 10 zera contagem
  40. Tempo[BotaoSeq] = 1; // Tempo = 1 no Led indicado pelo botão
  41. for (unsigned j = 0; j < 10; j++) // Faça 10 vezes
  42. {
  43. if (Tempo[j] > 0 ) // Se tempo maior que zero
  44. {
  45. Tempo[j] = Tempo[j] + 10000; // Some mais 10000 (ajustar este tempo para 1seg)
  46. }
  47. }
  48. BotaoSeq++; // Incrementa contagem de LED
  49. }
  50. // --------------------------- loop --------------------------
  51. void loop()
  52. {
  53. for(;;) // Faça sempre
  54. {
  55. for (unsigned k = 0; k < 10; k++) // Faça 10 vezes
  56. {
  57. Tempo[k]--; // Decrementa tempo de cada LED
  58. if (Tempo[k]< 0) Tempo[k] = 0; // Se ficar negativo, zera
  59. }
  60.  
  61. for (unsigned L = 0; L < 10; L++) // Faça 10 vezes
  62. {
  63. if (Tempo[L] >0) digitalWrite(Led[L],HIGH); // Se tiver tempo acende LED
  64. else digitalWrite(Led[L],LOW); // Se não apague
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement