// 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();
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
servo1:GND
servo1:V+
servo1:PWM
pot1:VCC
pot1:SIG
pot1:GND
led1:A
led1:C
r1:1
r1:2
water
atm
relay
atm analog
servo
Unused
sw1:1
sw1:2
sw1:3
sw2:1
sw2:2
sw2:3