Untitled
unknown
plain_text
9 months ago
4.8 kB
1
Indexable
#include <OneWire.h> #include <DHT.h> #define SENSOR_OUT_PIN A0 // Pin connected to the analog output of the soil moisture sensor #define SENSOR_DIS_PIN 12 // Pin connected to the DIS (disable) pin of the soil moisture sensor #define SENSOR_VCC_PIN 13 // Pin connected to the VCC (supply voltage) pin of the soil moisture sensor #define DHT_PIN 4 // Pin podłączony do czujnika DHT11 #define PHOTO_RESISTOR_PIN A1 // Pin connected to the photoresistor #define SERVO_PIN 18 // Pin connected to the servo motor DHT dht(DHT_PIN, DHT11); bool sensorPowerOn = false; // Variable to track soil moisture sensor power status // Function to initialize the servo motor void initServo() { ledc_timer_config_t timer_conf = { .duty_resolution = LEDC_TIMER_13_BIT, // 13-bit duty resolution .freq_hz = 50, // PWM frequency 50Hz (standard for servo) .speed_mode = LEDC_HIGH_SPEED_MODE, // High-speed mode .timer_num = LEDC_TIMER_0 // Timer 0 }; ledc_timer_config(&timer_conf); ledc_channel_config_t ledc_conf = { .gpio_num = SERVO_PIN, .speed_mode = LEDC_HIGH_SPEED_MODE, .channel = LEDC_CHANNEL_0, .intr_type = LEDC_INTR_DISABLE, .timer_sel = LEDC_TIMER_0, .duty = 0 }; ledc_channel_config(&ledc_conf); } // Function to control the servo motor void controlServo(int speed, float rotation_time) { int angle = (speed * 180) / 100; // Convert speed to angle float duty_cycle = (angle / 18.0) + 2.5; // Convert angle to duty cycle ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, duty_cycle * (1 << 13)); ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0); vTaskDelay(pdMS_TO_TICKS((rotation_time / 2) * 1000)); // Wait for half of the rotation time ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0); // Stop the servo ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0); } // Function to read the moisture voltage float readMoistureVoltage() { // Turn on the soil moisture sensor power if it's off if (!sensorPowerOn) { digitalWrite(SENSOR_VCC_PIN, HIGH); delay(10); // Delay for stability sensorPowerOn = true; } // Read analog output from the soil moisture sensor int sensorValue = analogRead(SENSOR_OUT_PIN); // Convert analog value to voltage (assuming 10-bit ADC) float voltage = sensorValue * (5.0 / 1023.0); // Turn off the soil moisture sensor power after reading digitalWrite(SENSOR_VCC_PIN, LOW); sensorPowerOn = false; return voltage; } // Function to determine moisture level String determineMoistureLevel(float voltage) { if (voltage >= 1.4 && voltage <= 1.8) { return "WET"; } else if (voltage > 1.8 && voltage <= 2.1) { return "MOIST"; } else if (voltage > 2.1 && voltage <= 2.3) { return "DRY"; } else { return "UNKNOWN"; } } // Funkcja do odczytu temperatury z czujnika DHT11 float readTemperature() { return dht.readTemperature(); // Odczyt temperatury w stopniach Celsjusza } // Funkcja do odczytu wilgotności powietrza z czujnika DHT11 float readHumidity() { return dht.readHumidity(); // Odczyt wilgotności powietrza w procentach } // Function to read the light level String readLightLevel() { // Read analog voltage from the photoresistor int lightSensorValue = analogRead(PHOTO_RESISTOR_PIN); // Convert analog value to voltage (assuming 10-bit ADC) float lightVoltage = lightSensorValue * (5.0 / 1023.0); // Determine light level based on voltage if (lightVoltage < 0.5) { return "Dark"; } else if (lightVoltage >= 0.5 && lightVoltage <= 2.5) { return "Medium"; } else { return "Bright"; } } void setup() { Serial.begin(9600); pinMode(SENSOR_DIS_PIN, OUTPUT); pinMode(SENSOR_VCC_PIN, OUTPUT); // Initialize DS18B20+ temperature sensor sensors.begin(); // Initialize servo initServo(); } void loop() { // Read and print the readings float moistureVoltage = readMoistureVoltage(); String moistureLevel = determineMoistureLevel(moistureVoltage); Serial.println("Moisture Reading:"); Serial.println("Voltage: " + String(moistureVoltage) + " V"); Serial.println("Moisture Level: " + moistureLevel); float temperatureC = readTemperature(); Serial.println("Temperatura: " + String(temperatureC) + " °C"); float humidity = readHumidity(); Serial.println("Wilgotność powietrza: " + String(humidity) + " %"); String lightLevel = readLightLevel(); Serial.println("Light Level: " + lightLevel); // Control the servo motor controlServo(50, 2.0); // Example: Rotate at 50% speed for 2 seconds delay(5000); // Delay for 5 seconds before next reading }
Editor is loading...
Leave a Comment