View more (ดูแผนผังวงจร และกดคัดลอกโค้ด / Copy Code)
แผนผังการต่อวงจร (Wire Diagram):
Source Code สำหรับ Arduino:
// Mosquito Trap Automation by Make-It // This code uses a DS3231 Real-Time Clock (RTC) to trigger a water change every 4 days. #include <Wire.h> #include "RTClib.h" // Create an RTC object RTC_DS3231 rtc; // --- Pin Definitions --- #define EMPTY_PUMP_PIN 7 // Digital pin connected to IN1 on the relay module #define REFILL_PUMP_PIN 8 // Digital pin connected to IN2 on the relay module // --- Configuration --- const long CYCLE_INTERVAL_SECONDS = 345600; // 4 days (4 * 24 * 60 * 60 seconds) const int EMPTY_PUMP_DURATION_MS = 60000; // Run empty pump for 60 seconds const int REFILL_PUMP_DURATION_MS = 60000; // Run refill pump for 60 seconds // We need to store the time of the last cycle. We'll use a variable for this. // For a real-world device, this should be stored in EEPROM to survive power loss. long lastCycleTime = 0; void setup() { Serial.begin(9600); Wire.begin(); pinMode(EMPTY_PUMP_PIN, OUTPUT); pinMode(REFILL_PUMP_PIN, OUTPUT); // Make sure pumps are off at startup digitalWrite(EMPTY_PUMP_PIN, HIGH); // Relays are often LOW-triggered, so HIGH is OFF digitalWrite(REFILL_PUMP_PIN, HIGH); if (!rtc.begin()) { Serial.println("Couldn't find RTC"); Serial.flush(); abort(); } // UNCOMMENT THE FOLLOWING LINE THE *FIRST* TIME YOU UPLOAD // This sets the RTC to the date and time this sketch was compiled. // After setting the time, comment it out and re-upload to prevent resetting the time on every reboot. // rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); Serial.println("Mosquito Trap Initialized."); // Get the current time to start our cycle timer DateTime now = rtc.now(); lastCycleTime = now.unixtime(); Serial.print("Current Unix time: "); Serial.println(lastCycleTime); } void loop() { // Get the current time from the RTC DateTime now = rtc.now(); long currentTime = now.unixtime(); Serial.print("Current Time: "); Serial.print(now.timestamp(DateTime::TIMESTAMP_FULL)); Serial.print(", Seconds since last cycle: "); Serial.println(currentTime - lastCycleTime); // Check if 4 days (CYCLE_INTERVAL_SECONDS) have passed since the last cycle if (currentTime - lastCycleTime >= CYCLE_INTERVAL_SECONDS) { Serial.println("--- Cycle time reached! Starting water change. ---"); runWaterChangeCycle(); // Update the last cycle time to the current time lastCycleTime = currentTime; Serial.println("--- Water change complete. Timer reset. ---"); } else { Serial.println("Not time yet. Waiting..."); } // Wait for a while before checking again to conserve power. // For ultra-low power, you would use deep sleep modes here. delay(3600000); // Check once per hour (3600 * 1000 ms) } void runWaterChangeCycle() { // Step 1: Turn on the emptying pump Serial.println("Starting empty pump..."); digitalWrite(EMPTY_PUMP_PIN, LOW); // LOW turns the relay ON delay(EMPTY_PUMP_DURATION_MS); // Step 2: Turn off the emptying pump digitalWrite(EMPTY_PUMP_PIN, HIGH); // HIGH turns the relay OFF Serial.println("Empty pump finished."); delay(5000); // Wait 5 seconds before refilling // Step 3: Turn on the refilling pump Serial.println("Starting refill pump..."); digitalWrite(REFILL_PUMP_PIN, LOW); // LOW turns the relay ON delay(REFILL_PUMP_DURATION_MS); // Step 4: Turn off the refilling pump digitalWrite(REFILL_PUMP_PIN, HIGH); // HIGH turns the relay OFF Serial.println("Refill pump finished."); }