Hello All.
I am working on a project with break beam sensors.
I have the sketch for one sensor (pair) and is working fine. However i need to use 12 sensors in my project, and i need help to edit the below sketch accordingly;
#define LEDPIN 4
// Pin 4: Arduino has an LED connected on pin 4
#define SENSORPIN 2
// 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;
}
Packe lastState und sensorState in eine struct und erstelle ein Array davon. Mit einem Beispiel kann ich leider nicht dienen, mir ist die C/C++ Syntax zu fremd.
Oder ein Array aus jeder Variablen: int sensorState[12], lastState[12];
Dann alle Sensoren in einer Schleife abarbeiten.
Are you going to use 12 leds with the 12 sensors ?
You may have to deal with IR reflections causing spurious results. Possible solutions are either by the design of the physical enclosure or in code by switching the leds / sensors in sequence.
The code attached works and does what i need but is only for one input. I am stuck on adding 11 more sensors as input.
Basically what i am aiming here, is to have 12 beams around and object. Whenever one of the beams is interrupted, a relay is activated and trigger and alarm in the Milestone VMS.
Again, everything works as described but only with one beam
IR radiation is emitted in a wide angle and is heavily reflected by many materials, as you may know from your TV... remote control. Breaking one such "beam" may be undetected.
Thank you for the feedback DrDiettrich. I will place the sensors 8cm apart (as required in my project) and see if there are any interferences. Then i can test the beams one by one with the code i have. Just don't know enough about coding to enable 12 different input pins
Hello again,
After taking in consideration your advices and reading all related posts, i managed to figure this out for the most part.
All sensors working and detecting. The only inconvenience now is: If a beam is interrupted, the serial monitor is continuously displaying the "Broken" message. In my first code it would only print the message once.
int SENSORPIN [12] = {2,3,4,5,6,7,8,9,10,11,12,13};
// variables will change:
int sensorState = 0, lastState= 0; // variable for reading the pushbutton status
void setup() {
for (int eachSensor = 0; eachSensor <= 11; eachSensor++) {
pinMode(SENSORPIN [eachSensor] ,INPUT_PULLUP);
}
digitalWrite(SENSORPIN, HIGH); // turn on the pullup
Serial.begin(9600);
}
void loop() {
// check the sensor array.
for(int s = 0; s <= 11; s++) {
sensorState = digitalRead(SENSORPIN[s]);
// if it is, the sensorState is LOW:
if (sensorState == LOW) {
}
if (!sensorState && lastState) {
Serial.println("Broken ,");
}
lastState = sensorState;}
}
int SENSORPIN [12] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int lastState[ 12 ] = { 0 } ;
// variables will change:
int sensorState = 0 ; // variable for reading the pushbutton status
void setup() {
for (int eachSensor = 0; eachSensor <= 11; eachSensor++) {
pinMode(SENSORPIN [eachSensor] , INPUT_PULLUP);
}
// digitalWrite(SENSORPIN, HIGH); // turn on the pullup // redundant
Serial.begin(9600);
}
void loop() {
// check the sensor array.
for (int s = 0; s <= 11; s++) { // please use < 12 instead of <= 11
// yes, it has the same effect but much nicer to look at when the array
// is specified as arrayXX[ 12 ]
sensorState = digitalRead(SENSORPIN[s]);
// if it is, the sensorState is LOW:
if (sensorState == LOW) {
// this isn't doing much
}
if (!sensorState && lastState[ s ] ) {
Serial.println("Broken ,");
}
lastState[ s ] = sensorState;
}
}