Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Event.h>
- #include <Timer.h>
- #include <dht11.h>
- #include <SoftwareServo.h>
- const int PIN_LIGHT = 6;
- const int PIN_COOLER = 2;
- const int PIN_DOORS = 10;
- const int PIN_ROOF = 8;
- const int PIN_WATER = 27;
- const int PIN_HEATER = 25;
- const int PIN_DHT11 = 52;
- const int PIN_ILLUMINATION = A15;
- const int PIN_GROUND_HUMIDITY = A14;
- dht11 DHT11;
- SoftwareServo servoDoors;
- SoftwareServo servoRoof;
- Timer timerHumidityIllumination;
- Timer timerTemperature;
- Timer timerAutoRegulation;
- int light = 0;
- int cooler = 0;
- int doors = 0;
- boolean water = false;
- int roof = 0;
- boolean heater = false;
- int temperature = 0;
- int air_humidity = 0;
- int illumination = 0;
- int ground_humidity = 0;
- boolean auto_regulation = false;
- int auto_temperature = 20;
- int auto_temperature_t = 2;
- int auto_ground_humidity = 80;
- int auto_ground_humidity_t = 5;
- int auto_air_humidity = 80;
- int auto_air_humidity_t = 5;
- int auto_illumination = 1200;
- int auto_illumination_t = 300;
- void setup()
- {
- pinMode(PIN_LIGHT, OUTPUT);
- pinMode(PIN_COOLER, OUTPUT);
- pinMode(PIN_WATER, OUTPUT);
- pinMode(PIN_HEATER, OUTPUT);
- pinMode(PIN_ILLUMINATION, INPUT);
- pinMode(PIN_GROUND_HUMIDITY, INPUT);
- servoDoors.attach(PIN_DOORS);
- servoDoors.setMaximumPulse(2200);
- servoRoof.attach(PIN_ROOF);
- servoRoof.setMaximumPulse(2200);
- Serial.begin(115200);
- Serial.setTimeout(3);
- timerHumidityIllumination.every(2013, measureHumidityIllumination);
- timerTemperature.every(3000, measureTemperature);
- timerAutoRegulation.every(100, checkAutoRegulation);
- }
- void loop()
- {
- timerTemperature.update();
- timerHumidityIllumination.update();
- timerAutoRegulation.update();
- SoftwareServo::refresh();
- if (Serial.available() > 0)
- {
- switch (Serial.read())
- {
- case 's':
- //svjetlo
- light = Serial.parseInt();
- analogWrite(PIN_LIGHT, light);
- break;
- case 'k':
- //kuler
- analogWrite(PIN_COOLER, Serial.parseInt());
- break;
- case 'v':
- //vrata
- servoDoors.write(Serial.parseInt());
- break;
- case 'r':
- //krov
- servoRoof.write(Serial.parseInt());
- break;
- case 'g':
- //grijac
- digitalWrite(PIN_HEATER, Serial.parseInt());
- break;
- case 'p':
- //pumpa
- digitalWrite(PIN_WATER, Serial.parseInt());
- break;
- //automatska regulacija
- case 'a':
- delay(4);
- switch (Serial.read())
- {
- case '1':
- auto_regulation = true;
- break;
- case '0':
- auto_regulation = false;
- break;
- case 's':
- //automatsko osvjetljenje
- auto_illumination = Serial.parseInt();
- break;
- }
- break;
- }
- }
- }
- int getAutoValue(int actuator_current, int sensor_current, int sensor_needed)
- {
- int astep = 10;
- if (sensor_needed > sensor_current)
- astep *= -1;
- actuator_current += astep;
- if (actuator_current > 255) actuator_current = 255;
- if (actuator_current < 0) actuator_current = 0;
- return actuator_current;
- }
- void checkAutoRegulation()
- {
- if (auto_regulation)
- {
- if (abs(auto_illumination - illumination) > 0)
- {
- light = getAutoValue(light, illumination, auto_illumination);
- analogWrite(PIN_LIGHT, light);
- }
- }
- }
- void measureTemperature()
- {
- DHT11.read(PIN_DHT11);
- //mjeri temperaturu
- Serial.print("t");
- Serial.println((int)DHT11.temperature);
- //mjeri vlaznost vazduha
- Serial.print("v");
- Serial.println((int)DHT11.humidity);
- }
- void measureHumidityIllumination()
- {
- //mjeri osvjetljenje
- illumination = map(analogRead(PIN_ILLUMINATION), 0, 1024, 0, 4000);
- Serial.print("s");
- Serial.println(illumination);
- //mjeri vlaznost zemlje
- Serial.print("z");
- Serial.println(map(analogRead(PIN_GROUND_HUMIDITY), 0, 1024, 0, 100));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement