LED turned on by IR switch sensor; turned off by pushbutton

Hello Arduino forum.
I'm new to Arduino so please bear with me if I miss out on something.

I'm trying to write some code that will use an E18-D80NK IR switch sensor to detect when my door opens, turns on an LED which then can only be turned off by a pushbutton when I leave.

I have build my code up from a pushbutton example which can remember the state of the pushbutton, but I have a problem with "disabling" the IR sensor so the code only responds to the pushbutton. I don't know if it is even possible to do so. The IR sensor should be made active again when the pushbutton is off.

I'm using the very basics of programming but wanted to play around with a new kind of input and the IR was available to me. I can't seem to figure out how to remember two input states and shift between which one is prioritised. I have thought about maybe using "switch" or "while" statements but I haven't used them before.

The code as it is now will also blink three times before remembering the state which I think is because of the three "if" statements. Please correct me if I'm wrong, I'm here to learn :slight_smile:

This is my code:

const int LED = 13;   // LED anode connected to digital pin 13
const int SENSOR = 2; // Infrared proximity switch connected to digital pin 2
const int BUTTON = 7; // Pushbutton is connected to pin 7

int sen_val = 0;      // This variable will read the value from the IR sensor
int but_val = 0;      // This variable will read the value from the pushbutton
int old_sen_val = 0;  // This variable stores the previous value of "sen_val" 
int old_but_val = 0;  // This variable stores the previous value of "but_val" 
int state = 0;        // This variable changes if LED is on (=1) or off (=0)

void setup() {
  pinMode(LED, OUTPUT);   // Defining the LED as an output
  pinMode(SENSOR, INPUT); // Defining the infrared sensor as an input
  pinMode(BUTTON, INPUT); // Defining the pushbutton as an input
}

void loop() {
 sen_val = digitalRead(SENSOR); // Read input value from IR sensor
 but_val = digitalRead(BUTTON); // Read input value from pushbutton

 // check if there was a transition
 if ((but_val == HIGH) && (old_but_val == LOW)) {
  state = 1 - state;
  delay(100);
 }

old_but_val = but_val;  // but_val is now old, let's store it

 // check if IR sensor detected any movement
 if ((sen_val == LOW) && (old_but_val == LOW)) {
  state = 1 - state;
 }

 if (state == 1) {
  digitalWrite(LED, HIGH); // turn LED on
 } else {
  digitalWrite(LED, LOW); // turn LED off
 }
}

I have a problem with "disabling" the IR sensor so the code only responds to the pushbutton.

It sounds like you need a flag variable to control whether the IR sensor is active or not.

Once the LED is on, set the flag variable to false and only respond to the IR input if the flag state is true. When the button is pressed to turn the LED off set the flag to true to make the IR active again.

When you say flag variable, do you mean a variable containing a boolean value (true/false)? Again I'm new to this.

Schmonkey:
When you say flag variable, do you mean a variable containing a boolean value (true/false)?

Yes.

When you say flag variable, do you mean a variable containing a boolean value (true/false)?

Exactly that. Being it a boolean it is easy to test its value and if you give it a meaningful name makes the code easier to read, such as

if (IRActive)
{
  //read the IR and respond
}

I can't seem to grasp the order of the commands in the loop. Everytime I think I got it right either the button or the sensor does the opposite.

I think I have to define...

boolean IR_sensor;

...in the beginning, but I can't figure out when to set it "false" and "true". It's driving me crazy.

Thanks for the help, though.

When you define the flag give it a value, probably true, so that the IR is active.

For more help please post your whole program as it is now