Advertisement
RuiViana

Sequencial com botão

Nov 7th, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. // Rui
  2. // Seguencia LEDs usando o CI 74HC595
  3.  
  4. #define clock 2 // Conectar ao pino 11 dos 74HC595
  5. #define data 3 // Conectar ao pino 14 dos 74HC595
  6. #define latch 4 // Conectar ao pino 12 dos 74HC595
  7. #define botao 8 // Botão ligado no pino 8
  8. bool Level; // Nivel de Data
  9. bool estadoB = LOW; // Indicador se botão foi acionado
  10. byte Cnt = 0; // Contador de saidas
  11. // ********************************************************************
  12. void setup()
  13. {
  14. Serial.begin(9600);
  15. pinMode(clock,OUTPUT); // Port D2 saída
  16. pinMode(data,OUTPUT); // Port D3 saída
  17. pinMode(latch,OUTPUT); // Port D4 saída
  18. pinMode (botao, INPUT_PULLUP); // Botão para avancar
  19. Apaga(); // Apaga tudo antes de iniciar
  20. }
  21. //----------------------------------------------------------------------
  22. void HC595()
  23. {
  24. digitalWrite(data,Level); // Data bit ON acende 1o. LED
  25. digitalWrite(latch,LOW); // transfere dados dos regs para os latchs e saídas
  26. digitalWrite(clock,HIGH); // Liga clock para entra dado
  27. digitalWrite(clock,LOW); // Desiga clock
  28. digitalWrite(latch,HIGH); // Desatica transferencia
  29. }
  30. //---------------------------------------------------------------
  31. void Apaga () // Rotina para apagar tudo
  32. {
  33. Level = 0; // Define nivel das saidas
  34. Cnt = 0; // Contador de saida
  35. for (unsigned int i = 0; i<8; i++) // Faça 8 vezes
  36. {
  37. HC595(); // Envia para o 595
  38. }
  39. }
  40. // ********************************************************************
  41. void loop()
  42. {
  43. while (digitalRead (botao) == HIGH) // Se botão não está acionado
  44. {
  45. estadoB = HIGH; // Diga que ele esta HIGH
  46. }
  47. while (digitalRead (botao) == LOW) // Se botão está acionado
  48. {
  49. if (estadoB == HIGH) // Se ele foi acionado
  50. {
  51. estadoB = LOW; //. Limpa a indicação
  52. // Serial.println(Cnt);
  53. Cnt++; // Incrementa saida
  54. Level = 1; // inverta o nivel de Data
  55. HC595(); // Envia para o 595
  56. delay(100); // Tempo de 50 ms
  57. }
  58. }
  59. if (Cnt >8) // Se a saida é maior que 8, apaga tudo
  60. {
  61. Apaga();
  62. }
  63. } // End loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement