Coding a IR Breakbeam Sensors for labeler

Hi there.

I'm a vegetable with coding, simply looking for help to build a labeler, i need to hold signal on for 2 sec after beam broken until next triger. As i understanding - delay does not work in this case.
I have in use following code with millis. I tried build code with millis as was recommended at adafruit, but i'm a vegetable with coding, this all what i able to build)))

   /*
      IR Breakbeam sensor demo!
    */

    #define LEDPIN 13
      // Pin 13: Arduino has an LED connected on pin 13
      // Pin 11: Teensy 2.0 has the LED on pin 11
      // Pin  6: Teensy++ 2.0 has the LED on pin 6
      // Pin 13: Teensy 3.0 has the LED on pin 13

    #define SENSORPIN 4

    // variables will change:
    int sensorState = 0, lastState=0;         // variable for reading the pushbutton status

    void setup() {
      // initialize the LED pin as an output:
      pinMode(LEDPIN, OUTPUT);     
      // initialize the sensor pin as an input:
      pinMode(SENSORPIN, INPUT);     
      digitalWrite(SENSORPIN, HIGH); // turn on the pullup
     
      Serial.begin(9600);
    }

    void loop(){
      // read the state of the pushbutton value:
      sensorState = digitalRead(SENSORPIN);

      // check if the sensor beam is broken
      // if it is, the sensorState is LOW:
      if (sensorState == LOW) {     
        // turn LED on:
        digitalWrite(LEDPIN, HIGH); 
      }
      else {
        // turn LED off:
        digitalWrite(LEDPIN, LOW);
      }
     
      if (sensorState && !lastState) {
        Serial.println("Unbroken");
      }
      if (!sensorState && lastState) {
        Serial.println("Broken");
      }
      lastState = sensorState;
    }

IR Breakbeam Sensors from adafruit:

I will be grateful for any help.

Many thanks, Thomas.

i need to hold signal on for 2 sec after beam broken until next triger.

That does not make sense. Does the Arduino need to do anything during the 2 seconds and what do you mean by "until next trigger" ?

Hi UKHeliBob.

Thanks for your quick response.
Once beam broken (triggered) LED on for 2 sec. After 2 sec (beam still broken) LED off until second trigger (beam broken) ...process repeated - beam broken (triggered) LED on for 2 sec...

This is for labeler, roll with labels run around two sec to unroll part of label to be easy unattached from roll. As a sample of idea HERMA 452C - Small-budget top labelling machine - YouTube

Many thanks.

beam still broken

That is the part that makes delay() unsuitable because the program must check whether the beam is still broken during the wait period. What should happen if the beam becomes unbroken during the 2 seconds ?

I have in use following code with millis.

The code that you posted uses neither millis() or delay()

UKHeliBob:
What should happen if the beam becomes unbroken during the 2 seconds ?

As shown in given sample video - beam broken when package run over conveyor. Once beam broken, rol with labels runs half turn (say those 2 sec) to unroll end of label and wait while beam unbroken (while package finish to runs over the beam - beam unbroken). Proces reapeted again ...beam broken when following package run over conveyor...

UKHeliBob:
The code that you posted uses neither millis() or delay()

Yes, i know, thats why im here ...i'm a vegetable with coding)))