int triggerPin = 5; //triggering on pin 5 int echoPin = 6; //echo on pin 6 int state = 0;//sensor state void setup() { //we will be combinig both setups from the codes Serial.begin(9600); //we'll start serial comunication, so we can see the distance on the serial monitor pinMode(triggerPin, OUTPUT); //defining pins pinMode(echoPin, INPUT); } void loop(){ //main code int duration, distance; //Adding duration and distance digitalWrite(triggerPin, HIGH); //triggering the wave(like blinking an LED) delay(10); digitalWrite(triggerPin, LOW); duration = pulseIn(echoPin, HIGH); //a special function for listening and waiting for the wave distance = (duration/2) / 29.1; //transforming the number to cm(if you want inches, you have to change the 29.1 with a suitable number //YOU CAN UNCOMMENT THIS IF YOU WANT TO USE BT TERMINAL TO SHOW YOU THE DISTANCE SO THE APP IS VERSATILE BUT YOU WILL NEED TO UNCOMMENT THE IF STATEMENTS FOR THE TRIPPING LENGHT BELOW // Serial.print(distance); //printing the numbers // Serial.print("cm"); //and the unit // Serial.println(" "); //just printing to a new line //UNCOMMENT TILL THE END IF YOU WANT TO USE THE CODE JUST TO DISPLAY TE LENGHT if(distance <= 20){ if(state == 0){ Serial.println("1");//when the hand is 20cm or less above the sensor, it will send "1" --> enabling the wifi state = 1; } delay(500); //the stopping time } else{ if(state == 1){ Serial.println("0");// send "0" if there is no obsicle in the 20cm range --> turning the wifi off state = 0; } } }