/*-----------------------------------------------------------------------------------------
 *  This software is in the public domain, furnished "as is", without technical
 *  support, and with no warranty, express or implied, as to its usefulness for
 *  any purpose.
 *
 *  Watersensor.ino
 *
 *  Funktion to read digital humidity sensor as water arlert for vehicle
 *
 *  Author: Volker Frauenstein
 *  Date: 03/03/2021  Version: 1.1 threshold for hysteresis as const added
 *
 *  Date: 05/01/2021  Version: 1.0 implementation for digital interface of sensor
 *
 */

// pinout definition for the watersensor
const int Pin_w = 23;

// generig definitions
int MS_hyst = 0;                          // counter for MS_hyst
const int Wthold = 10;                    // threshold for hysteresis
// #define DEBUG_W

void setup_watersensor() {
 pinMode(Pin_w, INPUT);
}

bool checkwater() {                       // function to check if water is dedected in vehicle

   int check = digitalRead(Pin_w);

   if (check > 0) {                       // PIN HIGH -> no alert
    #ifdef DEBUG_W
       Serial.print("wateralarm: off - ");Serial.println(check);
    #endif
    MS_hyst=0;
    AlarmLED(false);                      // LED should keep state color
    return LOW;
   }
   if (check == 0 && MS_hyst < Wthold) {  // we have water alert, but wait to be sure
    MS_hyst++;
    #ifdef DEBUG_W
       Serial.print("initiate alarm - counter: ");Serial.println(MS_hyst);
    #endif
    return LOW;
   }
   if (check == 0 && MS_hyst == Wthold) { // now we have water alert return HIGH
    #ifdef DEBUG_W
       Serial.print("wateralarm on:  - ");Serial.println(check);
    #endif
    AlarmLED(true);                       // LED set to white-blinking
    return HIGH;
  }
}                                         // function closed