Multiple Infrared Break Beam Sensor's //Help Needed\\

Hello,

For a project, Im setting up an array of IR sensors that will trigger audio samples in a Max patch when they are interrupted. Ive got one to work perfectly, but trying to get the second one to work is proving tricky. By the end of this project I will have 4 sensors set up but for the moment I want to try and get 2 to work propperly.

I brought the Adafruit break beam IR sensors and im modifying the example code to accommodate 3 more IR sensors (available here Overview | IR Breakbeam Sensors | Adafruit Learning System ). The code I have pasted below is what I have done so far. Both of the IR sensors register but badly. For example if the transmitter and the receiver are facing each other it should go '1' and then when interrupted again it goes '0'. What mine does it is goes '101010'. this causes the max patch the crash because max reads this as loads of quick changes.

If i change the sensorState = digitalRead(digital4); to read sensorState = digitalRead(SENSORPIN); on both the IR sensors then only the receiver on digital pin 5 works.

Does any one know what Im doing wrong? I've been trying to figure it out for the past couple of weeks on and off. Looking at other examples on the internet but I cant seem to improve this, does any one know where im going wrong?

Here is the example of the code:

/* 
  IR Breakbeam sensor demo!
*/

#define LEDPIN 13
#define SENSORPIN 4 
#define SENSORPIN 5


// variables will change:
int sensorState = 0, lastState=0;  
int digital4 = 4;
int digital5 = 5;
// 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(digital4, INPUT);  
  pinMode(digital5, INPUT);   
  digitalWrite(SENSORPIN, HIGH); // turn on the pullup
  
  Serial.begin(9600);
}

void loop(){
  // read the state of the pushbutton value:
  sensorState = digitalRead(digital4);
  // check if the sensor beam is broken
  // if it is, the sensorState is LOW:
  if (sensorState == LOW) {     
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);  
    //Serial.println("1");
  } 
  else {
    // turn LED off:
    digitalWrite(LEDPIN, LOW); 
    //Serial.println("0");
  }
  
  if (sensorState && !lastState) {
    Serial.println("2");
  } 
  if (!sensorState && lastState) {
    Serial.println("0");
  }
  lastState = sensorState;
   
   // read the state of the pushbutton value:
{  sensorState = digitalRead(digital5);
  // check if the sensor beam is broken
  // if it is, the sensorState is LOW:
  if (sensorState == LOW) {     
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);  
    //Serial.println("3");
  } 
  else {
    // turn LED off:
    digitalWrite(LEDPIN, LOW); 
    //Serial.println("1");
  }
  
  if (sensorState && !lastState) {
    Serial.println("1");
  } 
  if (!sensorState && lastState) {
    Serial.println("3");
  }
  lastState = sensorState;
}
}

Any help would be great!

pinMode(digital4, INPUT);  
  pinMode(digital5, INPUT);   
  digitalWrite(SENSORPIN, HIGH); // turn on the pullup

Only pin 5 will be set to input pullup.

#define SENSORPIN 4 
#define SENSORPIN 5

SENSOPIN can't be both 4 and 5. Since the last define is to 5, SENSORPIN =5.

thanks for the help, I managed to figure out what I did about 5 minutes after posting this which was abit annoying. I have got both receivers and transmitters working in the serial monitor. But I still cant stop the IR sensors transmitting data when they are facing each other. Its a constant stream of '0' and '1' and only stops when I break the beams. Do you know why this is happening? all the wires are connected properly.

Each sensor needs its own set of variables. I modified the Adafruit code to illustrate. Arrays and/or classes will make it easier to use more than a couple sensors.

#define LEDPIN 13

#define SENSORPIN_A 4
#define SENSORPIN_B 5

// variables will change:
int sensorState_A = 0, lastState_A = 0;
int sensorState_B = 0, lastState_B = 0;

void setup()
{
  // initialize the LED pin as an output:
  pinMode(LEDPIN, OUTPUT);
  // initialize the sensor pins as an input and enable internal pullup:
  pinMode(SENSORPIN_A, INPUT_PULLUP);
  pinMode(SENSORPIN_B, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop()
{
  // read the state of the sensor:
  sensorState_A = digitalRead(SENSORPIN_A);
  sensorState_B = digitalRead(SENSORPIN_B);

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

  if (sensorState_B == LOW)
  {
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);
  }
  else
  {
    // turn LED off:
    digitalWrite(LEDPIN, LOW);
  }

  if (sensorState_B && !lastState_B)
  {
    Serial.println("B Unbroken");
  }
  if (!sensorState_B && lastState_B)
  {
    Serial.println("B Broken");
  }
  lastState_B = sensorState_B;
  delay(500); // so we don't flood the serial port
}

The crap comments in the Adafruit code about pushbuttons is just wrong. I know you did not write the code, but to me, no comment is better than a wrong one, FYI.

Thanks a lot, Ill try this out tonight!

It works a charm. it didn't occur to me that I needed an extra sensorState argument at all, I thought it was something to do with the digital pins or that I had messed up the Void loop () section by playing around with it too much.

Thanks again.