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```