Advertisement
RuiViana

Untitled

May 9th, 2015
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. // Rotina para controle de motor. (110v ou 220v)
  2. // Uso didático
  3. // Rui 09/05/2015
  4.  
  5. //*************** Variaveis e constantes *************
  6. const int Motor = 13; // Pino de disparo do Triac
  7. const int Start = 4; // Entrada switch start
  8. const int Stop = 5; // Entrada switch stop
  9. const int EPO = 6; // Entrada switch EPO
  10. const int Sensor = 7; // Entrada switch sensor
  11. const int Reset = 8; // Entrada switch reset
  12.  
  13. unsigned int debounceDelay = 50; // Tempo de debounce
  14.  
  15. unsigned int FlagMotor;
  16. unsigned int FlagStart;
  17. unsigned int FlagEPO;
  18. unsigned int FlagSensor;
  19. //*********************** Setup ********************
  20. void setup()
  21. {
  22. // Serial.begin(9600); // Inicializa a serial a 9600 bps para Debug
  23.  
  24. pinMode(Motor, OUTPUT); // Port como saída
  25. digitalWrite(Motor, LOW); // Desliga motor
  26.  
  27. pinMode(Start, INPUT); // Port como entrada
  28. pinMode(Stop, INPUT);
  29. pinMode(EPO, INPUT);
  30. pinMode(Sensor, INPUT);
  31. pinMode(Reset, INPUT);
  32.  
  33. digitalWrite(Start, HIGH); // Puul-Up Entrada
  34. digitalWrite(Stop, HIGH);
  35. digitalWrite(EPO, HIGH);
  36. digitalWrite(Sensor, HIGH);
  37. digitalWrite(Reset, HIGH);
  38. }
  39. //*********************** Loop ********************
  40. void loop()
  41. {
  42. // ================= Star deboucing ===================
  43. if(!digitalRead(Start))
  44. {
  45. delay(debounceDelay);
  46. if(!digitalRead(Start))
  47. {
  48. FlagStart = 1;
  49. while(!digitalRead(Start)) // Espera pela liberação do switch
  50. delayMicroseconds(1);
  51. }
  52. }
  53. // ================= Stop deboucing ===================
  54. if(!digitalRead(Stop))
  55. {
  56. delay(debounceDelay);
  57. if(!digitalRead(Stop))
  58. {
  59. FlagStart = 0;
  60. while(!digitalRead(Stop)) // Espera pela liberação do switch
  61. delayMicroseconds(1);
  62. }
  63. }
  64. // ================= EPO deboucing ===================
  65. if(!digitalRead(EPO))
  66. {
  67. delay(debounceDelay);
  68. if(!digitalRead(EPO))
  69. {
  70. FlagStart = 0;
  71. FlagEPO = 1;
  72. digitalWrite(Motor, LOW);
  73. while(!digitalRead(EPO)) // Espera pela liberação do switch
  74. delayMicroseconds(1);
  75. }
  76. }
  77. // ================= Sensor deboucing ===================
  78. if(!digitalRead(Sensor))
  79. {
  80. delay(debounceDelay);
  81. if(!digitalRead(Sensor))
  82. {
  83. FlagStart = 0;
  84. FlagSensor = 1;
  85. digitalWrite(Motor, LOW);
  86. while(!digitalRead(Sensor)) // Espera pela liberação do switch
  87. delayMicroseconds(1);
  88. }
  89. }
  90. // ================= Reset deboucing ===================
  91. if(!digitalRead(Reset))
  92. {
  93. delay(debounceDelay);
  94. if(!digitalRead(Reset))
  95. {
  96. FlagStart = 0;
  97. FlagEPO = 0;
  98. FlagSensor = 0;
  99. digitalWrite(Motor, LOW);
  100. while(!digitalRead(Reset)) // Espera pela liberação do switch
  101. delayMicroseconds(1);
  102. }
  103. }
  104. // ================= Controle ===========================
  105.  
  106. /* //------------------ Para Debug -----------------
  107. Serial.print ("FlagStart ");
  108. Serial.println (FlagStart);
  109. Serial.print ("FlagEPO ");
  110. Serial.println (FlagEPO);
  111. Serial.print ("FlagSensor ");
  112. Serial.println (FlagSensor);
  113. */ //------------------ Para Debug -----------------
  114.  
  115. if ((FlagEPO == 0)&&(FlagSensor == 0))
  116. {
  117. if (FlagStart == 1) digitalWrite(Motor, HIGH);
  118. else digitalWrite(Motor, LOW);
  119. }
  120. // Serial.println (" "); // Para Debug
  121. // delay(500); // Para Debug
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement