Help me code this funtion

Hi...i made a project on staircase automatic lights with ir sensors
as the person climbs from 1st step sensor 1 triggers lights and as he moves to last step sensor 2 triggers and turns off the lights.
the point where i need help me, if the person walks and triggers the sensor 1 but dosnt go all way up rather gets back..soo he dosnt trigger sensor 2 which makes lights to glow forever
so i want to set a timer like..if sensor 1 is triggers and sensor 2 is not triggers for the specific time , turn off all lights(relays).
i tried millis but fell more complicated for me. can anyone help me get this done?
my code

#include <EEPROM.h>   //We need this library
#include <Arduino.h>  // for type definitions

int timex=1000;
//int potpin = A3;
#define button 8              //Push button on D8

int step1=2;
int step2=3;
int step3=4;

int lowerin=0;
int upperin=0;
//int lowerout=0;
//int upperout=0;

int lowerstep=14;
int upperstep=19;

int state = 0;                //integer to hold current state
int old = 0;                  //integer to hold last state
int buttonpoll = 0;           //integer to hold button state

void toupon(){

  digitalWrite(step1,HIGH);delay(timex);Serial.print(" step 1 on ");
  digitalWrite(step2,HIGH);delay(timex);Serial.print(" step 2 on ");
  digitalWrite(step3,HIGH);delay(timex);Serial.print(" step 3 on ");
  
}

void toupoff(){

  digitalWrite(step1,LOW);delay(timex);Serial.print(" step 1 off ");
  digitalWrite(step2,LOW);delay(timex);Serial.print(" step 2 off ");
  digitalWrite(step3,LOW);delay(timex);Serial.print(" step 3 off ");
  
}

void todownon(){

  digitalWrite(step3,HIGH);delay(timex);Serial.print(" step 3 on ");
  digitalWrite(step2,HIGH);delay(timex);Serial.print(" step 2 on ");
  digitalWrite(step1,HIGH);delay(timex);Serial.print(" step 1 on ");
}

void todownoff(){

  digitalWrite(step3,LOW);delay(timex);Serial.print(" step 3 off ");
  digitalWrite(step2,LOW);delay(timex);Serial.print(" step 2 off ");
  digitalWrite(step1,LOW);delay(timex);Serial.print(" step 1 off ");
}

void setup() {

  Serial.begin(9600);
  Serial.println(" Staircase System Ready ");
    pinMode(lowerstep,INPUT);
    pinMode(upperstep,INPUT);
    digitalWrite(lowerstep,HIGH);
    digitalWrite(upperstep,HIGH);
  
    pinMode(step1,OUTPUT);
    pinMode(step2,OUTPUT);
    pinMode(step3,OUTPUT);
    pinMode(button, INPUT);  
    
    state = EEPROM.read(0);
}

void loop() {
  
  buttonRead();
  switchDecision();
  dataWriteLongTerm();
}



void buttonRead() 
 {//debouncing routine to read button
  buttonpoll = digitalRead(button);      //poll the state of button
  if (buttonpoll == 1) {                 //check if it has been pressed
    delay(50);                           //wait 50ms
    buttonpoll = digitalRead(button);    //poll button again
    if (buttonpoll == 0) {               //if it is 0 considered one press
      state = old + 1;    
    }
  }
   else {                                //if button has not been pressed
    delay(100);                         // wait 100ms
   }
 }

void switchDecision() {
  int lower=digitalRead(lowerstep);
  int upper=digitalRead(upperstep);

   switch(state){
    
    case 1:
      digitalWrite(step1,HIGH);
      digitalWrite(step2,HIGH);
      digitalWrite(step3,LOW);{Serial.println(" 1 AND 2 steps ON ");}
      old = state;
      break;
      
      
     case 2:
      digitalWrite(step1,HIGH);
      digitalWrite(step2,HIGH);
      digitalWrite(step3,HIGH);{Serial.println(" All steps ON ");}
      old = state;
      break;

     case 3:
      digitalWrite(step1,HIGH);
      digitalWrite(step2,LOW);
      digitalWrite(step3,HIGH);{Serial.println(" 1 and 3 steps ON ");}
      old = state;
      break;

     case 4:
      
      digitalWrite(step1,LOW);
      digitalWrite(step2,LOW);
      digitalWrite(step3,LOW);{Serial.println(" All steps OFF ");}
      old = state;
      break;

      default:
      {Serial.print( " SENSORS ACTIVATED ");}
      if ( lower==LOW && upperin==0 ){Serial.println(" Moving up "); toupon();lowerin=1; }         //turns ON light from 1st step to last
      if ( lower==LOW && upperin==1 ){Serial.println(" closing down "); todownoff();upperin=0;}    //turns OFF light from last step to 1st
      if ( upper==LOW && lowerin==0 ){Serial.println(" Moving down "); todownon();upperin=1 ;}     //turns ON light from last step to 1st
      if ( upper==LOW && lowerin==1 ){Serial.println(" closing up "); toupoff();lowerin=0;}        //turns OFF light from 1st step to last
      old = 0;
      break;
   }
 }   
void dataWriteLongTerm() {
  EEPROM.write(0, state);
  
}

this is the part which requires changes i guess

      if ( lower==LOW && upperin==0 ){Serial.println(" Moving up "); toupon();lowerin=1; }         //turns ON light from 1st step to last
      if ( lower==LOW && upperin==1 ){Serial.println(" closing down "); todownoff();upperin=0;}    //turns OFF light from last step to 1st
      if ( upper==LOW && lowerin==0 ){Serial.println(" Moving down "); todownon();upperin=1 ;}     //turns ON light from last step to 1st
      if ( upper==LOW && lowerin==1 ){Serial.println(" closing up "); toupoff();lowerin=0;}        //turns OFF light from 1st step to last```

Duplicate, awaiting merge

Why a timer? Why don't you just have trigger one and trigger two both toggling a single boolean for the lights?

That way if a person goes up the lights trigger and if they get half way and come back down passed sensor 1 again they turn back off

this is different....i added soo many stuffs here

this sounds cool but dosnt work in practical use as...one person goes up and other comes back of him end up trigger off lights...soo timer is a better alternative

It looks to me like you took a lot of stuff away, but "toupon" and "toupoff" still look pretty similar to me.

1 Like

You have the same issue in reverse already though. Say one person starts going up the stairs (triggering the lights on), immediately after someone comes down the stairs and triggers sensor 2 plunging them both into darkness.

Unless you just let any time either trigger is set off to keep the lights on for a further 1min (or however long seems acceptable to scale the stairs) then turn off again

uh its the same project and almost i finished it..execpt for that one funtion, can you help me code that please

this happens very rare in my
Say one person starts going up the stairs (triggering the lights on), immediately after someone comes down the stairs and triggers sensor 2 plunging them both into darkness.

but your idea makes sense to me. it would work great if i use 4 ir sensors
and set up people count.but as im a beginner ..it feels like fantasy to me

LOL.

Sry, it really is quite amusing thinking of all the ways this can go wrong.

a7

uh yeah ..best one would be adding 4 ir sensors and making people count

The best idea is probably just to install a switch at the top and bottom of the staircase and ask the residents to kindly turn the light back off again if they are the last person on the staircase and are no longer using it?

I've been in public restrooms before with motion sensor lights and they turn off mid-poop if you take too long. Trying to clean up is fun!

lol then whats the point of making all this dude?

thats pir

hi i want to merge this codes. one is ir automatic stiarcase light which uses relays that are directly connected to arduino nano digital pins. but i want to use shift register to connect those relays.
Can anyone help me fix this ?

STAIRCASE IR SENSOR CODE:

int timex=1000;
int potpin = A3;

int step1=2;
int step2=3;
int step3=4;
int step4=5;
int step5=6;
int step6=7;
int step7=8;
int step8=9;

int lowerin=0;
int upperin=0;
int lowerout=0;
int upperout=0;

int lowerstep=14;
int upperstep=19;

void toupon(){

 digitalWrite(step1,LOW);delay(timex);Serial.print(" step 1 on");
 digitalWrite(step2,LOW);delay(timex);Serial.print(" step 2 on");
 digitalWrite(step3,LOW);delay(timex);Serial.print(" step 3 on"); 
 digitalWrite(step4,LOW);delay(timex);Serial.print(" step 4 on");
 digitalWrite(step5,LOW);delay(timex);Serial.print(" step 5 on");
 digitalWrite(step6,LOW);delay(timex);Serial.print(" step 6 on"); 
 digitalWrite(step7,LOW);delay(timex);Serial.print(" step 7 on");
 digitalWrite(step8,LOW);delay(timex);Serial.print(" step 8 on");
 }

void toupoff(){

 digitalWrite(step1,HIGH);delay(timex);Serial.print(" step 1 OFF");
 digitalWrite(step2,HIGH);delay(timex);Serial.print(" step 2 OFF");
 digitalWrite(step3,HIGH);delay(timex);Serial.print(" step 3 OFF");
 digitalWrite(step4,HIGH);delay(timex);Serial.print(" step 4 OFF");
 digitalWrite(step5,HIGH);delay(timex);Serial.print(" step 5 OFF");
 digitalWrite(step6,HIGH);delay(timex);Serial.print(" step 6 OFF");
 digitalWrite(step7,HIGH);delay(timex);Serial.print(" step 7 OFF");
 digitalWrite(step8,HIGH);delay(timex);Serial.print(" step 8 OFF");
}
void todownon(){

 digitalWrite(step8,LOW);delay(timex);Serial.print(" step 8 on");
 digitalWrite(step7,LOW);delay(timex);Serial.print(" step 7 on");
 digitalWrite(step6,LOW);delay(timex);Serial.print(" step 6 on");
 digitalWrite(step5,LOW);delay(timex);Serial.print(" step 5 on");
 digitalWrite(step4,LOW);delay(timex);Serial.print(" step 4 on");
 digitalWrite(step3,LOW);delay(timex);Serial.print(" step 3 on");
 digitalWrite(step2,LOW);delay(timex);Serial.print(" step 2 on");
 digitalWrite(step1,LOW);delay(timex);Serial.print(" step 1 on");
  }

void todownoff(){

 digitalWrite(step8,HIGH);delay(timex);Serial.print(" step 8 OFF");
 digitalWrite(step7,HIGH);delay(timex);Serial.print(" step 7 OFF");
 digitalWrite(step6,HIGH);delay(timex);Serial.print(" step 6 OFF");
 digitalWrite(step5,HIGH);delay(timex);Serial.print(" step 5 OFF");
 digitalWrite(step4,HIGH);delay(timex);Serial.print(" step 4 OFF");
 digitalWrite(step3,HIGH);delay(timex);Serial.print(" step 3 OFF");
 digitalWrite(step2,HIGH);delay(timex);Serial.print(" step 2 OFF");
 digitalWrite(step1,HIGH);delay(timex);Serial.println(" step 1 OFF");}

void setup() {
Serial.begin(9600);
Serial.println("StairCase System Ready");
 pinMode(lowerstep,INPUT);
 pinMode(upperstep,INPUT);
 digitalWrite(lowerstep,HIGH);
 digitalWrite(upperstep,HIGH);

 pinMode(step1,OUTPUT);
 pinMode(step2,OUTPUT);
 pinMode(step3,OUTPUT);
 pinMode(step4,OUTPUT);
 pinMode(step5,OUTPUT);
 pinMode(step6,OUTPUT);
 pinMode(step7,OUTPUT);
 pinMode(step8,OUTPUT);  
}

void loop() {

Serial.println(timex);
int lower=digitalRead(lowerstep);
int upper=digitalRead(upperstep);

if (lower==LOW && upperin==0 ){Serial.println(" Moving up "); toupon(); lowerin;}
if (lower==LOW && upperin==1 ){Serial.println(" closing down"); todownoff(); upperin;}
if (lower==LOW && upperin==0 ){Serial.println(" Moving down ");todownon(); upperout;}
if (lower==LOW && upperin==1 ){Serial.println(" closing up "); toupoff();lowerout;}
}

SHIFT REGISTER IC CODE

int DS1_pin = 16; // data pin A2
int STCP1_pin =15; // Latch A1
int SHCP1_pin = 14 ; // Clock A0

int digits [17][16]{
  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//0
  {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//1
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//2
  {0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},//3
  {0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},//4
  {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},//5
  {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},//6
  {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},//7
  {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},//8
  {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0},//9
  {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},//10
  {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},//11
  {0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0},//12
  {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0},//13
  {0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0},//14
  {1,0,1,0,1,1,1,1,1,0,1,0,1,0,0,0},//15
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}// 16

  /*  {0,1,1,1,1,1,1,0},  // digit 0
  {0,0,1,1,0,0,0,0},  // digit 1
  {0,1,1,0,1,1,0,1},  // digit 2
  {0,1,1,1,1,0,0,1},  // digit 3
  {0,0,1,1,0,0,1,1},  // digit 4
  {0,1,0,1,1,0,1,1},  // digit 5
  {0,1,0,1,1,1,1,1},  // digit 6
  {0,1,1,1,0,0,0,0},  // digit 7
  {0,1,1,1,1,1,1,1},  // digit 8
  {0,1,1,1,1,0,1,1},  // digit 9*/
};


void display_digit(int d) {
  if (digits[d][0]==1){} //A
  if (digits[d][1]==1){} //B
  if (digits[d][2]==1){} //C
  if (digits[d][3]==1){} //D
  if (digits[d][4]==1){} //E
  if (digits[d][5]==1){} //F
  if (digits[d][6]==1){} //G
}

void DisplayDigit(int Digit)
{
    digitalWrite(STCP1_pin,LOW);
    for (int i = 16; i>=0; i--)
    {
      digitalWrite(STCP1_pin,LOW);
      if (digits[Digit][i]==1) digitalWrite(DS1_pin, HIGH);
      if (digits[Digit][i]==0) digitalWrite(DS1_pin, LOW);
      digitalWrite(SHCP1_pin,HIGH);
    }
      digitalWrite(STCP1_pin,HIGH);
}


void setup() {
  pinMode(DS1_pin, OUTPUT);
  pinMode(STCP1_pin, OUTPUT);
  pinMode(SHCP1_pin, OUTPUT);
}

void loop() {

  DisplayDigit (0);
  
}


Hello
Take a view here.

1 Like

You'll want to read this too.

1 Like

@boopesh_boo

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

1 Like

Stop creating duplicate posts or you will be banned for a period

Your THIRD post on this subject has been deleted

Post your full code here using code tags when you do

See How to get the best out of this forum