Improving the PIR sensor code

Good afternoon! I'm currently running an automated lighting system using a relay and PIR sensor. I posted concerns about my project here in the forums before and this time, I need the help again. The automated system we've made, controlled via the PIR and relay, are working well with common PIR sensor code, like the one I used:

const int ledPin_one= 13;
const int inputPin= 2;


void setup(){
  Serial.begin(9600);
  pinMode(ledPin_one, OUTPUT);
  pinMode(inputPin, INPUT);
  
}

void loop(){
  int value= digitalRead(inputPin);

  if (value == HIGH)
  {
     RelayOn();
     delay(1800000L);
    
  }

  if (value == LOW)
  {
    RelayOff();
  }
}
  
void RelayOn()
{
  Serial.println("HIGH");
  digitalWrite(ledPin_one, HIGH);
}

void RelayOff()
{
  Serial.println("LOW");
  digitalWrite(ledPin_one, LOW);
}

In this case, the light turns on for 30 minutes as a person passes through it and turns off after the said time range. But we were suggested by our instructor that the delay should be excluded and it will dynamically turns off by itself when a person passes it again, that the light is will not just be on for 30 minutes but depending on how long before the person will pass it again. I think it is efficient in a way as to not to waste electricity if a person is only using it for 10 minutes or so. Suggestions are highly appreciated. Thanks in advance.

EDIT: Switched the "LOW" and "HIGH" with the other.

You just need the code to "remember" what state the light is in and have it switch state every time the sensor is activated.

dynamically turns off by itself when a person passes it again, that the light is will not just be on for 30 minutes but depending on how long before the person will pass it again. I think it is efficient

My suggestion is that this is a daft idea. You have a room with a light in which turns off when some one walks in the room. What sort of a sick joke is that?

Your code is very simple minded to the point of stupidity at the moment, you want to turn off the light when no activity is detected for a certain time say five minutes. To do this you capture the time from the millis() timer every time the sensor triggers on. Then when this millis() timer shows that the value in the capture variable exceeds the required time the light is turned off.

I think they mean that it turns on when a person enters and off when they leave. It will work if the sensor is in an entrance AND only one person is in the room at a time.

But I think it works mostly as a learning exercise!

Hutkikz:
I think they mean that it turns on when a person enters and off when they leave. It will work if the sensor is in an entrance AND only one person is in the room at a time.

But I think it works mostly as a learning exercise!

Precisely what I wanted to say sir. We asked our instructor about that if another person was entering and turns off the light but said it was okay so our top priority for the project is the automation. On your first post, you mean I shouldn't read only what states are the PIR and relay in but the light itself?

On your first post, you mean I shouldn't read only what states are the PIR and relay in but the light itself?

No.
There is no need to read what state the light is in. You know this because your program set the state of the light in the first place.

Hi,
I think that your instructor is doing this as a learning exercise, detecting input changes, not conditions (HIGH or LOW) and not using blocking delay code.

Check out the IDE example called, "Blink Without Display"

Tom... :slight_smile:

TomGeorge:
Hi,
I think that your instructor is doing this as a learning exercise, detecting input changes, not conditions (HIGH or LOW) and not using blocking delay code.

Check out the IDE example called, "Blink Without Display"

Tom... :slight_smile:

Sir, it is a project for our finals, not a activity of any sorts.

Sir, it is a project for our finals, not a activity of any sorts.

Your English is lacking. An exercise is exactly what it it. You are doing it not to have a working product, which is good because it is a crap specification. You are doing it to learn how to do things. That is called a learning exercise.

Anyway what level of final has that sort of project?

On your first post, you mean I shouldn't read only what states are the PIR and relay in but the light itself?

Like Mike says you set the light(relay ) on or off in the code so you just need it to remember what it is set to. you do that by using a variable.

In the Arduino IDE look under Examples > 02.Digital > StateChangeDetection

if you have a room at the end of a hall.
you want down the hall, the lights come on.
you wash you hands and walk back down the hall.

how can you tell it was not a second person ?
how can you tell there is not someone else there reading or such ?

It would appear that you can make logical decisions, but you have to ask the right questions.