/*
   PIR sensor tester
*/

int ledPin1 = 12;
int ledPin2 = 13;               // choose the pin for the LED
int inputPin1 = 14;               // choose the input pin (for PIR sensor)
int inputPin2 = 27;
int pirState1 = LOW;             // we start, assuming no motion detected
int pirState2 = LOW;
int val1 = 0;                    // variable for reading the pin status
int val2 = 0;
int MANUSIA = 0;
int kapasitas;
#define BLYNK_TEMPLATE_ID "TMPLP8Ltcucw"
#define BLYNK_DEVICE_NAME "SENSOR PIR 2"
#define BLYNK_AUTH_TOKEN "cPWUvEENWx5y5o1lFnJJe3RmaSYIbMy8"

// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";

BlynkTimer timer;
void myTimerEvent()
{
  Blynk.virtualWrite(V1, MANUSIA);
   {val1 = digitalRead(inputPin1);  // read input value
  if (val1 == HIGH) {            // check if the input is HIGH
    if (pirState1 == LOW) {
      // we have just turned on
      Serial.println( MANUSIA+=1);
      // We only want to print on the output change, not state
      pirState1 = HIGH;
    }
  } else {
    if (pirState1 == HIGH) {
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState1 = LOW;
    }
  }
  }

  {val2 = digitalRead(inputPin2);  // read input value
  if (val2 == HIGH) {            // check if the input is HIGH
    if (pirState2 == LOW) {
      // we have just turned on
      Serial.println(MANUSIA-=1);
      // We only want to print on the output change, not state
      pirState2 = HIGH;
    }
  } else {
    if (pirState2 == HIGH) {
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState2 = LOW;
    }
  }
  }
  if(MANUSIA<=kapasitas-1){
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin1, LOW);
  }
  if(MANUSIA>=kapasitas){
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin1, HIGH);
  } 
}

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt();
  kapasitas=pinValue;
}
void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, myTimerEvent);
  pinMode(ledPin1, OUTPUT);      // declare LED as output
  pinMode(ledPin2, OUTPUT);
  pinMode(inputPin1, INPUT);     // declare sensor as input
  pinMode(inputPin2, INPUT);

  Serial.begin(9600);
}

void loop() {
Blynk.run();
timer.run();
  
}