Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- // DEFINE CONSTANTES
- // PINs Digitais
- #define SERVO_MOTOR 13
- #define PAINEL_LED 12
- #define RELE_VALVULA 11
- // PINs Analógicos
- #define FOTORESISTOR A0
- #define SENSOR_UMIDADE A1
- // INSTANCIA O OBJETO SERVO
- Servo servo;
- void setup() {
- // DEFINE MODO DE OPERAÇÃO DOS PINs
- // PINs DIGITAIS
- pinMode(SERVO_MOTOR, OUTPUT);
- pinMode(PAINEL_LED, OUTPUT);
- pinMode(RELE_VALVULA, OUTPUT);
- // CONFIGURA O SERVO MOTOR PARA OPERAR NO PIN CONFIGURADO
- servo.attach(SERVO_MOTOR);
- // MOVE O MOTOR PARA 150° AO INICIALIZAR
- moveMotor(150);
- }
- void loop() {
- if(!estaUmido()) {
- digitalWrite(RELE_VALVULA, HIGH);
- } else {
- digitalWrite(RELE_VALVULA, LOW);
- }
- if(!haLuz()) {
- digitalWrite(PAINEL_LED, HIGH);
- moveMotor(90);
- } else {
- moveMotor(150);
- digitalWrite(PAINEL_LED, LOW);
- }
- delay(1000);
- }
- // FUNÇÕES AUXILIARES
- // Verifica se a terra está úmida
- bool estaUmido() {
- return (analogRead(SENSOR_UMIDADE) < 800);
- }
- // Verifica se há iluminação no local
- bool haLuz() {
- return (analogRead(FOTORESISTOR) < 500);
- }
- // Move o eixo do servo motor para o ângulo definido
- void moveMotor(int angulo) {
- int angulo_atual = servo.read();
- if (angulo == angulo_atual) {
- return;
- }
- if (angulo > angulo_atual) {
- for (int i = angulo_atual; i != angulo; i++) {
- servo.write(i);
- delay(25);
- }
- } else {
- for (int i = angulo_atual; i != angulo; i--) {
- servo.write(i);
- delay(25);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement