Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DHT.h"
- #define DHTPIN1 12
- #define DHTPIN2 10
- #define DHTTYPE1 DHT22
- #define DHTTYPE2 DHT22
- DHT dht1(DHTPIN1, DHTTYPE1);
- DHT dht2(DHTPIN2, DHTTYPE2);
- float humidity1, temperature1, humidity2, temperature2, criticalTemperature;
- float A0_val, A1_val, moisture1, moisture2, moistureAverage, setMoisture;
- const int ventilPin = 5;
- bool zalijevano;
- int irrigationType;
- double setVolume;
- byte sensorInterrupt = 0; // 0 = digital pin 2
- int ventil;
- float vrijeme;
- float calibrationFactor = 7.5;
- volatile byte pulseCount;
- float flowRate;
- float flowLitres;
- double totalLitres;
- unsigned long startTime, endTime, timeDifference;
- int i = 0;
- void setup()
- {
- Serial.begin(9600);
- dht1.begin();
- dht2.begin();
- pulseCount = 0;
- flowRate = 0.0;
- flowLitres = 0.0;
- totalLitres = 0.0;
- pinMode(13, OUTPUT);
- pinMode(ventilPin, OUTPUT);
- // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
- // Configured to trigger on a FALLING state change (transition from HIGH
- // state to LOW state)
- //attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
- }
- void uzmiNoveVrijednosti(){
- //Temperatura i vlažnost zraka
- humidity1 = dht1.readHumidity();
- temperature1 = dht1.readTemperature();
- humidity2 = dht2.readHumidity();
- temperature2 = dht2.readTemperature();
- //Vlaznost zemlje
- A0_val = analogRead(0);
- A1_val = analogRead(1);
- moisture1 = (102400 / 939) - (100 * A0_val) / 939;
- moisture2 = (102400 / 939) - (100 * A1_val) / 939;
- if (moisture1 > 100.0) {
- moisture1 = 100.00;
- }
- if (moisture2 > 100.0) {
- moisture2 = 100.0;
- }
- moistureAverage = (moisture1 + moisture2) / 2;
- //Serial.println(temperature1);
- }
- void loop()
- {
- int n;
- char v[10], u[10], vzr[10], vze[10], l[10];
- // put your main code here, to run repeatedly:
- //Serial.flush(); //flush all previous received and transmitted data
- while(!Serial.available()) ; // hang program until a byte is received notice the ; after the while()
- /*continue program ...*/
- while (Serial.available() > 0){
- n = Serial.parseInt();
- if (Serial.read() == '\n') {
- //Serial.println(n);
- switch(n){
- case 0:
- //delay(1000);
- //v-vanjska temp, u-unutarnja temp., vzr - vlaga zraka, vze - vlaga zemlje, lit-litara
- uzmiNoveVrijednosti();
- //String v = String(temperature1);
- //String u = String(temperature2);
- //String vzr = String(humidity1);
- //String vze = String(moistureAverrage);
- //String l = String(0);
- dtostrf(temperature1, 4, 2, v);
- dtostrf(temperature2, 4, 2, u);
- dtostrf(humidity1, 4, 2, vzr);
- dtostrf(moistureAverage, 4, 2, vze);
- dtostrf(flowLitres, 4, 2, l);
- Serial.print("v="+String(v)+"u="+String(u)+"vzr="+String(vzr)+"vze="+String(vze)+"lit="+String(l)+"ventil="+String(ventil)+"\n");
- break;
- case 1:
- digitalWrite(ventilPin, HIGH);
- ventil = 1;
- break;
- case 2:
- digitalWrite(ventilPin, LOW);
- ventil = 0;
- break;
- }
- }
- }
- //Serial.println("Read from raspberry");
- delay(1000); // delay in between reads for stability
- if(n == 1 && i != 1){
- startTime = millis();
- attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
- i = 1;
- }
- else if(n == 2 && i == 1){
- detachInterrupt(sensorInterrupt);
- endTime = millis();
- Serial.println(pulseCount);
- timeDifference = endTime - startTime;
- timeDifference = timeDifference/(1000*60); // 1000*60 da dobijem minute
- //Serial.print("timeDiff: ");
- //Serial.println(timeDifference);
- flowRate = pulseCount / (timeDifference * calibrationFactor); // protok u L/min
- /*Serial.print("flowRate: ");
- Serial.println(flowRate);*/
- flowLitres = (flowRate * timeDifference);
- i = 0;
- //unsigned int frac;
- }
- }
- //Insterrupt Service Routine
- void pulseCounter()
- {
- pulseCount++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement