#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <uri/UriBraces.h>

#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6
//checks to see if the mode changed
int currentMode=0; //keeps track of which sets of lights are on
int nextMode=0; //signals the need to update lights
int currentTouch=LOW; //currentState of the touch panel
int lastTouch=LOW; //previous state of the touch panel
int motionState=LOW; //the state of the motion sensor

//pins
const int whitePin=9; //pin 9,10 are PWM to eventually allow fading--white LEDs
const int blackPin=10; //blacklight LEDs
const int touchPin=32;  //capacitive touch sensor
const int motionPin=8;  //pir motion sensor


int touchHold=1000; //hold for this long to turn off
unsigned long touchPressed=0; //stores the time the touch sensor was touched

void checkMode() { 
  if (nextMode!=currentMode) {//checks to see if the mode changed
    if (nextMode!=0) {// there are no lights for mode=0
      digitalWrite(nextMode,HIGH); //sets the pin 
    }
    if (currentMode!=0) { //there are no lights for mode=0
      digitalWrite(currentMode, LOW);
    }
  currentMode=nextMode;
  }
}
void checkTouch() {
  currentTouch=analogRead(touchPin);
  if (currentTouch!=lastTouch) { // change in touch state
    if (currentTouch==HIGH) {//button up must mean clicked
      if (touchPressed!=0) { //0 means hold routine reset it to 0, so must be single click
        nextMode=currentMode==0?1:currentMode==1?2:1;
        lastTouch=currentTouch;
      }
    } else { //currentTouch!=lastTouch && currentTouch==LOW button down
      touchPressed=millis(); //record the time
      lastTouch=currentTouch; 
    }
  } else { //same state of touch
    if (currentTouch==LOW) { //touch pressed so still touching
      unsigned long touchTime=millis()-touchPressed;
      if (touchTime>touchHold) {
        nextMode=0; //shut off
        touchPressed=0; //so the click handler knows not to do anything
      }
    }
  }
}  //end checkTouch
void checkMotion() {
  if (currentMode!=0) { // no need to check motion when off
    motionState=analogRead(motionPin);
    if (motionState==LOW) {//no motion detected for 120s
      nextMode=0; //shut off
      touchPressed=0;
    }
  }
}
WebServer server(80);
int trig =25,echo=33;
long zaman;
long faseleh;
const int LED1 = 26;
const int LED2 = 27;

bool led1State = false;
bool led2State = false;

void sendHtml() {
  server.send(200, "text/html", "alireza mosavi");
}

void setup(void) {
  Serial.begin(115200);
  ////////////////////////
  pinMode(whitePin, OUTPUT);
  pinMode(blackPin, OUTPUT);
  pinMode(touchPin, INPUT);
  pinMode(motionPin, INPUT);
  /////////////////////////
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  //////////////////////
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  //////////////////////
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
  Serial.print("Connecting to WiFi ");
  Serial.print(WIFI_SSID);
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
  Serial.println(" Connected!");

  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  server.on("/", sendHtml);

  server.on(UriBraces("/toggle/{}"), []() {
    String led = server.pathArg(0);
    Serial.print("Toggle LED #");
    Serial.println(led);

    switch (led.toInt()) {
      case 1:
        led1State = !led1State;
        digitalWrite(LED1, led1State);
        break;
      case 2:
        led2State = !led2State;
        digitalWrite(LED2, led2State);
        break;
    }

    sendHtml();
  });

  server.begin();
  Serial.println("HTTP server started");
}

void loop(void) {
  server.handleClient();
  delay(2);
      digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  delayMicroseconds(2);

  zaman=pulseIn(echo,HIGH);
  faseleh=(zaman*0.034)/2;


    checkMode();
checkTouch();
checkMotion();
Serial.println(currentTouch);
  if(currentTouch=4095)
  {
    if (faseleh>=100){
    Serial.println("danger!!!!");
    }
  }
  
currentTouch=0;
delay(100);
}