Room light control with two laser beam in arduino

hi everyone, i want to build a system to turn on/off room lights. i want to used two laser beams. if one person go inside to room first and second laser beam borken and arduino calculate one person inside and turn on the lights . while one person in the room second person want to go inside the room arduino do nothing. until room inside counter says zero person in the room then lights off. two laser beams and a relay and also a arduino uno. can you help the code. i ant to write a code from chatgpt but didn t work

The basics of what you’re trying to achieve our account —

If the counter is greater than zero the light is on when it returns to zero, the light turns off.

Get that working first and then use your laser beams being broken in sequence - forward (ingoing), or reverse (outgoing) to increment or decrement the counter.

You do not actually mean laser; that would blind people. You probably mean a PIR, which is a very imprecise sensor. If you meant an opening detector like a doorway with an IR transmitter on one side and a receiver on the other, then that would act as a 'stile' type. Now, keeping count of beam breakage will almost work, but to really work, each doorway needs two detectors, and the sequence of the beam breaking tells you if the person went in or out. Of course, it all goes up in smoke when a person gets halfway there and changes their mind.
This is an excellent candidate for a state machine and precision non-blocking timing code. Example, how many millis does ut take a adult to go from point A to point A plus 10cm. Now, how many times when little Johnny is running to get away from his annoying little sister? What happens if someone goes under the beam or jumps over the beam? Does that mean 4 beams in total, or perhaps 6?

Both my cats learned quickly to jump over the beams I hoped they would break entering forbidden zones.

One cat learned to slip in behind his "friend" when she entered her safe space, literal tailgating.

So I hope you aren't dealing w/ cats.

a7

Not all lasers are dangerous. ;) And some are not visible, although wavelength is unrelated to with power and safety.

Does it work as a simple "counter"?

Can you control the relay with the relay and is it working to turn the lights on & off?

One small step at a time and that includes the software.

I usually work on the input first (that would be you laser sensors).

Then you can read those you can work on the output (relay). For example, run the Blink Example to see if you can blink the light.

Then "build" your logic, maybe just turn the light on when the first sensor is triggered, and add steps from there...

Personally, I would NEVER attempt to write the code for someone else's project unless I had a schematic, and the hardware so I could test and debug. (Plus I'm lazy. :P )

Does ChatGTP understand the hardware and schematic?

After that you have describe EXACTLY the logic of what the code is supposed to do (maybe in pseudocode) and by that time you're almost done anyway. ;)

Defining the software specs is a BIG part of programming and in the real world some thing(s) are often overlooked and only found during testing.

I assume Occupancy Sensing Switches use PIRs.

PIR is what security systems use inside the house. They are very gross indicators of human presence (can pick up pets), for instance, walking past an open door that has a PIR pointed that way will often trigger, unless it is desensitized or too far away. It isn't a problem for that system since the 'I am at home' switch is on and that causes that sensor to be ignored.

This is a fun part of your project. Research "state machine" because you need to keep track of which beam was tripped first, and the order in which the beams are tripped. It will test your logic, but that is the fun part.

This is a great way to start...

[edit] this is how I see your set up... (power and wires are invisible and work magically).

[edit: code updated to show stages of entering/exiting (first trip), entered/exited (second trip)

// https://forum.arduino.cc/t/room-light-control-with-two-laser-beam-in-arduino/1414689

#define outPin 2
#define inPin 3
int inLED = 4, outLED = 5;
int occupants = 0, occupantsOld, entered, exited;
bool entering = false, exiting = false;
int debounce = 150;

void setup() {
  Serial.begin(115200);
  pinMode(outPin, INPUT_PULLUP);
  pinMode(inPin, INPUT_PULLUP);
  pinMode(inLED, OUTPUT);
  pinMode(outLED, OUTPUT);
  Serial.println("Hello, World!");
}

void loop() {
  if (entering == false && exiting == false) { // no sequence started
    if (digitalRead(outPin) == LOW) {
      entering = true;
      exiting = false;
      digitalWrite(outLED, HIGH);
      Serial.print("Entering. ");
    }
    if (digitalRead(inPin) == LOW) {
      entering = false;
      exiting = true;
      digitalWrite(inLED, HIGH);
      Serial.print("Exiting.  ");
    }
    delay(debounce);
  }

  if (entering == true && exiting == false) { // enter sequence started
    if (digitalRead(inPin) == LOW) {
      digitalWrite(inLED, HIGH);
      Serial.print("Entered. ");
      delay(debounce);
      occupants++;
      entered++;
    }
  }

  if (entering == false && exiting == true) { // exit sequence started
    if (digitalRead(outPin) == LOW) {
      digitalWrite(outLED, HIGH);
      Serial.print("Exited.  ");
      delay(debounce);
      occupants--;
      exited++;
    }
  }

  if (occupants != occupantsOld) { // if count changes
    occupantsOld = occupants; // store current count
    Serial.print("Occupants: ");
    Serial.print(occupants);
    Serial.print(" Entered: ");
    Serial.print(entered);
    Serial.print(" Exited: ");
    Serial.print(exited);
    Serial.println();
    entering = false; // reset sequences
    exiting = false;
      digitalWrite(inLED, LOW);
      digitalWrite(outLED, LOW);
  }
}
diagram.json for the wokwi nerds
{
  "version": 1,
  "author": "",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": -51.4,
      "left": 134.4,
      "attrs": { "color": "blue", "xray": "1" }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn2",
      "top": -99.4,
      "left": 134.4,
      "attrs": { "color": "green", "xray": "1" }
    },
    {
      "type": "wokwi-text",
      "id": "text1",
      "top": -86.4,
      "left": 201.6,
      "attrs": { "text": "IN" }
    },
    {
      "type": "wokwi-text",
      "id": "text2",
      "top": -38.4,
      "left": 201.6,
      "attrs": { "text": "OUT" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -106.8,
      "left": 39.8,
      "rotate": 270,
      "attrs": { "color": "green", "flip": "" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": -58.4,
      "left": 39.8,
      "rotate": 270,
      "attrs": { "color": "blue", "flip": "" }
    }
  ],
  "connections": [
    [ "nano:GND.2", "btn1:2.l", "black", [ "v0" ] ],
    [ "nano:2", "btn1:1.l", "green", [ "v0" ] ],
    [ "nano:GND.2", "btn2:2.l", "black", [ "v0" ] ],
    [ "nano:3", "btn2:1.l", "green", [ "v0" ] ],
    [ "nano:GND.2", "led2:C", "black", [ "v0" ] ],
    [ "nano:GND.2", "led1:C", "black", [ "v0" ] ],
    [ "nano:5", "led2:A", "green", [ "v0" ] ],
    [ "nano:4", "led1:A", "green", [ "v0" ] ]
  ],
  "dependencies": {}
}