// MASTER #include #include long startMS; // This will hold the start time in milliseconds int windCounterStatus = LOW; int rainCounterStatus = HIGH; int resetStatus = LOW; int val = 0; int val1 = 0; int val2 = 0; int rats =0; // number of rats detected int mice =0; // number of mice detected unsigned long rain = 0; unsigned long previousRain =0; int windSpeedInputPin = 2; int rainInputPin= 3; int resetWatcherPin = 4; // Recieves a pulse from pin 5 on the slave. int powerToWindDirPin = 7; int sleepModePin = 8; int resetFonaPin = 9; const int ledPin = 13; unsigned long rainPulses= 0; unsigned long windPulseRate = 0; unsigned long maxWindPulseRate = 0; unsigned long avWindPulseRate = 0; unsigned long addingWindPulseRate = 0; int resetEverything = 0; int moisturePin = A1; int moisturePowerPin = 10; unsigned long moistureValue = 0; unsigned long futureRainDebounceValue =0; int n = 0; int r = 0; int i = 0; int ledState = LOW; int sleepMode = LOW; unsigned long previousLongMillis = 0; unsigned long previousShortMillis = 0; unsigned long previousMillis = 0; const long veryShortInterval = 50; const long shortInterval = 5000; const long longInterval = 600000; // 60000 = 1 minute const long rainDebounceValue = 10000; int windDirection = 0; int windDirection2 = 0; int windDirection3 = 0; int one =0; int two =0; int three =0; int four =0; int five =0; int six =0; int seven =0; int eight =0; int answer = 0; void setup() { Wire.begin(); pinMode(ledPin, OUTPUT); pinMode(sleepModePin, OUTPUT); pinMode(powerToWindDirPin, OUTPUT); pinMode(resetFonaPin, OUTPUT); pinMode(windSpeedInputPin, INPUT_PULLUP); pinMode(rainInputPin, INPUT_PULLUP); pinMode(resetWatcherPin, INPUT_PULLUP); pinMode(moisturePowerPin, OUTPUT); digitalWrite(sleepModePin,HIGH); digitalWrite(powerToWindDirPin,LOW); delay(2000); tone(6,500,1000); digitalWrite(resetFonaPin,HIGH); delay(100); digitalWrite(resetFonaPin,LOW); Serial.begin(115200); Serial.println("Starting wind and rain recording ......."); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void loop() { unsigned long currentMillis = millis(); // Millis goes back to zero after 50 days windCounter(); // Wind pulse counting function rainCounter(); resetCounter(); sleepModeFunction(); resetFonaFunction(); windDirectionCalc(); if (currentMillis - previousLongMillis <= longInterval) // Note that we never use the 'delay' function during this 'if' statement. { /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (currentMillis - previousShortMillis <= veryShortInterval) /// This sends a 5V pulse to wind direction sensor for a very short interval. { digitalWrite(powerToWindDirPin,HIGH); windDirection3 = analogRead(0); tone(6,(windDirection3+300)); } else { digitalWrite(powerToWindDirPin,LOW); noTone(6); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (currentMillis - previousShortMillis >= shortInterval) { /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // if ((n>10)&&(n<120)){tone(6,((answer*15)+400));} else {noTone(6);} // use for debugging wind direction sensor. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// windPulseRate = 100 * rats/(shortInterval/1000); addingWindPulseRate = addingWindPulseRate + windPulseRate; n = n+1; myMode(); // Calculates the mode wind direction. Serial.println(""); Serial.print("moisture value is: ");Serial.print(moistureValue);Serial.println(" %"); Serial.print("Millis = ");Serial.println(currentMillis); Serial.print("Wind pulse rate per micro second= ");Serial.println(windPulseRate); Serial.print("Average wind pulse rate per micro second= ");Serial.println(avWindPulseRate); Serial.print("n = ");Serial.println(n); Serial.print("r = ");Serial.println(r); Serial.print("Maximum wind pulse rate per micro second = ");Serial.println(maxWindPulseRate); Serial.print("Rain fall = ");Serial.println(rain); Serial.print("resetEverything = ");Serial.println(resetEverything); Serial.print("Wind direction = ");Serial.println(windDirection2); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// previousShortMillis = currentMillis; i = 0; rats = 0; } if (windPulseRate > maxWindPulseRate) { maxWindPulseRate = windPulseRate; Serial.print("Maximum wind pulse rate per micro second = ");Serial.println(maxWindPulseRate); } if (rain > previousRain) { previousRain = rain; } } else { avWindPulseRate = addingWindPulseRate/n; windDirection = answer; Serial.print("Median Wind direction for Wire = ");Serial.println(windDirection); Wire.beginTransmission(9); // transmit to device #9 // Make sure the order is the same in master and slave. digitalWrite(moisturePowerPin, HIGH); delay(10); Wire.write(maxWindPulseRate/10); // Values must not be greater than 255 (1 byte). delay(10); Wire.write(windDirection); // A median number between 1 and 8 inclusive. delay(10); Wire.write(rain); moistureValue = analogRead(moisturePin)*0.102; // The sensor is calibrated by multiplying by 0.102. delay(10); Wire.write(avWindPulseRate/10); delay(10); Wire.write(moistureValue); delay(10); digitalWrite(moisturePowerPin, LOW); Wire.endTransmission(); // stop transmitting resetMyMode(); // For debugging this function should not be in resetEverything if statement. previousLongMillis = currentMillis; addingWindPulseRate = 0; avWindPulseRate = 0; n=0; r=r+1; // counts up to a time period at which fona is reset in multiples of whatever long interval is set to. } if(resetEverything > 0) // This is the call back to say successful data transmission to HTTP. { tone(6,((answer*15)+400),1000); maxWindPulseRate = 0; rain = 0; previousRain = 0; resetEverything = 0; } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void windCounter() { val = digitalRead(windSpeedInputPin); // read input value if (val == HIGH) { // checking if the input is HIGH if (windCounterStatus == LOW) { // we have just turned on windCounterStatus = HIGH; digitalWrite(ledPin,HIGH); // Serial.print("Pulses = ");Serial.println(rats); rats = rats +1; } } else { if (windCounterStatus == HIGH) { windCounterStatus = LOW; digitalWrite(ledPin,LOW); } } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void resetCounter() { val1 = digitalRead(resetWatcherPin); // read input value if (val1 == HIGH) { // checking if the input is HIGH if (resetStatus == LOW) { // we have just turned on resetStatus = HIGH; // digitalWrite(ledPin,HIGH); // Serial.print("resetEverything = ");Serial.println(resetEverything); resetEverything = 1; } } else { if (resetStatus == HIGH) { resetStatus = LOW; // digitalWrite(ledPin,LOW); } } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void rainCounter (){ val2 = digitalRead(rainInputPin); // read input value if (val2 == HIGH) { // checking if the input is HIGH if (rainCounterStatus == LOW) { // we have just turned on rainCounterStatus = HIGH; digitalWrite(ledPin,HIGH); mice = mice +1; } } else { if (rainCounterStatus == HIGH) { rainCounterStatus = LOW; digitalWrite(ledPin,LOW); } } // Debounce (we suspect that mice is too high) : unsigned long currentMillis = millis(); if ((mice > 0)&&( (currentMillis - previousShortMillis) > (shortInterval - 10) )) { rain = rain + 1; mice = 0; } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void windDirectionCalc () { if ( (windDirection3 > -1) && (windDirection3 < 20) ) { windDirection2 = 1; } if ( (windDirection3 > 20) && (windDirection3 < 60) ) { windDirection2 = 2; } if ( (windDirection3 > 60) && (windDirection3 < 120) ) { windDirection2 = 3; } if ( (windDirection3 > 200) && (windDirection3 < 250) ) { windDirection2 = 4; } if ( (windDirection3 > 345) && (windDirection3 < 400) ) { windDirection2 = 5; } if ( (windDirection3 > 310) && (windDirection3 < 345) ) { windDirection2 = 6; } if ( (windDirection3 > 250) && (windDirection3 < 310) ) { windDirection2 = 7; } if ( (windDirection3 > 120) && (windDirection3 < 200) ) { windDirection2 = 8; } } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void sleepModeFunction () { if ( (n > 110)||(n<10) ) { digitalWrite(sleepModePin,HIGH); } else { digitalWrite(sleepModePin,LOW); } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void resetFonaFunction () { if (r>143) // resets fona once every 24 hours. This needs to be changed back to r>143. { digitalWrite(resetFonaPin,HIGH); delay(100); digitalWrite(resetFonaPin,LOW); r=0; } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void myMode() // Calculates the mode wind direction (The value that occurrs most often). { if (windDirection2 == 1){one = one + 1;} if (windDirection2 == 2){two = two + 1;} if (windDirection2 == 3){three = three + 1;} if (windDirection2 == 4){four = four + 1;} if (windDirection2 == 5){five = five + 1;} if (windDirection2 == 6){six = six + 1;} if (windDirection2 == 7){seven = seven + 1;} if (windDirection2 == 8){eight = eight + 1;} if ( (one>two)&&(one>three)&&(one>four)&&(one>five)&&(one>six)&&(one>seven)&&(one>eight) ){answer = 1;} if ( (two>one)&&(two>three)&&(two>four)&&(two>five)&&(two>six)&&(two>seven)&&(two>eight) ){answer = 2;} if ( (three>one)&&(three>two)&&(three>four)&&(three>five)&&(three>six)&&(three>seven)&&(three>eight) ){answer = 3;} if ( (four>one)&&(four>two)&&(four>three)&&(four>five)&&(four>six)&&(four>seven)&&(four>eight) ){answer = 4;} if ( (five>one)&&(five>two)&&(five>three)&&(five>four)&&(five>six)&&(five>seven)&&(five>eight) ){answer = 5;} if ( (six>one)&&(six>two)&&(six>three)&&(six>four)&&(six>five)&&(six>seven)&&(six>eight) ){answer = 6;} if ( (seven>one)&&(seven>two)&&(seven>three)&&(seven>four)&&(seven>five)&&(seven>six)&&(seven>eight) ){answer = 7;} if ( (eight>one)&&(eight>two)&&(eight>three)&&(eight>four)&&(eight>five)&&(eight>six)&&(eight>seven) ){answer = 8;} Serial.print("My Mode value is: ");Serial.println(answer); } void resetMyMode() { one =0; two =0; three =0; four =0; five =0; six =0; seven =0; eight =0; answer = 0; }