Untitled
unknown
plain_text
9 months ago
2.7 kB
1
Indexable
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #include <Servo.h> #include <TimeLib.h> // Library untuk waktu #define DHTPIN 2 // Pin DHT22 #define DHTTYPE DHT22 // DHT 22 (AM2302) #define RELAY_PIN 3 // Pin Relay #define SERVO_PIN 9 // Pin Servo #define SCREEN_WIDTH 128 // OLED display width #define SCREEN_HEIGHT 64 // OLED display height DHT dht(DHTPIN, DHTTYPE); Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); Servo fishFeederServo; int lastFeedHour = -1; void setup() { pinMode(RELAY_PIN, OUTPUT); digitalWrite(RELAY_PIN, LOW); // Initially off fishFeederServo.attach(SERVO_PIN); fishFeederServo.write(0); // Initial position Serial.begin(9600); dht.begin(); if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for (;;); } display.display(); delay(2000); display.clearDisplay(); // Set initial time for testing purposes setTime(6, 59, 50, 1, 1, 2024); // hour, minute, second, day, month, year } void loop() { // Update current time float humidity = dht.readHumidity(); if (isnan(humidity)) { Serial.println(F("Failed to read from DHT sensor!")); return; } // Control relay based on humidity if (humidity < 65) { digitalWrite(RELAY_PIN, HIGH); } else if (humidity > 80) { digitalWrite(RELAY_PIN, LOW); } // Display data on OLED display.clearDisplay(); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.setCursor(0, 0); display.print("Humidity: "); display.print(humidity); display.println(" %"); if (digitalRead(RELAY_PIN) == HIGH) { display.println("Spray: ON"); } else { display.println("Spray: OFF"); } display.setCursor(0, 20); display.print("Time: "); display.print(hour()); display.print(":"); display.print(minute()); display.print(":"); display.print(second()); // Fish feeder at specific times int currentHour = hour(); if ((currentHour == 7 || currentHour == 12 || currentHour == 17 || currentHour == 22) && currentHour != lastFeedHour) { feedFish(); lastFeedHour = currentHour; // Update last feed hour } display.display(); delay(1000); // Delay untuk mengurangi beban CPU } void feedFish() { fishFeederServo.write(90); // Rotate to dispense food delay(5000); // Wait for 5 seconds fishFeederServo.write(0); // Rotate back to initial position Serial.println("Fish fed!"); // Tambahkan output untuk debug }
Editor is loading...
Leave a Comment