// https://forum.arduino.cc/t/pls-help-unable-to-club-two-different-codes-in-one/1322814
// https://wokwi.com/projects/414817462925105153

// hallowed31 

#include <Servo.h>

Servo tapServo;

const int atmRelayPin = 12;
const int atmSensorPin = 3;   // what is this sensor for? What model?
const int atmAnalogPin = A0;  // what is this sensor for? What model?
int atmDigitalState = 1;
int lastAtmDigitalState = 1;
int atmAnalogVal = 0;

const int waterSensorPin = 4;  // what model sensor is this?
const int tapServoPin = 5;
int waterSensorState = 1;
int lastWaterSensorState = 1;

void setup() {
  Serial.begin(9600);  // open Serial port at 9600 baud
  Serial.println("water harvesting project breadboard test copy");
  delay(1000);  // give a second to read what software is installed on the Arduino

  pinMode(waterSensorPin, INPUT_PULLUP);
  pinMode(atmSensorPin, INPUT_PULLUP);
  pinMode(atmAnalogPin, INPUT);
  pinMode(atmRelayPin, OUTPUT);

  // only do this right before you move it, save wearing out your servo
  tapServo.attach(tapServoPin);
  tapServo.write(0);
  // detach if you don't need servo holding power
  tapServo.detach();
}

void loop() {
  checkForRain();
  checkWaterATM();
}

void checkForRain() {
  // always check for rain
  waterSensorState = digitalRead(waterSensorPin);
  // did something change from last loop through, last time we checked?
  if (waterSensorState != lastWaterSensorState) {
    switch (waterSensorState) {
      // logic reversed due to internal pullups used on pushbuttons
      case 0:
        servoWetAction();
        break;
      case 1:
        servoDryAction();
        break;
      default:
        servoWetAction();
        break;
    }
  }
  // save the current sensor state into the last state variable
  lastWaterSensorState = waterSensorState;
}

void checkWaterATM() {
  // always read the state of the water ATM sensor
  atmDigitalState = digitalRead(atmSensorPin);
  // and always check if something changed on the water ATM sensor
  if (atmDigitalState != lastAtmDigitalState) {
    // save the current water ATM sensor state into the last state variable
    lastAtmDigitalState = atmDigitalState;
    switch (atmDigitalState) {
      case 0:
        digitalWrite(atmRelayPin, HIGH);
        break;
      case 1:
        digitalWrite(atmRelayPin, LOW);
        break;
      default:
        digitalWrite(atmRelayPin, HIGH);
        break;
    }
    Serial.print("Digital Output: ");
    Serial.print(atmDigitalState);
    // read this in the event that something changed on the digital side
    atmAnalogVal = analogRead(atmAnalogPin);
    Serial.print("\tAnalog Output: ");  // \t is to tab over
    Serial.println(atmAnalogVal);       // to end the line
    Serial.println();                   // extra blank line for readability
  }
}

void servoDryAction() {
  tapServo.attach(tapServoPin);
  Serial.println("\ndry");  // \n is for a leading blank line
  Serial.println();
  tapServo.write(0);
  tapServo.detach();
}

void servoWetAction() {
  tapServo.attach(tapServoPin);
  Serial.println("\nrain");
  Serial.println();
  tapServo.write(90);
  tapServo.detach();
}
water
atm
relay
atm analog
servo
Unused