Arduino motion sensor switch

Hello, I am trying to design a ir motion sensor for which i am already following an ardiuno design build for. However don't know how to change the code appropriately.

current code link

this code seems to only activate the output when an object is in proximity however I want to change it so that the output will turn on when an object is detected and stay on until another object is detected. This will be for a LED lamp that I want to turn on with a wave of my hand and then turn it off with another wave of my hand. Any help is greatly appreciated. This is only my second arduino project and am definately a beginner. Thanks

Below is the current code for the existing project:

int ledPin = 13; //LED anode connected to digital pin 13
int inputPin = 2; //infrared proximity switch connected to digital pin 2
int val = 0; //this variable will read the value from the sensor


void setup() 
{
pinMode(ledPin, OUTPUT); //declare LED as output
pinMode(inputPin, INPUT); //declare infrared sensor as input
}

void loop(){

val = digitalRead(inputPin); // read input value

if (val == HIGH) { //check if the input is HIGH
digitalWrite(ledPin, LOW); //LED is off
} else {
digitalWrite(ledPin, HIGH); //LED is turned on
}
}

Please use code tags </> to present code.

For a toggle switch add two variables, for the on/off state and for the previous input value. When the value changes (previous_val != val), e.g. from low to high, toggle the state (state = !state), and output it to the LED.

The answer has already been supplied by DrDiettrich but I'd thought I'd point you (and any other noob reading this) at my series of YouTube Arduino articles aimed at enthusiastic newcomers just like you!

Although I haven't covered an IR detector (yet, it's been on the backlog for about 6 months) I do cover the ultrasonic detector which would basically do the same functionality - who knows, you may learn something along the way.

So take a look (the URL is in my signature below) and if you like what you see, subscribe so you get notification of new videos that I post regularly. All aimed at giving noobs a kick-start in their Arduino (and other) electronic projects :slight_smile:

Thanks. i will check out your videos and consider the ultra sonic detector for the project. Great community here that is ready to help. Thanks a ton.