Advertisement
tvrtko282

asdasd

Sep 2nd, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.98 KB | None | 0 0
  1. #include "DHT.h"
  2.  
  3. #define DHTPIN1 12
  4. #define DHTPIN2 10
  5. #define DHTTYPE1 DHT22
  6. #define DHTTYPE2 DHT22
  7. DHT dht1(DHTPIN1, DHTTYPE1);
  8. DHT dht2(DHTPIN2, DHTTYPE2);
  9. float humidity1, temperature1, humidity2, temperature2, criticalTemperature;
  10.  
  11. float A0_val, A1_val, moisture1, moisture2, moistureAverage, setMoisture;
  12.  
  13. const int ventilPin = 5;
  14. bool zalijevano;
  15.  
  16. int irrigationType;
  17. double setVolume;
  18.  
  19.  
  20. byte sensorInterrupt = 0;  // 0 = digital pin 2
  21. int ventil;
  22. float vrijeme;
  23. float calibrationFactor = 7.5;
  24. volatile byte pulseCount;
  25. float flowRate;
  26. float flowLitres;
  27. double totalLitres;
  28. unsigned long startTime, endTime, timeDifference;
  29.  
  30. int i = 0;
  31.  
  32. void setup()
  33. {
  34.   Serial.begin(9600);
  35.   dht1.begin();
  36.   dht2.begin();
  37.  
  38.   pulseCount = 0;
  39.   flowRate   = 0.0;
  40.   flowLitres = 0.0;
  41.   totalLitres = 0.0;
  42.  
  43.   pinMode(13, OUTPUT);
  44.   pinMode(ventilPin, OUTPUT);
  45.  
  46.   // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  47.   // Configured to trigger on a FALLING state change (transition from HIGH
  48.   // state to LOW state)
  49.  
  50.   //attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  51. }
  52.  
  53. void uzmiNoveVrijednosti(){
  54.  
  55.   //Temperatura i vlažnost zraka
  56.   humidity1 = dht1.readHumidity();
  57.   temperature1 = dht1.readTemperature();
  58.   humidity2 = dht2.readHumidity();
  59.   temperature2 = dht2.readTemperature();
  60.  
  61.   //Vlaznost zemlje
  62.   A0_val = analogRead(0);
  63.   A1_val = analogRead(1);
  64.   moisture1 = (102400 / 939) - (100 * A0_val) / 939;
  65.   moisture2 = (102400 / 939) - (100 * A1_val) / 939;
  66.   if (moisture1 > 100.0) {
  67.     moisture1 = 100.00;
  68.   }
  69.   if (moisture2 > 100.0) {
  70.     moisture2 = 100.0;
  71.   }
  72.   moistureAverage = (moisture1 + moisture2) / 2;
  73.  
  74.   //Serial.println(temperature1);
  75.  
  76. }
  77.  
  78. void loop()
  79. {
  80.  int n;
  81.  char v[10], u[10], vzr[10], vze[10], l[10];
  82.   // put your main code here, to run repeatedly:
  83.   //Serial.flush(); //flush all previous received and transmitted data
  84.   while(!Serial.available()) ; // hang program until a byte is received notice the ; after the while()
  85.   /*continue program ...*/
  86.  
  87.   while (Serial.available() > 0){
  88.   n = Serial.parseInt();
  89.  
  90.   if (Serial.read() == '\n') {
  91.     //Serial.println(n);
  92.     switch(n){
  93.       case 0:
  94.         //delay(1000);
  95.         //v-vanjska temp, u-unutarnja temp., vzr - vlaga zraka, vze - vlaga zemlje, lit-litara
  96.         uzmiNoveVrijednosti();
  97.         //String v = String(temperature1);
  98.         //String u = String(temperature2);
  99.         //String vzr = String(humidity1);
  100.         //String vze = String(moistureAverrage);
  101.         //String l = String(0);
  102.         dtostrf(temperature1, 4, 2, v);
  103.         dtostrf(temperature2, 4, 2, u);
  104.         dtostrf(humidity1, 4, 2, vzr);
  105.         dtostrf(moistureAverage, 4, 2, vze);
  106.         dtostrf(flowLitres, 4, 2, l);
  107.         Serial.print("v="+String(v)+"u="+String(u)+"vzr="+String(vzr)+"vze="+String(vze)+"lit="+String(l)+"ventil="+String(ventil)+"\n");
  108.         break;
  109.       case 1:
  110.         digitalWrite(ventilPin, HIGH);
  111.         ventil = 1;
  112.         break;
  113.       case 2:
  114.         digitalWrite(ventilPin, LOW);
  115.         ventil = 0;
  116.         break;  
  117.     }
  118.   }
  119.   }
  120.   //Serial.println("Read from raspberry");
  121.   delay(1000);        // delay in between reads for stability
  122.  
  123.  
  124.   if(n == 1 && i != 1){
  125.     startTime = millis();
  126.     attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  127.     i = 1;
  128.   }
  129.   else if(n == 2 && i == 1){
  130.     detachInterrupt(sensorInterrupt);
  131.     endTime = millis();
  132.     Serial.println(pulseCount);
  133.     timeDifference = endTime - startTime;
  134.     timeDifference = timeDifference/(1000*60); // 1000*60 da dobijem minute
  135.     //Serial.print("timeDiff: ");
  136.     //Serial.println(timeDifference);
  137.     flowRate = pulseCount / (timeDifference * calibrationFactor); // protok u L/min
  138.     /*Serial.print("flowRate: ");
  139.     Serial.println(flowRate);*/
  140.     flowLitres = (flowRate * timeDifference);
  141.     i = 0;
  142.     //unsigned int frac;
  143.   }
  144. }
  145.  
  146.  
  147.  
  148. //Insterrupt Service Routine
  149. void pulseCounter()
  150. {
  151.     pulseCount++;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement