Stairs lightning LED off issue

Hello,

I am new to the Arduino programming and i hope someone can help me with the stairs lightning code i have.

All the hardware is installed and the code works fine, but i want to tweak the code so that if one of the sensors is triggered, but no one walks up or down the stairs the leds go out after 10 seconds.

If someone can help me with this i would be very gratefull.

Thanks in advance

Best regards, Amido

///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
///////////// User settings, U may edit!! /////////////
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////



#define SpeedOn 25  //Time speed leds turning on in mili seconds
#define SpeedOff 50  //Time speed leds turning off in mili seconds
#define AmoundLed 14 //Amound of leds you use, max 16




///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
///////////// Start program do not edit!! /////////////
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////




#define sensorUpPin 2  //Sensor upstairs
#define sensorDownPin 3  //Sensor downstairs
#define ledDown 8  //Sensor downstairs indication ( not required )not used
#define ledUp 9  //Sensor upstairs indication ( not required )not used
#include <Wire.h>
#include <PCA9685.h>

boolean downSensorSet = false;
boolean upSensorSet = false;
int sensorUpVal = 0;
int sensorDownVal = 0;

PCA9685 ledDriver;


void setup() {

 pinMode(sensorUpPin, INPUT); //sonar upstairs
 pinMode(sensorDownPin, INPUT);//sonar downstairs
 pinMode(ledDown, OUTPUT);
 pinMode(ledUp, OUTPUT);
 
 Wire.begin();           // Wire must be started!
 ledDriver.begin(B101000);  // Address pins A5-A0 set to B111000
 ledDriver.init();

}

void loop() {
  
 sensorUpVal = digitalRead(sensorUpPin);
   if (sensorUpVal == HIGH) { 
   digitalWrite(ledUp,HIGH);
 
   //driveLedsInvertedOff();
   if(downSensorSet)    {
     driveLedsInvertedOff();
     downSensorSet = false;}
     
   else if(!upSensorSet && !downSensorSet){
     upSensorSet = true;
     driveLedsOn();}      
 }
 else {
   digitalWrite(ledUp,LOW);
   delay(50);
 }


 sensorDownVal = digitalRead(sensorDownPin);
   if (sensorDownVal == HIGH){
   digitalWrite(ledDown,HIGH);
  
   //driveLedsInvertedOn();
   if(upSensorSet)   {
     driveLedsOff();
     upSensorSet = false;}
     
   else if(!downSensorSet && !upSensorSet){
     downSensorSet = true;
     driveLedsInvertedOn();}    
  }
 else {
   digitalWrite(ledDown,LOW);
   delay(50);
 }
}



void driveLedsOn()
{
   for(int i=0; i<AmoundLed; i++)  {
 for(int level=10;level>=0;level--)  {
     ledDriver.setLEDDimmed(i,level*20);
     delay(SpeedOn);     
   }
 }
}
 
void driveLedsOff()
{

 for(int i=0; i<AmoundLed; i++)  {
 for(int level=0;level<=10;level++)  {    
     ledDriver.setLEDDimmed(i,level*20);
     delay(SpeedOff);     
   }
 }
}

void driveLedsInvertedOn()
{
   for(int i=AmoundLed; i>=0; i--)  {
 for(int level=10;level>=0;level--)  {
     ledDriver.setLEDDimmed(i,level*20);
     delay(SpeedOn);     
   }
 }
}

void driveLedsInvertedOff()
{
for(int i=AmoundLed; i>=0; i--)  {
 for(int level=0;level<=10;level++)  {    
     ledDriver.setLEDDimmed(i,level*20);
     delay(SpeedOff);     
   }
 }
}

Please follw the quidelines how to post code...

    for(int i=0; i<AmoundLed; i++)
for(int i=AmoundLed; i>=0; i--)

Can you see a problem with the second for loop there?

No sorry, I can see they are different, but which one is right?
And is this the cause of the leds not turning off?

Thanks in advance

@Amido
take a pencil and paper and write down the values i can take in both loops ...

Thanks for the advice, but i still cannot see the problem in the code.

You can't see that, for example, if you've got ten LEDs, addressing the eleventh LED might cause problems?

I am really a newbie at this, so you might think that i am stupid not seeing the problem.
I wrote the values down, but i still cannot see where the problem is.

Sorry for that.

Re-read reply #6

I wrote the values down, but i still cannot see where the problem is.

Show us your work.

What work do you mean?

The work where you wrote the numbers down.

void driveLedsOn()
{
    for(int i=0; 0<14; i++)  {
  for(int level=10;level>=0;level--)  {
      ledDriver.setLEDDimmed(i,level*20);
      delay(SpeedOn);     
    }
  }
}
  
void driveLedsOff()
{

  for(int i=0; 0<14; i++)  {
  for(int level=0;level<=10;level++)  {    
      ledDriver.setLEDDimmed(i,level*20);
      delay(SpeedOff);     
    }
  }
}

void driveLedsInvertedOn()
{
    for(int i=14; 14>=0; i--)  {
  for(int level=10;level>=0;level--)  {
      ledDriver.setLEDDimmed(i,level*20);
      delay(SpeedOn);     
    }
  }
}

void driveLedsInvertedOff()
{
 for(int i=14; 14>=0; i--)  {
  for(int level=0;level<=10;level++)  {    
      ledDriver.setLEDDimmed(i,level*20);
      delay(SpeedOff);     
    }
  }
}

Amido:
What work do you mean?

I’ll help. AmoundLed=14.

for(int i=0;i<AmoundLed; i++)
0 1 2 3 4 5 6 7 8 9 10 11 12 13
0 to 13 = 14 elements

for(int i=AmoundLed; i>=0; i–)
14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
14 to 0 = 15 elements

for(int i=14; 14>=0; i--)  {In my limited experience, 14 is always greater than 0

Amido:

    for(int i=0; 0<14; i++)  {

for(int i=0; 0<14; i++)  {

for(int i=14; 14>=0; i–)  {

for(int i=14; 14>=0; i–)  {

Never tried it, but it looks like endless for loops.

Thanks for the help, but will changing the amountled make the leds turn off after a while?

Thank for the effort.

No, for that you need to keep track of last sensor detection, and after a specified time has passed call your off function.

unsigned long sensorTime;
unsigned int waitTime=10000;
.
.
.
sensorTime=millis();
.
.
.
if (millis()-sensorTime>waitTime)
 {
 turn off light
 }
[code]

Thanks a lot for the effort. Is there anyone who can modify the code so that the leds go off if no motion is detected?
It has taken me a long time to setup what i have now, but i am stuck now.

It would be appreciated if someone would help me out.

Thank you.

Amido:
Thanks a lot for the effort. Is there anyone who can modify the code so that the leds go off if no motion is detected?

Yes, you :wink: We are a forum to help you learn, not to edit code you happen to find or glue together :wink: