IR break beam

SO im very new to this and i cant figure out how to get 5 different ir break sensors to light up 5 different leds. i can get one ir to work but once i start adding more i get error messages. Is there anyone that can tell me where im going wrong? What i do is copy and paste from #define LEDPIN 12 to the bottom, then i switch the numbers around to coraspond with another port.

/*
IR Breakbeam sensor demo!
*/
// Left red
#define LEDPIN 12
// Pin 12: Arduino has an LED connected on pin 12
// Pin 11: Teensy 2.0 has the LED on pin 11
// Pin 6: Teensy++ 2.0 has the LED on pin 6
// Pin 12: Teensy 3.0 has the LED on pin 12

#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); int led = 12;
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
delay(3000);
}
else {
// turn LED off:
digitalWrite(LEDPIN, LOW);
}

if (sensorState && !lastState) {
Serial.println("Unbroken");
}
if (!sensorState && lastState) {
Serial.println("Broken");
}
lastState = sensorState;
}

Why are you setting pinMode() in loop()? Do you really need to change the mode of the pin on every pass through loop()?

What have you tried, to extend the code to two sensors?

How connected ?
Is it ok to use pins 12,11,10,9,8 for outputs and 7,6,5,4,3 for inputs

byte i;
void setup() 
{
  for (i=12; i>7; i--)
  {  pinMode(i, OUTPUT); // 12..8  to LED+ 330 Ohm
     pinMode(i-5, INPUT);//  7..3 inputs from sensors
  }
//  Serial.begin(9600);
}

void loop()
{
  for (i=3; i<8; i++);
  digitalWrite(i+5; digitalRead(i));
}
void loop()
{
  for (i=3; i<8; i++);
  digitalWrite(i+5; digitalRead(i));
}

Do nothing 5 times, then turn pin 13 on or off, based on the state of pin 8. Hmmm...

knut_ny:
How connected ?
Is it ok to use pins 12,11,10,9,8 for outputs and 7,6,5,4,3 for inputs

byte i;

void setup()
{
 for (i=12; i>7; i--)
 {  pinMode(i, OUTPUT); // 12..8  to LED+ 330 Ohm
    pinMode(i-5, INPUT);//  7..3 inputs from sensors
 }
//  Serial.begin(9600);
}

void loop()
{
 for (i=3; i<8; i++);
 digitalWrite(i+5; digitalRead(i));
}

Yes i have an (open) board. so (example. when the ir breaks in port 2 it turns on the light in port 12 for 3 seconds then turns off. then when ir 3 breaks it turns on port 11 for 3 seconds and so forth.)

All my wiring is complete now, but im still stuck with the code. I know 100% my wiring is ready to go cause i still have control of one out of five, so ive been testing with that input 4 and output 12. I am using and Arduino UNO

So this is just to help give a bit more information about my project. I'm making a game where an object slides down a board and turns on the light in the slot where the object fell through. I need this done 5 times (5 different slots with a 3 second delay in the led). As of right now i'm using pin 4 as the input and 12 for an output and that works perfect but when i add to the code all i get is error messages.

I am very green with Arduino coding, ive been teaching my self since friday (yes i know i should read up on some more) but this is a time sencitive build for myself. If you can point me in the right direction cause i have not found alot of information on multiple IR BREAKBEAM anywhere online.

Thank you in advance.

i have not found alot of information on multiple IR BREAKBEAM anywhere online.

If you needed to buy a pencil, could you manage that?

If you needed to buy 5 pencils, would you need to google how to do that?

Doing something 5 times is NO more difficult than doing it once. Copy and paste, if you must. Use arrays and for loops if you have half a brain.

PaulS:
If you needed to buy a pencil, could you manage that?

If you needed to buy 5 pencils, would you need to google how to do that?

Doing something 5 times is NO more difficult than doing it once. Copy and paste, if you must. Use arrays and for loops if you have half a brain.

I have tried copy and paste and that's when I get the error code, that was my first though bit it didn't work.

I have tried copy and paste and that's when I get the error code

I guess you don't want help understanding or resolving the issue, since you did not post that code or the error messages.

I'm going to guess, though, that when you copied and pasted the code, you tried to have 5 variables containing different pin numbers but the same name, and 5 variables containing the different states but having the same name. That, obviously is not allowed.

/*
IR Breakbeam sensor demo!
*/
// Left red
#define LEDPIN 12
// Pin 12: Arduino has an LED connected on pin 12
// Pin 11: Teensy 2.0 has the LED on pin 11
// Pin 6: Teensy++ 2.0 has the LED on pin 6
// Pin 12: Teensy 3.0 has the LED on pin 12

#define SENSORPIN 3

// variables will change:
int sensorState = 3, lastState = 12; // 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); int led = 12;
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
delay(3000);
}
else {
// turn LED off:
digitalWrite(LEDPIN, LOW);
}

if (sensorState && !lastState) {
Serial.println("Unbroken");
}
if (!sensorState && lastState) {
Serial.println("Broken");
}
lastState = sensorState;
}

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

#define SENSORPIN 4

// variables will change:
int sensorState = 3, lastState = 11; // 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); int led = 11;
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
delay(3000);
}
else {
// turn LED off:
digitalWrite(LEDPIN, LOW);
}

if (sensorState && !lastState) {
Serial.println("Unbroken");
}
if (!sensorState && lastState) {
Serial.println("Broken");
}
lastState = sensorState;
}

Arduino: 1.8.2 (Windows 7), Board: "Arduino/Genuino Uno"

multiircode:65: error: redefinition of 'int sensorState'

int sensorState = 3, lastState = 11; // variable for reading the pushbutton status

^

C:\Users\User\Documents\Arduino\multiircode\multiircode.ino:15:5: note: 'int sensorState' previously defined here

int sensorState = 3, lastState = 12; // variable for reading the pushbutton status

^

multiircode:65: error: redefinition of 'int lastState'

int sensorState = 3, lastState = 11; // variable for reading the pushbutton status

^

C:\Users\User\Documents\Arduino\multiircode\multiircode.ino:15:22: note: 'int lastState' previously defined here

int sensorState = 3, lastState = 12; // variable for reading the pushbutton status

^

C:\Users\User\Documents\Arduino\multiircode\multiircode.ino: In function 'void setup()':

multiircode:67: error: redefinition of 'void setup()'

void setup() {

^

C:\Users\User\Documents\Arduino\multiircode\multiircode.ino:17:6: note: 'void setup()' previously defined here

void setup() {

^

C:\Users\User\Documents\Arduino\multiircode\multiircode.ino: In function 'void loop()':

multiircode:77: error: redefinition of 'void loop()'

void loop() {

^

C:\Users\User\Documents\Arduino\multiircode\multiircode.ino:27:6: note: 'void loop()' previously defined here

void loop() {

^

exit status 1
redefinition of 'int sensorState'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Pauls, I know im missing the most basic step, ive been trying to figure out where im going wrong. I do want your help I see what you mean but my half a brain cant see it.

is their anyone that can see my mistake?

is their anyone that can see my mistake?

Everyone can see it. The compiler told you what the problem is. You can have ONE loop() function, not two. You can have ONE setup() function, not two.

Every variable name MUST be unique.

PaulS, with your unorthodox way of teaching/helping, i have finished the code and it works perfect. sorry i was a slow learner but i used you advice so thank you.