Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. // DEFINE CONSTANTES
  4.  
  5. // PINs Digitais
  6. #define SERVO_MOTOR 13
  7. #define PAINEL_LED 12
  8. #define RELE_VALVULA 11
  9.  
  10. // PINs Analógicos
  11. #define FOTORESISTOR A0
  12. #define SENSOR_UMIDADE A1
  13.  
  14. // INSTANCIA O OBJETO SERVO
  15. Servo servo;
  16.  
  17.  
  18. void setup() {
  19.  
  20. // DEFINE MODO DE OPERAÇÃO DOS PINs
  21.  
  22. // PINs DIGITAIS
  23. pinMode(SERVO_MOTOR, OUTPUT);
  24. pinMode(PAINEL_LED, OUTPUT);
  25. pinMode(RELE_VALVULA, OUTPUT);
  26.  
  27. // CONFIGURA O SERVO MOTOR PARA OPERAR NO PIN CONFIGURADO
  28. servo.attach(SERVO_MOTOR);
  29.  
  30. // MOVE O MOTOR PARA 150° AO INICIALIZAR
  31. moveMotor(150);
  32.  
  33. }
  34.  
  35. void loop() {
  36.  
  37. if(!estaUmido()) {
  38. digitalWrite(RELE_VALVULA, HIGH);
  39. } else {
  40. digitalWrite(RELE_VALVULA, LOW);
  41. }
  42. if(!haLuz()) {
  43. digitalWrite(PAINEL_LED, HIGH);
  44. moveMotor(90);
  45. } else {
  46. moveMotor(150);
  47. digitalWrite(PAINEL_LED, LOW);
  48. }
  49.  
  50. delay(1000);
  51.  
  52. }
  53.  
  54. // FUNÇÕES AUXILIARES
  55.  
  56. // Verifica se a terra está úmida
  57.  
  58. bool estaUmido() {
  59. return (analogRead(SENSOR_UMIDADE) < 800);
  60. }
  61.  
  62. // Verifica se há iluminação no local
  63.  
  64. bool haLuz() {
  65. return (analogRead(FOTORESISTOR) < 500);
  66. }
  67.  
  68. // Move o eixo do servo motor para o ângulo definido
  69.  
  70. void moveMotor(int angulo) {
  71. int angulo_atual = servo.read();
  72. if (angulo == angulo_atual) {
  73. return;
  74. }
  75. if (angulo > angulo_atual) {
  76. for (int i = angulo_atual; i != angulo; i++) {
  77. servo.write(i);
  78. delay(25);
  79. }
  80. } else {
  81. for (int i = angulo_atual; i != angulo; i--) {
  82. servo.write(i);
  83. delay(25);
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement