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!