Arduino IR Sensor Beam Break Count

Hi,

I was wondering if I could get help writing codes for a "beam break for 2-minute intervals and count how many times the beam has broken."

Here is my code for detecting motions, but I was wondering how I can add 2-minute intervals in this code. So that Arduino can count how many times the beam has been broken for 2-minute intervals.

//IR Photodiode Receivers
#define IR_Photodiode 4
//IR LED Pins
#define IR_LED 13 //right
//ints to monitor the beam break status in each of the ports
int sensorState = 0;
int lastState = 0;
void setup() {
// put your setup code here, to run once:
pinMode(IR_Photodiode, INPUT);
digitalWrite(IR_Photodiode, HIGH);
pinMode(IR_LED, OUTPUT);
digitalWrite(IR_LED, HIGH);
Serial.begin(115200); //turns on the serial monitor
Serial.println("Begin Session");
}
void loop() {

  sensorState = digitalRead(IR_Photodiode);
  if (sensorState && !lastState) {
    Serial.println("Right Port Entry");
  }
  lastState = sensorState;

// put your main code here, to run repeatedly:
}

Thank you!

hi!

maybe not an interval but a timer wich will trigger a counter after 2min then reset, or reset if the beam is back.
I propose you link a value to the state of your beam (like a single button triggered or not):

A) the beam is triggered and was on before (last loop occurence): you start a timer
B) the beam is triggered and was triggered before: you don't touch the timer
C) the beam is no more triggered but was triggered before: you reset the timer

in parallel, you test the timer for it goes up to 2min. It normally happens only in your desired condition (cuz it will be reset in case C). There, you add 1 in a counter, then reset the timer (or maybe wait for the beam to be on again).

Hope it will helps!

EDIT: I confuse myself by post reading: "beam triggered" is : "something breaks the beam".

Hi! I appreciate your reply!

I'm sorry for the confusion,

I want to generate a code that shows how many times the beam has been triggered for a 2-minute duration. May I get some help on this?

Best,

no the confusion was mine by using "triggered" and the example of the button.
I think I am ok with your description.
What do you think of my proposal?

Yes, proposal C is mainly aligned with my project (the beam gets no more triggered after 2 minutes of working time).

To do so, may I please get some help writing a code for the "timer"? My current code can detect the number of triggers and I want to add 2-minute timers to that.

I hope my terrible english won't make more bad than good. But I sincerely wish I helped you.

actually, it was not a pick up list for or A or B or C, but a short definitions of the 3 states your value can have. You go from a state to another in your code depending on the timer and the beam.

Timer is easily used this way:

unsigned long time_stamp = millis();

the time_stamp value now contains the number of milliseconds elapsed since the board is on. You have no control on its value, but you can use it to measure the time goes on.
So for your 2 sec intervals, I will use that kind of statment:

if (millis() - time_stamp > interval){
// do what you need here, for example: add 1 to a counter
time_stamp = millis(); // reset the counter by transferring actual elapsed time into time_stamp data
}

in parallel, I would test the state of your beam (cut or not) and store this state in a value (your code already do that). But I add a verification to know what WAS the state of the beam. This is the meaning of the A, B and C state I described.
It is a "trick" commonly used for joypad or anything with "long click".
Consider having a value storing the last known state of the beam, so you can compare to the newest reading of your beam.

My malicious pleasure to not give you a written code. But we all in this forum will be glad to help fixing your attempts!

EDIT: please edit your first post and put your entire code in tags </> :+1:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.