Advertisement
lukicdarkoo

MK ATMEGA 2560, Demtra21, Pametni Plastenik

Sep 13th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.02 KB | None | 0 0
  1. #include <Event.h>
  2. #include <Timer.h>
  3. #include <dht11.h>
  4. #include <SoftwareServo.h>
  5.  
  6.  
  7. const int PIN_LIGHT = 6;
  8. const int PIN_COOLER = 2;
  9. const int PIN_DOORS = 10;
  10. const int PIN_ROOF = 8;
  11. const int PIN_WATER = 27;
  12. const int PIN_HEATER = 25;
  13. const int PIN_DHT11 = 52;
  14. const int PIN_ILLUMINATION = A15;
  15. const int PIN_GROUND_HUMIDITY = A14;
  16.  
  17. dht11 DHT11;
  18. SoftwareServo servoDoors;
  19. SoftwareServo servoRoof;
  20.  
  21. Timer timerHumidityIllumination;
  22. Timer timerTemperature;
  23. Timer timerAutoRegulation;
  24.  
  25. int light = 0;
  26. int cooler = 0;
  27. int doors = 0;
  28. boolean water = false;
  29. int roof = 0;
  30. boolean heater = false;
  31. int temperature = 0;
  32. int air_humidity = 0;
  33. int illumination = 0;
  34. int ground_humidity = 0;
  35.  
  36. boolean auto_regulation = false;
  37. int auto_temperature = 20;
  38. int auto_temperature_t = 2;
  39. int auto_ground_humidity = 80;
  40. int auto_ground_humidity_t = 5;
  41. int auto_air_humidity = 80;
  42. int auto_air_humidity_t = 5;
  43. int auto_illumination = 1200;
  44. int auto_illumination_t = 300;
  45.  
  46. void setup()
  47. {
  48.   pinMode(PIN_LIGHT, OUTPUT);
  49.   pinMode(PIN_COOLER, OUTPUT);
  50.   pinMode(PIN_WATER, OUTPUT);
  51.   pinMode(PIN_HEATER, OUTPUT);
  52.  
  53.   pinMode(PIN_ILLUMINATION, INPUT);
  54.   pinMode(PIN_GROUND_HUMIDITY, INPUT);
  55.  
  56.   servoDoors.attach(PIN_DOORS);
  57.   servoDoors.setMaximumPulse(2200);
  58.  
  59.   servoRoof.attach(PIN_ROOF);
  60.   servoRoof.setMaximumPulse(2200);
  61.  
  62.   Serial.begin(115200);
  63.   Serial.setTimeout(3);
  64.  
  65.   timerHumidityIllumination.every(2013, measureHumidityIllumination);
  66.   timerTemperature.every(3000, measureTemperature);
  67.   timerAutoRegulation.every(100, checkAutoRegulation);
  68. }
  69.  
  70. void loop()
  71. {
  72.   timerTemperature.update();
  73.   timerHumidityIllumination.update();
  74.   timerAutoRegulation.update();
  75.  
  76.   SoftwareServo::refresh();
  77.  
  78.   if (Serial.available() > 0)
  79.   {
  80.     switch (Serial.read())
  81.     {
  82.       case 's':
  83.         //svjetlo
  84.         light = Serial.parseInt();
  85.         analogWrite(PIN_LIGHT, light);
  86.       break;
  87.      
  88.       case 'k':
  89.          //kuler
  90.         analogWrite(PIN_COOLER, Serial.parseInt());
  91.       break;
  92.      
  93.       case 'v':
  94.          //vrata
  95.         servoDoors.write(Serial.parseInt());
  96.       break;
  97.      
  98.        case 'r':
  99.          //krov
  100.          servoRoof.write(Serial.parseInt());
  101.       break;
  102.      
  103.       case 'g':
  104.          //grijac
  105.         digitalWrite(PIN_HEATER, Serial.parseInt());
  106.       break;
  107.      
  108.        case 'p':
  109.          //pumpa
  110.         digitalWrite(PIN_WATER, Serial.parseInt());
  111.       break;
  112.      
  113.       //automatska regulacija
  114.       case 'a':
  115.         delay(4);
  116.         switch (Serial.read())
  117.         {
  118.           case '1':
  119.             auto_regulation = true;
  120.           break;
  121.          
  122.           case '0':
  123.             auto_regulation = false;
  124.           break;
  125.          
  126.           case 's':
  127.             //automatsko osvjetljenje
  128.             auto_illumination = Serial.parseInt();
  129.           break;
  130.         }
  131.       break;
  132.     }
  133.   }
  134. }
  135.  
  136. int getAutoValue(int actuator_current, int sensor_current, int sensor_needed)
  137. {
  138.   int astep = 10;
  139.  
  140.   if (sensor_needed > sensor_current)
  141.     astep *= -1;
  142.    
  143.   actuator_current += astep;
  144.  
  145.   if (actuator_current > 255) actuator_current = 255;
  146.   if (actuator_current < 0) actuator_current = 0;
  147.   return actuator_current;
  148. }
  149.  
  150. void checkAutoRegulation()
  151. {
  152.   if (auto_regulation)
  153.   {
  154.     if (abs(auto_illumination - illumination) > 0)
  155.     {
  156.       light = getAutoValue(light, illumination, auto_illumination);
  157.       analogWrite(PIN_LIGHT, light);
  158.     }
  159.   }
  160. }
  161.  
  162. void measureTemperature()
  163. {
  164.   DHT11.read(PIN_DHT11);
  165.  
  166.   //mjeri temperaturu
  167.   Serial.print("t");
  168.   Serial.println((int)DHT11.temperature);
  169.  
  170.   //mjeri vlaznost vazduha
  171.   Serial.print("v");
  172.   Serial.println((int)DHT11.humidity);
  173. }
  174.  
  175. void measureHumidityIllumination()
  176. {
  177.   //mjeri osvjetljenje
  178.   illumination = map(analogRead(PIN_ILLUMINATION), 0, 1024, 0, 4000);
  179.   Serial.print("s");
  180.   Serial.println(illumination);
  181.  
  182.   //mjeri vlaznost zemlje
  183.   Serial.print("z");
  184.   Serial.println(map(analogRead(PIN_GROUND_HUMIDITY), 0, 1024, 0, 100));
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement