Arduino controled stairs lights with 2 PIR sensors not working

The code dosen't work as i want it , if i triger only one part for exemple A5 its gona start execute all the code and not that just one part , i think i do something wrong .

I use an arduino uno copy ,the relay board that i use trigers the relays on low signal and i plan to use a SN74LS32 to triger A3 , A2, A1 and make it more interesting , i also plan to use am312 PIR sensors.
its my first time working with an arduino
Thanks in advance

int sensorPin1 = A4;  //Bottom - Up sensor
int sensorPin2 = A5;  //Top - Down sensor
int sensorPin3 = A3;  //or gate output
int sensorPin4 = A2;  //or gate output
int sensorPin5 = A1;  //or gate auto off after time
int sensorStatus1 = 1;
int sensorStatus2 = 1;
int sensorStatus3 = 1;
int sensorStatus4 = 1;
int sensorStatus5 = 1;
int ledPin0 = 0;    // The outputlight
int ledPin1 = 1;    // The outputlight
int ledPin2 = 2;    // The outputlight
int ledPin3 = 3;    // The outputlight
int ledPin4 = 4;    // The outputlight
int ledPin5 = 5;    // The outputlight
int ledPin6 = 6;    // The outputlight
int ledPin7 = 7;    // The outputlight
int ledPin8 = 8;    // The outputlight
int ledPin9 = 9;    // The outputlight
int ledPin10 = 10;  // The outputlight
int ledPin11 = 11;  // The outputlight
int ledPin12 = 12;  // The outputlight
int ledPin13 = 13;  // The outputlight

