IR Break Beam Timer Coding Question

When the beam is broken and stays broken, what do you want printed ?



Here is one beam only, you can add the second beam yourself.

When the beam is broken, you see Beam is broken.

When the beam is restored, you see Beam is restored.



//  https://forum.arduino.cc/t/ir-break-beam-timer-coding-question/1106808
//
//
//  Version    YY/MM/DD    Comments
//  =======    ========    ====================================================================
//  1.00       23/03/29    Running code
//
//
//

#define PULSE62_13             cli(); PINB = bit(PINB5); PINB = bit(PINB5); sei()

#define LEDon                  HIGH
#define LEDoff                 LOW

#define noRX                   HIGH
#define goodRX                 LOW

#define ENABLED                true
#define DISABLED               false

//IR Break Beam Sensors with Timer

const byte heartbeatLED      = 13;

const byte IR_Receiver1      = 4; 
const byte IR_LED1           = 3; 

bool IR_LED1state            = LEDoff;
bool broken1Flag             = ENABLED;

//timing stuff
unsigned long heartbeatTime;
unsigned long IR_LED1Time;

//                               s e t u p ( )
//*****************************************************************************
void setup()
{
  Serial.begin(115200); // Initialize serial communication

  pinMode(heartbeatLED, OUTPUT);
  digitalWrite(heartbeatLED, LEDoff);

  pinMode(IR_LED1, OUTPUT);

  pinMode(IR_Receiver1, INPUT_PULLUP);

} //END of   setup()


//                               l o o p ( )
//*****************************************************************************
void loop()
{
  //**********************************************
  //is it time to toggle the heartbeatLED ?
  if (millis() - heartbeatTime > 250)
  {
    //restart this TIMER
    heartbeatTime = millis();

    //toggle LED
    digitalWrite(heartbeatLED, digitalRead(heartbeatLED) == HIGH ? LOW : HIGH);
  }

  //**********************************************
  //is it time to toggle the IR_LED1 ?
  if (millis() - IR_LED1Time >= 50)
  {
    //restart this TIMER
    IR_LED1Time = millis();

    //is the IR #1 LED ON ?
    if (IR_LED1state == LEDon)
    {
      //stop modulation
      noTone(IR_LED1);

      //update the LED status
      IR_LED1state = LEDoff;
    }

    else
    {
      //turn on the IR #1 TX LED
      //start modulation
      tone(IR_LED1, 38000);

      //update the LED status
      IR_LED1state = LEDon;

      //some time for the receiver to react
      delayMicroseconds(500);

      //check to see if the beam is broken
      sampleIRreceiver1();
    }
  }

} //END of   loop()


//                    s a m p l e I R r e c e i v e r 1 ( )
//*****************************************************************************
void sampleIRreceiver1()
{
  //**********************************************
  //When the TX IR LED is modulating, the receiver output is LOW.
  //At this point, we should be receiving a signal from the TX #1 IR LED.
  //If we are not receiving, the beam has been broken.
  if (broken1Flag == ENABLED && digitalRead(IR_Receiver1) == noRX)
  {
    Serial.println("Beam is broken");

    //The user has been notified about the loss in IR.
    broken1Flag = DISABLED;
  }

  //**********************************************
  //has the IR beam now been restored ?
  if (broken1Flag == DISABLED && digitalRead(IR_Receiver1) == goodRX)
  {
    Serial.println("Beam is restored");

    //the user has been notified about the restoring of the IR signal
    broken1Flag = ENABLED;
  }

} //end of   sampleIRreceiver1()

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