Stairs Project Code Issue

Hey All,
New to posting here in the community.
I originally got an Arduino Uno years ago, to do a stairs lighting project, but ended up doing many others before actually getting around to doing the stairs project.
I'm finally doing it and ran into an issue with the coding and wanted to see if someone could give some pointers.
I'm doing the typical stairs lighting project where there's a sensor at the bottom and top of the stairs that triggers LED strips (solid white, not RGB or anything fancy) and illuminates step by step (up or down) depending on which sensor is triggered. I'm doing my programming in stages, to make sure things work as I add functions, so right now I'm doing the step by step illumination, then shut all off at once after 10sec.
I'm using HC-SR501 PIR sensors (at the moment) and the issue I have is... When the sensor is triggered, the strips light one by one in the correct direction, stay on for 10sec, then go off, but it then runs the opposite direction once.
Each sensor triggers the correct direction, but then does the one cycle in the opposite direction . I'm sure there's some sort of "stop" command that I need, but all of my past projects were just continuous run loops for special stage lighting props, I never had a "stop", therefore I'm looking for some assistance on this one and any help would be appreciated.
The forums won't let me post an attachment, so I'm posting the code below - sorry so long!!

/*******************************************
TITLE: Stairway Lighting - Chase
*********************************************/
int sensorPin1 = A4; //Bottom - Up sensor
int sensorPin2 = A5; //Top - Down sensor
int sensorStatus1 = 0;
int sensorStatus2 = 0;
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(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);
}
void loop() {

sensorStatus1 = digitalRead(A4); //Stairs Bottom Sensor - Motion Up
if (sensorStatus1 == HIGH || sensorStatus2 == HIGH) {
// if bottom sensor is triggered lights travel upward
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(10000);// delay time 10 sec

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(2, LOW);

}

sensorStatus2 = digitalRead(A5); //Stairs Top Sensor - Motion Down
if (sensorStatus2 == HIGH || sensorStatus1 == HIGH) {
// if top sensor is triggered lights travel downward
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);

delay(10000);// delay time 10 sec

digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(2, LOW);

}
}

Take a look at this topic. The how to post code and the use of schematics and other documentation. Novells are never good.

Hello mtldrummer and welcome to the worldbest Arduino Forum.

Check and try these mods:

/*******************************************
  TITLE: Stairway Lighting - Chase
*********************************************/
int sensorPin1 = A4; //Bottom - Up sensor
int sensorPin2 = A5; //Top - Down sensor
int sensorStatus1 = 0;
int sensorStatus2 = 0;
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(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);
}
void loop() {

  sensorStatus1 = digitalRead(A4); //Stairs Bottom Sensor - Motion Up
  if (sensorStatus1 == HIGH) {
    // if bottom sensor is triggered lights travel upward
    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(10000);// delay time 10 sec

    digitalWrite(13, LOW);
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
    digitalWrite(4, LOW);
    digitalWrite(3, LOW);
    digitalWrite(2, LOW);

  }

  sensorStatus2 = digitalRead(A5); //Stairs Top Sensor - Motion Down
  if (sensorStatus2 == HIGH ) {
    // if top sensor is triggered lights travel downward
    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);

    delay(10000);// delay time 10 sec

    digitalWrite(13, LOW);
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(10, LOW);
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    digitalWrite(7, LOW);
    digitalWrite(6, LOW);
    digitalWrite(5, LOW);
    digitalWrite(4, LOW);
    digitalWrite(3, LOW);
    digitalWrite(2, LOW);

  }
}

The sketch has a lot of room for improvement with the use of arrays etc.

Have a nice day and enjoy coding in C++.

sensorStatus1 = digitalRead(A4); //Stairs Bottom Sensor - Motion Up
  if (sensorStatus1 == HIGH || sensorStatus2 == HIGH) {

What's the sensor state when blocked/unblocked?

That worked!
I tried removing the 2nd command line in each, thinking that was it, but I see what I did wrong now too.
Thanks much for the help!
I'm kind of old school and like to see each line and how it would flow, but still no way an expert. If I can't figure something out, I find one online that works basically how I like and alter as I need. Guess I'm going to have to break down and look into C++

Not sure where you're going with your reply, but I know how to post code and read schematics.
I was not allowed to post attachments because I have never posted in the forums previously.
And if you're referring to my long explanation as the "novel". It explains everything, which is needed.
Not being ignorant on my reply, but maybe I'm reading your reply wrong

I tried to watch that in the serial monitor, but it doesn't display correctly, so I can't tell you what it was doing, but PaulPaulSon was able to help out..

I fully understand You! A link to an advice topic was missing! My fault!
The intended link is : How to get the best out of this forum - Using Arduino / Project Guidance - Arduino Forum

Know that all helpers are ignorant, newbies, to Your project. The wanted documentation puts meat on the bones so to say.
Maybe the advice You got from other members pulls the plug for You.

1 Like

Hello mtldrummer

Many thanks for your feedback.

Below you will find a modified sketch using a.m. arrays for upLedPins and downLedPins. The sketch is not tested.

Check it, try it:

/*******************************************
  TITLE: Stairway Lighting - Chase
  https://forum.arduino.cc/t/stairs-project-code-issue/1092407
  Stairs Project Code Issue
*********************************************/
constexpr int UpSensorPin = A4; //Bottom - Up sensor
constexpr int DownSensorPin = A5; //Top - Down sensor
int sensorStatus1 = 0;
int sensorStatus2 = 0;
constexpr int upLedPins   [] {13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2};
constexpr int downLedPins [] {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

void setup()
{
  pinMode(UpSensorPin, INPUT);
  pinMode(DownSensorPin, INPUT);
  for (auto upLedPin : upLedPins) pinMode(upLedPin, OUTPUT);
}

void loop()
{
  sensorStatus1 = digitalRead(UpSensorPin); //Stairs Bottom Sensor - Motion Up
  if (sensorStatus1 == HIGH)
  {
    // if bottom sensor is triggered lights travel upward
    for (auto upLedPin : upLedPins) digitalWrite(upLedPin, HIGH), delay(200);
    delay(10000);// delay time 10 sec
    for (auto upLedPin : upLedPins) digitalWrite(upLedPin, LOW);
  }

  sensorStatus2 = digitalRead(DownSensorPin); //Stairs Top Sensor - Motion Down
  if (sensorStatus2 == HIGH )
  {
    // if top sensor is triggered lights travel downward
    for (auto downLedPin : downLedPins) digitalWrite(downLedPin, HIGH), delay(200);
    delay(10000);// delay time 10 sec
    for (auto downLedPin : downLedPins) digitalWrite(downLedPin, LOW);
  }
}

Have a nice day and enjoy coding in C++.

Cool, I'll give it a try.
Thanks again

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.