void setup() {
  pinMode(ledPin0, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(ledPin8, OUTPUT);
  pinMode(ledPin9, OUTPUT);
  pinMode(ledPin10, OUTPUT);
  pinMode(ledPin11, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  pinMode(ledPin13, OUTPUT);
  pinMode(sensorPin1, INPUT);
  pinMode(sensorPin2, INPUT);
  pinMode(sensorPin3, INPUT);
  pinMode(sensorPin4, INPUT);
  pinMode(sensorPin5, INPUT);
}
void loop() {

  sensorStatus1 = digitalRead(A4);  //Stairs Bottom Sensor - Motion Up
  if (sensorStatus1 == LOW) {
    // if bottom sensor is triggered lights travel upward
    digitalWrite(0, LOW);
    delay(200);
    digitalWrite(1, LOW);
    delay(200);
    digitalWrite(2, LOW);
    delay(200);
    digitalWrite(3, LOW);
    delay(200);
    digitalWrite(4, LOW);
    delay(200);
    digitalWrite(5, LOW);
    delay(200);
    digitalWrite(6, LOW);
    delay(200);
    digitalWrite(7, LOW);
    delay(200);
    digitalWrite(8, LOW);
    delay(200);
    digitalWrite(9, LOW);
    delay(200);
    digitalWrite(10, LOW);
    delay(200);
    digitalWrite(11, LOW);
    delay(200);
    digitalWrite(12, LOW);
    delay(200);
    digitalWrite(13, LOW);
  }

 
  {
    sensorStatus3 = digitalRead(A3);
    if (sensorStatus3 == LOW)
      delay(200);
    digitalWrite(0, HIGH);
    delay(200);
    digitalWrite(1, HIGH);
    delay(200);
    digitalWrite(2, HIGH);
    delay(200);
    digitalWrite(3, HIGH);
    delay(200);
    digitalWrite(4, HIGH);
    delay(200);
    digitalWrite(5, HIGH);
    delay(200);
    digitalWrite(6, HIGH);
    delay(200);
    digitalWrite(7, HIGH);
    delay(200);
    digitalWrite(8, HIGH);
    delay(200);
    digitalWrite(9, HIGH);
    delay(200);
    digitalWrite(10, HIGH);
    delay(200);
    digitalWrite(11, HIGH);
    delay(200);
    digitalWrite(12, HIGH);
    delay(200);
    digitalWrite(13, HIGH);
  }
 }

  sensorStatus2 = digitalRead(A5);  //Stairs Top Sensor - Motion Down
  if (sensorStatus2 == LOW) {
    // if top sensor is triggered lights travel downward
    delay(200);
    digitalWrite(13, LOW);
    delay(200);
    digitalWrite(12, LOW);
    delay(200);
    digitalWrite(11, LOW);
    delay(200);
    digitalWrite(10, LOW);
    delay(200);
    digitalWrite(9, LOW);
    delay(200);
    digitalWrite(8, LOW);
    delay(200);
    digitalWrite(7, LOW);
    delay(200);
    digitalWrite(6, LOW);
    delay(200);
    digitalWrite(5, LOW);
    delay(200);
    digitalWrite(4, LOW);
    delay(200);
    digitalWrite(3, LOW);
    delay(200);
    digitalWrite(2, LOW);
    delay(200);
    digitalWrite(1, LOW);
    delay(200);
    digitalWrite(0, LOW);
  }

  sensorStatus4 = digitalRead(A2);
  if (sensorStatus4 == LOW) {
    digitalWrite(13, HIGH);
    delay(200);
    digitalWrite(12, HIGH);
    delay(200);
    digitalWrite(11, HIGH);
    delay(200);
    digitalWrite(10, HIGH);
    delay(200);
    digitalWrite(9, HIGH);
    delay(200);
    digitalWrite(8, HIGH);
    delay(200);
    digitalWrite(7, HIGH);
    delay(200);
    digitalWrite(6, HIGH);
    delay(200);
    digitalWrite(5, HIGH);
    delay(200);
    digitalWrite(4, HIGH);
    delay(200);
    digitalWrite(3, HIGH);
    delay(200);
    digitalWrite(2, HIGH);
    delay(200);
    digitalWrite(1, HIGH);
    delay(200);
    digitalWrite(0, HIGH);
  }

  sensorStatus5 = digitalRead(A1);
  if (sensorStatus5 == LOW) {
    delay(30000);
    digitalWrite(13, HIGH);
    digitalWrite(0, HIGH);
    delay(300);
    digitalWrite(12, HIGH);
    digitalWrite(1, HIGH);
    delay(300);
    digitalWrite(11, HIGH);
    digitalWrite(2, HIGH);
    delay(300);
    digitalWrite(10, HIGH);
    digitalWrite(3, HIGH);
    delay(300);
    digitalWrite(9, HIGH);
    digitalWrite(4, HIGH);
    delay(300);
    digitalWrite(8, HIGH);
    digitalWrite(5, HIGH);
    delay(300);
    digitalWrite(7, HIGH);
    digitalWrite(6, HIGH);
  }
}

Simplify your code and work your way up from there once a small section works.
Serial prints at certain points will help.
Comments also help, as you may remember now what is happening, in the passing of time you will forget, hence what the.......?????

1 Like

Post an annotated schematic and note any wires over 10"/25cm. Lengths longer then this make great antennas, the shorter ones are antennas as well.

It was actualy because a badly put brace , i will upload the final code If someone wants to try it or use it

here is the working version

`int sensorPin1 = A4;  //Bottom - Up sensor
int sensorPin2 = A5;  //Top - Down sensor
int sensorPin3 = A3;  //or gate output
int sensorPin4 = A2;  //or gate output
int sensorPin5 = A1;  //or gate auto off after time
int sensorStatus1 = 1;
int sensorStatus2 = 1;
int sensorStatus3 = 1;
int sensorStatus4 = 1;
int sensorStatus5 = 1;
int ledPin0 = 0;    // The outputlight
int ledPin1 = 1;    // The outputlight
int ledPin2 = 2;    // The outputlight
int ledPin3 = 3;    // The outputlight
int ledPin4 = 4;    // The outputlight
int ledPin5 = 5;    // The outputlight
int ledPin6 = 6;    // The outputlight
int ledPin7 = 7;    // The outputlight
int ledPin8 = 8;    // The outputlight
int ledPin9 = 9;    // The outputlight
int ledPin10 = 10;  // The outputlight
int ledPin11 = 11;  // The outputlight
int ledPin12 = 12;  // The outputlight
int ledPin13 = 13;  // The outputlight

void setup() {
  pinMode(ledPin0, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(ledPin8, OUTPUT);
  pinMode(ledPin9, OUTPUT);
  pinMode(ledPin10, OUTPUT);
  pinMode(ledPin11, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  pinMode(ledPin13, OUTPUT);
  pinMode(sensorPin1, INPUT);
  pinMode(sensorPin2, INPUT);
  pinMode(sensorPin3, INPUT);
  pinMode(sensorPin4, INPUT);
  pinMode(sensorPin5, INPUT);
}
void loop() {

  sensorStatus1 = digitalRead(A4);  //Stairs Bottom Sensor - Motion Up
  if (sensorStatus1 == LOW) {
    // if bottom sensor is triggered lights travel upward
    digitalWrite(0, LOW);
    delay(200);
    digitalWrite(1, LOW);
    delay(200);
    digitalWrite(2, LOW);
    delay(200);
    digitalWrite(3, LOW);
    delay(200);
    digitalWrite(4, LOW);
    delay(200);
    digitalWrite(5, LOW);
    delay(200);
    digitalWrite(6, LOW);
    delay(200);
    digitalWrite(7, LOW);
    delay(200);
    digitalWrite(8, LOW);
    delay(200);
    digitalWrite(9, LOW);
    delay(200);
    digitalWrite(10, LOW);
    delay(200);
    digitalWrite(11, LOW);
    delay(200);
    digitalWrite(12, LOW);
    delay(200);
    digitalWrite(13, LOW);
  }

 
  
    sensorStatus3 = digitalRead(A3);
    if (sensorStatus3 == LOW){
      delay(200);
    digitalWrite(0, HIGH);
    delay(200);
    digitalWrite(1, HIGH);
    delay(200);
    digitalWrite(2, HIGH);
    delay(200);
    digitalWrite(3, HIGH);
    delay(200);
    digitalWrite(4, HIGH);
    delay(200);
    digitalWrite(5, HIGH);
    delay(200);
    digitalWrite(6, HIGH);
    delay(200);
    digitalWrite(7, HIGH);
    delay(200);
    digitalWrite(8, HIGH);
    delay(200);
    digitalWrite(9, HIGH);
    delay(200);
    digitalWrite(10, HIGH);
    delay(200);
    digitalWrite(11, HIGH);
    delay(200);
    digitalWrite(12, HIGH);
    delay(200);
    digitalWrite(13, HIGH);
  }
 

  sensorStatus2 = digitalRead(A5);  //Stairs Top Sensor - Motion Down
  if (sensorStatus2 == LOW) {
    // if top sensor is triggered lights travel downward
    delay(200);
    digitalWrite(13, LOW);
    delay(200);
    digitalWrite(12, LOW);
    delay(200);
    digitalWrite(11, LOW);
    delay(200);
    digitalWrite(10, LOW);
    delay(200);
    digitalWrite(9, LOW);
    delay(200);
    digitalWrite(8, LOW);
    delay(200);
    digitalWrite(7, LOW);
    delay(200);
    digitalWrite(6, LOW);
    delay(200);
    digitalWrite(5, LOW);
    delay(200);
    digitalWrite(4, LOW);
    delay(200);
    digitalWrite(3, LOW);
    delay(200);
    digitalWrite(2, LOW);
    delay(200);
    digitalWrite(1, LOW);
    delay(200);
    digitalWrite(0, LOW);
  }

  sensorStatus4 = digitalRead(A2);
  if (sensorStatus4 == LOW) {
    digitalWrite(13, HIGH);
    delay(200);
    digitalWrite(12, HIGH);
    delay(200);
    digitalWrite(11, HIGH);
    delay(200);
    digitalWrite(10, HIGH);
    delay(200);
    digitalWrite(9, HIGH);
    delay(200);
    digitalWrite(8, HIGH);
    delay(200);
    digitalWrite(7, HIGH);
    delay(200);
    digitalWrite(6, HIGH);
    delay(200);
    digitalWrite(5, HIGH);
    delay(200);
    digitalWrite(4, HIGH);
    delay(200);
    digitalWrite(3, HIGH);
    delay(200);
    digitalWrite(2, HIGH);
    delay(200);
    digitalWrite(1, HIGH);
    delay(200);
    digitalWrite(0, HIGH);
  }

  sensorStatus5 = digitalRead(A1);
  if (sensorStatus5 == LOW) {
    delay(30000);
    digitalWrite(13, HIGH);
    digitalWrite(0, HIGH);
    delay(300);
    digitalWrite(12, HIGH);
    digitalWrite(1, HIGH);
    delay(300);
    digitalWrite(11, HIGH);
    digitalWrite(2, HIGH);
    delay(300);
    digitalWrite(10, HIGH);
    digitalWrite(3, HIGH);
    delay(300);
    digitalWrite(9, HIGH);
    digitalWrite(4, HIGH);
    delay(300);
    digitalWrite(8, HIGH);
    digitalWrite(5, HIGH);
    delay(300);
    digitalWrite(7, HIGH);
    digitalWrite(6, HIGH);
  }
}
`

Your code is a great example how code can be written much shorter with some more programming knowledge about

  • arrays
  • for-loops
const byte sensorPinNr[6] = {0, A4, A5, A3, A2, A1};
byte sensorStatus[6] = {0, 1, 1, 1, 1, 1};
byte sensorNr;

const unsigned long delayTime = 200;

void setup() {
  for (byte ledPinNr = 0; ledPinNr < 14; ledPinNr++) {
  pinMode(ledPinNr, OUTPUT);
  }

  for(byte sensorNr = 1; sensorNr < 6; sensorNr++) {
  pinMode(sensorPinNr[sensorNr], INPUT);
  }
}


void loop() {

  sensorNr = 1;
  sensorStatus[sensorNr] = digitalRead(sensorPinNr[sensorNr]);  
  if (sensorStatus[sensorNr] == LOW) {
    // if bottom sensor is triggered lights travel upward

    for (byte ledNr = 0; ledNr < 14; ledNr++) {
      digitalWrite(ledNr, LOW);
      delay(delayTime);
    }
  }

  sensorNr = 3;
  sensorStatus[sensorNr] = digitalRead(sensorPinNr[sensorNr]);  
  if (sensorStatus[sensorNr] == LOW) {
    for (byte ledNr = 0; ledNr < 14; ledNr++) {
      digitalWrite(ledNr, HIGH);
      delay(delayTime);
    }
  }

  sensorNr = 2;
  sensorStatus[sensorNr] = digitalRead(sensorPinNr[sensorNr]);  
  if (sensorStatus[sensorNr] == LOW) {
    for (byte ledNr = 13; ledNr >= 0; ledNr--) {
      digitalWrite(ledNr, LOW);
      delay(delayTime);
    }
  }

  sensorNr = 4;
  sensorStatus[sensorNr] = digitalRead(sensorPinNr[sensorNr]);  
  if (sensorStatus[sensorNr] == LOW) {
    for (byte ledNr = 13; ledNr >= 0; ledNr--) {
      digitalWrite(ledNr, HIGH);
      delay(delayTime);
    }
  }

  sensorNr = 5;
  sensorStatus[sensorNr] = digitalRead(sensorPinNr[sensorNr]);  
  if (sensorStatus[sensorNr] == LOW) {
    for (byte ledNr = 0; ledNr < 7; ledNr++) {
      digitalWrite(13 - ledNr, HIGH);
      digitalWrite(     ledNr, HIGH);
      delay(delayTime);
    }
  }

}
3 Likes

Indeed. The only small difference I would suggest is with loops like

  sensorNr = 4;
  sensorStatus[sensorNr] = digitalRead(sensorPinNr[sensorNr]);  
  if (sensorStatus[sensorNr] == LOW) {
    for (byte ledNr = 13; ledNr >= 0; ledNr--) {
      digitalWrite(ledNr, HIGH);
      delay(delayTime);
    }
  }

Of course loops can go forwards or backwards, in steps formed by incrementing or decrementing or even adding or subtracting values different to what incrementing or decrementing do.

But my brain can't (would rather not) handle that very well.

A recent thread on getting a sequence of LEDs to illuminate in a fashionable and handsome manner had many examples offered using loop tricks.

For me it only came together by using the for loop in only one way: to control the body for N executions as the index went form 0 to N - 1.

  for (int ii = 0; ii < N; ii++) {

// whatever 

  }

and using simple maths inside the loop to make things go backwards or otherwise dance.

Also, the improved code still exploits and therefor depends on the LEDs being on a contikulous stretch of i/o pin numbers, including here including the use of 0 and 1, which might wish to be avoided so as not to interfere with their serial monitor and code uploading functions.

Adding an array in the way @StefanL38 did for the sensors

const byte ledPinNr[6] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

and letting the compiler count them

const byte nrOfLEDs = sizeof ledPinNr / sizeof *ledPinNr:

gives us

  sensorNr = 4;
  sensorStatus[sensorNr] = digitalRead(sensorPinNr[sensorNr]);  
  if (sensorStatus[sensorNr] == LOW) {
    for (int ii = 0; ii < nrOfLEDs; ii++) {
        digitalWrite(ledPinNr[nrOfLEDs - 1 - ii], HIGH);
        delay(delayTime);
    }
  }

a7

2 Likes

@alto777 @StefanL38 thank you for the suggestions and ideas, for the moment I will stick to the more rudimentary version. I will try to implement arrays, at the moment I am trying to do something with boolean mathematics to simulate 3 logic gates, to stop using dedicated circuits to get the desired result, but once I started to integrate the logic gates I realized that i don't have enough input / output pins , i have another r3 uno and a mega 2560, I thought of using two r3 uno in parallel, one for logic gates and one for controlling the relay module, or to use the mega r3 2560 for everything, but i think it will run into another problem if i try to run everything on the mega 2560, i think i will activate another part of the code bc i configured the logic gates to read the inputs that the board already receives from the sensor and instead of runing the logic gate part to do something else, I haven't tested the code yet, i will leave the code here if you guys want to take a look and potentially tell me if I'm worrying for nothing

I haven't set the logic gates as they should yet, so try to ignore their functionality

int sensorPin2 = A5;  
int sensorPin1 = A4;  
int sensorPin3 = A3;  
int sensorPin4 = A2;  
int sensorPin5 = A1;  
int sensorStatus1 = 1;
int sensorStatus2 = 1;
int sensorStatus3 = 1;
int sensorStatus4 = 1;
int sensorStatus5 = 1;
int ledPin0 = 0;    
int ledPin1 = 1;    
int ledPin2 = 2;    
int ledPin3 = 3;    
int ledPin4 = 4;    
int ledPin5 = 5;    
int ledPin6 = 6;    
int ledPin7 = 7;    
int ledPin8 = 8;    
int ledPin9 = 9;    
int ledPin10 = 10;  
int ledPin11 = 11;  
int ledPin12 = 12;  
int ledPin13 = 13;  
int gateout1 = 14;
int gateout2 = 15;
int gateout3 = 16;

void setup() {

  pinMode(gateout1, OUTPUT);
  pinMode(gateout2, OUTPUT);
  pinMode(gateout3, OUTPUT);
  
  pinMode(ledPin0, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(ledPin8, OUTPUT);
  pinMode(ledPin9, OUTPUT);
  pinMode(ledPin10, OUTPUT);
  pinMode(ledPin11, OUTPUT);
  pinMode(ledPin12, OUTPUT);
  pinMode(ledPin13, OUTPUT);

  pinMode(sensorPin1, INPUT);
  pinMode(sensorPin2, INPUT);
  pinMode(sensorPin3, INPUT);
  pinMode(sensorPin4, INPUT);
  pinMode(sensorPin5, INPUT);
}
void loop() {

  sensorStatus1 = digitalRead(A4);  
  if (sensorStatus1 == HIGH) {
    
    digitalWrite(0, LOW);
    delay(200);
    digitalWrite(1, LOW);
    delay(200);
    digitalWrite(2, LOW);
    delay(200);
    digitalWrite(3, LOW);
    delay(200);
    digitalWrite(4, LOW);
    delay(200);
    digitalWrite(5, LOW);
    delay(200);
    digitalWrite(6, LOW);
    delay(200);
    digitalWrite(7, LOW);
    delay(200);
    digitalWrite(8, LOW);
    delay(200);
    digitalWrite(9, LOW);
    delay(200);
    digitalWrite(10, LOW);
    delay(200);
    digitalWrite(11, LOW);
    delay(200);
    digitalWrite(12, LOW);
    delay(200);
    digitalWrite(13, LOW);
  }

 
    boolean pinA = digitalRead(sensorPin2);
    boolean pinB = digitalRead(ledPin13);
    boolean pinOut1;
    pinOut1 =pinA & pinB;
    digitalWrite(gateout1, pinOut1);

    sensorStatus3 = digitalRead(A3);
    if (sensorStatus3 == HIGH){
      delay(200); 
    digitalWrite(0, HIGH);
    delay(200);
    digitalWrite(1, HIGH);
    delay(200);
    digitalWrite(2, HIGH);
    delay(200);
    digitalWrite(3, HIGH);
    delay(200);
    digitalWrite(4, HIGH);
    delay(200);
    digitalWrite(5, HIGH);
    delay(200);
    digitalWrite(6, HIGH);
    delay(200);
    digitalWrite(7, HIGH);
    delay(200);
    digitalWrite(8, HIGH);
    delay(200);
    digitalWrite(9, HIGH);
    delay(200);
    digitalWrite(10, HIGH);
    delay(200);
    digitalWrite(11, HIGH);
    delay(200);
    digitalWrite(12, HIGH);
    delay(200);
    digitalWrite(13, HIGH);
  }
 

  sensorStatus2 = digitalRead(A5);  
  if (sensorStatus2 == HIGH) {
   
    delay(200);
    digitalWrite(13, LOW);
    delay(200);
    digitalWrite(12, LOW);
    delay(200);
    digitalWrite(11, LOW);
    delay(200);
    digitalWrite(10, LOW);
    delay(200);
    digitalWrite(9, LOW);
    delay(200);
    digitalWrite(8, LOW);
    delay(200);
    digitalWrite(7, LOW);
    delay(200);
    digitalWrite(6, LOW);
    delay(200);
    digitalWrite(5, LOW);
    delay(200);
    digitalWrite(4, LOW);
    delay(200);
    digitalWrite(3, LOW);
    delay(200);
    digitalWrite(2, LOW);
    delay(200);
    digitalWrite(1, LOW);
    delay(200);
    digitalWrite(0, LOW);
  }

    boolean pinC = digitalRead(sensorPin1);
    boolean pinD = digitalRead(ledPin0);
    boolean pinOut2;
    pinOut1 =pinC & pinD;
    digitalWrite(gateout2, pinOut2);

  sensorStatus4 = digitalRead(A2);
  if (sensorStatus4 == HIGH) {
   
    digitalWrite(13, HIGH);
    delay(200);
    digitalWrite(12, HIGH);
    delay(200);
    digitalWrite(11, HIGH);
    delay(200);
    digitalWrite(10, HIGH);
    delay(200);
    digitalWrite(9, HIGH);
    delay(200);
    digitalWrite(8, HIGH);
    delay(200);
    digitalWrite(7, HIGH);
    delay(200);
    digitalWrite(6, HIGH);
    delay(200);
    digitalWrite(5, HIGH);
    delay(200);
    digitalWrite(4, HIGH);
    delay(200);
    digitalWrite(3, HIGH);
    delay(200);
    digitalWrite(2, HIGH);
    delay(200);
    digitalWrite(1, HIGH);
    delay(200);
    digitalWrite(0, HIGH);
  }

    boolean pinE = digitalRead(ledPin13);
    boolean pinF = digitalRead(ledPin0);
    boolean pinOut3;
    pinOut3 =pinE & pinF;
    digitalWrite(gateout3, pinOut3);     

  sensorStatus5 = digitalRead(A1);
  if (sensorStatus5 == HIGH) {
    delay(30000);
    digitalWrite(13, HIGH);
    digitalWrite(0, HIGH);
    delay(300);
    digitalWrite(12, HIGH);
    digitalWrite(1, HIGH);
    delay(300);
    digitalWrite(11, HIGH);
    digitalWrite(2, HIGH);
    delay(300);
    digitalWrite(10, HIGH);
    digitalWrite(3, HIGH);
    delay(300);
    digitalWrite(9, HIGH);
    digitalWrite(4, HIGH);
    delay(300);
    digitalWrite(8, HIGH);
    digitalWrite(5, HIGH);
    delay(300);
    digitalWrite(7, HIGH);
    digitalWrite(6, HIGH);
  }
}

Not sure of this worries you, but the code you've produced has a high degree of redundancy and the boolean logic seems a bit contrived. It's also somewhat puzzling that you read a pin that you use as an output. None of it is wrong, per se, but it's not the most logical way of going about it. Btw, this is not to say your code will work as intended. I'm just commenting on its structure.

1 Like

First off, more of a peeve for good RAM habits --- you use ints where bytes would do the same using half as much RAM. They run faster too!

You can add pins to an arduino through daisy-chained shift registers using SPI bus or bit shifting or port expanders or led drivers.

You can multiplex pins to make 8 into 16 or 12 into 36.

You may need to add transistors to get bright leds, excepting led driver chips.

You can use WS2811 led strings (not strips, strings I have are 12mm dia RGB tree lights -- 50 bulbs over 5 meters that use 3 pins only; power, ground and data where each bulb is addressable. There is so little wiring it's kind of cheating as well as cheap and every bulb can be any of 16 million colors. It does require external power up to 3A, mine are 5V and I have OEM power supply rated to 60W for that where 3A x 5V is 15 Watts.
I use the FastLed library with an array of 150 bytes. I fill the array with red, green and blue values then use the arrayname.show() function to change the array.
You can cut the string or add more strings and use however few or many leds as you can power. You could start with red light and bring it up to make eyes adjusting easier. Do SHOP AROUND and be aware on shipping time for the cheap Ali Express price! Amazon... i am seeing $15 to $16 plus ~$6 shipping.

1 Like

This is a too much confusing description. I will not re-read it 10 times to maybe get half a clou what you really want. I will ignore the whole description.
Either you post a clear description or I will not read it.

You are re-placing actual work to learn how arrays works
with
work in the future where you have to fuzzling and fiddling around with a lot of lines of code in a repetitive manner.

1 Like

thanks for the comment, I also started to get lost in the code trying to figure it out myself.
Maybe it's time to start over

the first time I wanted to use TTL logic chips, but I don't have them at hand. My question is if it is possible to integrate the simulated logic gates in the code without interfering with the rest of the code, belive me now that I read what I wrote I don't understand either :sweat_smile:

depends on what you exactly mean with "interfere".

If you add more code this added code needs some execution time. Though your application to switch on off stairlights is a rather slow switching. So the impact of the additional code will be not recognisable.

There is a programming-tecnique called "non-blocking" which enables to do a lot of things "almost" in parallel.

Almost in parallel because of course each line of code is executed one after the other. But the time to execute a single line of code is short. So coding in a way that does
doing a small part of a code "A" quickly jump in / quickly jump out
doing a small part of a code "B" quickly jump in / quickly jump out
doing a small part of a code "C" quickly jump in / quickly jump out

then repeat with proceeding a small step of part "A"
then repeat with proceeding a small step of part "B"
then repeat with proceeding a small step of part "C"
.....
will make it look like executed in parallel.

But without a detailed description what each part of your code shall do there is no help.

This description MUST be in normal words.
Trying to use coding terms at this state will create misunderstandings.

So just describe it in normal words

1 Like

I'm trying to use some led neon strips that use 12V, the relay module board has a separate input for external power, at the moment I don't know what I'm going to use as a source, but it's not a problem at the moment.
I know about shift registers, this is what I wanted to use from the beginning, but in the meantime I received another arduino uno board and a mega 2560 and I thought that maybe I could use 2 boards in parallel or use just the mega 2560 because it has a lot of pins

I still haven't had time to test the code on the board and I keep thinking that maybe instead of running the logic gate and executing the part I want, it will just take the input it receives and run the part that is activated by that input

Use the lights you want, parts+work+hassle are my worth it or not factors. I switched to WS28xx after the prices dropped.

Instead of using logic gate chips, use bitwise logic operations on unsigned integer variables!

0b11110000 | 0b00110011 = 0b11110011 for example.

Arduino Language Reference Page -- look for Bitwise Operators

It seems that you use your thread for some personal notes that run through your mind while you are thinking about options.

If you re-read this posting. How much information does this posting offer for your potential helpers to help you writing code?

Yes. Almost no information.

There are a lot of things that you will have to specify:

  • about how many inputs are we talking?
  • what kind of logic gates are nescessary?
  • what are the logic conditions?
    • or-conditions?
    • and-conditions?
    • XOR-conditions?
    • NOT-conditions?

Why don't you post this actual code-version as complete sketch?

If you want decent help you should describe the wanted functionality in normal words and in detail.

Your hardware may have quite some parts but is still rather simply
If you build a WOKWI-simulation from your hardware all users here can see a lot of details and modify your code as suggestions

1 Like

Here is small demo-code that explains how arrays work

It is simply a slight variation of your existing numbering
instead of

  sensorPin1
  sensorPin2
  sensorPin3
  sensorPin4
  sensorPin5

you write

  sensorPin[1]
  sensorPin[2]
  sensorPin[3]
  sensorPin[4]
  sensorPin[5]

and this tiny difference makes the difference for compacting the code
through making the code counting up for you

1
2
3
4
5

by saying:

start counting at 1 and count up one by one until 5

  for (byte SensorNr = 1; SensorNr <= 5; SensorNr++) {
    pinMode(sensorPin[SensorNr], INPUT);
  }

So what the code is doing in detail is

pinMode(sensorPin[1], INPUT)
pinMode(sensorPin[2], INPUT)
pinMode(sensorPin[3], INPUT)
pinMode(sensorPin[4], INPUT)
pinMode(sensorPin[5], INPUT)

.
.

byte sensorPin[6] = {0, A4, A5, A3, A2, A1};

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");

  Serial.println("Now configuring IO-pins as inputs");

  for (byte SensorNr = 1; SensorNr <= 5; SensorNr++) {
    pinMode(sensorPin[SensorNr], INPUT);
    // Serial.println(SensorNr);
  }
  Serial.println("For-loop has finished");

  printExplanation();
}


void printExplanation() {
  Serial.println();

  Serial.println("the for-loop does:");
  Serial.println();

  for (byte SensorNr = 1; SensorNr <= 5; SensorNr++) {
    Serial.print("pinMode(sensorPin[");
    Serial.print(SensorNr);
    Serial.println("], INPUT)");
  }
}

void loop() {
}

This is the serial output of this demo-code
.
.

Setup-Start
Now configuring IO-pins as inputs
For-loop has finished

the for-loop does:

pinMode(sensorPin[1], INPUT)
pinMode(sensorPin[2], INPUT)
pinMode(sensorPin[3], INPUT)
pinMode(sensorPin[4], INPUT)
pinMode(sensorPin[5], INPUT)
1 Like

-I will have 5 imputs

-Speaking of logic gates, there will be 2 with 2 inputs each, I'm trying to figure out what conditions to use ,

-The code I uploaded is complete

  • Speaking of functionality, the code has 2 direct inputs from the sensors to start from bottom to top or top to bottom, 2 inputs from 2 logic gates to turn off the lights when the last led from top or botom is on and the sensor detects movement, this is because I don't want to do the closing sequence after a timer , The last part of the code I was thinking of moving it into a physical format with a relay and a timer maybe, this part should act as a beackup if the closing part dosen't execute