Help coding two sensors triggering LEDS

I'm just learning to code and working on a project for lightning led strips on stairs. I googled but i could not find the answer.

I have two sensors on each end of the stairs, if the first sensor goes high it triggers the leds ON , then must wait for the second sensor input, and when it goes high switchs off all leds. Then the reverse ( somebody going up and down).

Is it possible doing it without using the delay or the timer function ?

What kind of code I have to use here to accomplish the task ?

T.I.A.

Franky

No delays, no timers

climbing = false
descending = false

start of loop
  when bottom sensor goes HIGH
    if climbing == false
      climbing = true
      turn on the lights
    end if
    else if descending == true
      descending = false
      turn off the lights
    end if
    
    when top sensor goes HIGH
       if descending == false
       descending = true
      turn on the lights
    end if
    else if ascending == true
      ascending = false
      turn off the lights
    end if
end of loop
climbing = false
descending = false

UKHeli , thank you for your fast response. I need to go through the code step by step to understand its meaning. This looks like boolean TRUE/FALSE statement . Is'nt it?

Is the climbing=false / descending=false related to the PIR sensor state HIGH/LOW ? So false means HIGH here or what ?? this is confusing me :~

true and false are boolean values. Something is either true or false and can be tested. In the pseudo code that I suggested you need to know if the stairs are being ascended, descended or not being used.

If the stairs are not already being ascended (ascending == false then, if the bottom PIR detects someone (goes HIGH), then you need to turn on the lights but if they are being descended (descending == true) then we need to turn off the lights. The same for the top sensor but in reverse.

The boolean values have no relationship to the PIR values in this code. They hold the current state so that you can decide what to do when a PIR is triggered. For instance. If the bottom PIR is triggered (HIGH) do you turn on the lights because someone has passed by going up, or turn them off because someone has passed by coming down ?

A question which will complicate things. What happens if more than one person is on the stairs at the same time ? I suggest that you get the 'one at a time' code figured out first before you tackle that.

Just learning. See if I'm start moving the right way........

const int LED_PIN = 13 ; // Led is on PIN 13
const int LED_PINO = 12 ; // Led on pin 12
const int SENSORPIR_A = 3 ; // sensor PIR A is on pin 3
const int SENSORPIR_B = 4 ; // sensor PIR B is on pin 4
moveUP = false
moveDOWN = false

void setup () {

pinMODE ( 13 , OUTPUT); // set pin 13 as an OUTPUT
pinMODE ( 12 ,OUTPUT); // set pin 12 as an OUTPUT

pinMODE ( SENSORPIR_A, INPUT); // set pin 3 as INPUT A 
pinMODE ( SENSORPIR_B, INPUT); // set pin 4 as INPUT B

}

void loop () {

int A_sensor_state = digitalRead (SENSORPIR_A); //read value on pin 3
int B_sensor_state = digitalRead (SENSORPIR_B); // read value on pin 4

A_sensor_state = HIGH ;

if (moveUP == true) (moveDOWN == false) ;

digitalWrite (12, HIGH) ;
digitalWrite (13, HIGH) ;

}
else if 

B_sensor_state = HIGH

 (moveUP == true ) (moveDOWN == false) ;

digitalWrite (12, LOW ) ;
digitalWrite (13, LOW) ;

}

You have gone a bit astray.

const int LED_PIN = 13

Use the LED consts later in the pinMode() commands like you do with the sensor pin names. This makes the code easier to read and maintain.

int A_sensor_state = digitalRead (SENSORPIR_A); //read value on pin 3
int B_sensor_state = digitalRead (SENSORPIR_B); // read value on pin 4

A_sensor_state = HIGH ;

You have gone astray here. You should be testing the value of the sensor state variable, not setting it. Did you mean to have an if somewhere ?

if (moveUP == true) (moveDOWN == false) ;I am not sure what to make of this. Personally I always use braces round the code to be executed after an if even if it is only one line, like this. Note that each brace is on its own line to help readability of the code blocks and Auto Format helps too.

if (moveUP == true) 
{
  moveDOWN == false ;
}

This

else if 

B_sensor_state = HIGH

 (moveUP == true ) (moveDOWN == false) ;

digitalWrite (12, LOW ) ;
digitalWrite (13, LOW) ;

is another case in point. Which line(s) of code should be executed if the test is true. In any case you have missed the brackets around the thing(s) being tested by the if.

Have a tidy up and post your revised code.

Hi UKHeli, I gave the code a face lift.

void setup () {

const int LED_PIN = 13 ; // Led is on PIN 13
const int LED_PINO = 12 ; // Led on pin 12
const int SENSORPIR_A = 3 ; // sensor PIR A is on pin 3
const int SENSORPIR_B = 4 ; // sensor PIR B is on pin 4
moveUP = false
moveDOWN = false

pinMODE ( 13 , OUTPUT); // set pin 13 as an OUTPUT
pinMODE ( 12 ,OUTPUT); // set pin 12 as an OUTPUT

pinMODE ( SENSORPIR_A, INPUT); // set pin 3 as INPUT A 
pinMODE ( SENSORPIR_B, INPUT); // set pin 4 as INPUT B

}

void loop () {

int A_sensor_state = digitalRead (SENSORPIR_A); //read value on pin 3
int B_sensor_state = digitalRead (SENSORPIR_B); // read value on pin 4

if  (A_sensor_state = HIGH );
{

 (moveUP == true) ;
 (moveDOWN == false) ;

digitalWrite (12, HIGH) ;
digitalWrite (13, HIGH) ;

}
else if (B_sensor_state = HIGH);
{

 (moveUP == true ) ;
 (moveDOWN == false) ;

digitalWrite (12, LOW ) ;
digitalWrite (13, LOW) ;

}

I haven't looked at the logic but these problems stand out

moveUP = false
moveDOWN = false

These need a semi-colon on the end

if  (A_sensor_state = HIGH );

else if (B_sensor_state = HIGH);These don't

Confusing, isn't it !

I think the important thing for you to do is to get the code to the point where it Auto Formats and compiles even if it does not work the way you want. Then it can be debugged and fixed.

These don't

But, they do need ==, not =.

I started testing the code on the breadboard . It works like a charm only "moving UP" but not the other way around . One more question ... the END IF command is not recognized in the code , how to implement this ?

 int ledPin = 13 ; // Led is on PIN 13
 int ledPino = 12 ; // Led on pin 12
 int SENSORPIR_A = 3 ; // sensor PIR A is on pin 3
 int SENSORPIR_B = 4 ; // sensor PIR B is on pin 4
int moveUP = false ;
int moveDOWN = false ;

void setup () {

pinMode ( 13 , OUTPUT); // set pin 13 as an OUTPUT
pinMode ( 12 ,OUTPUT); // set pin 12 as an OUTPUT

pinMode ( SENSORPIR_A, INPUT); // set pin 3 as INPUT A 
pinMode ( SENSORPIR_B, INPUT); // set pin 4 as INPUT B

}

void loop () {

int A_sensor_state = digitalRead (SENSORPIR_A); //read value on pin 3
int B_sensor_state = digitalRead (SENSORPIR_B); // read value on pin 4

if (A_sensor_state == HIGH) 
{
 if (moveUP == true)  (moveDOWN == false) ; 

 digitalWrite (12, HIGH) ;
 delay (200);
 digitalWrite (13, HIGH) ;
 delay (200);
}

else if ( B_sensor_state == HIGH)
{
 if (moveUP == true) (moveDOWN == false) ; 

 digitalWrite (12, LOW ) ;
 delay (300);
 digitalWrite (13, LOW) ;
 delay (300);

}

else if (B_sensor_state ==HIGH)
{
  if (moveUP==false) (moveDOWN==true);
  
  digitalWrite (13, HIGH) ;
 delay (200);
 digitalWrite (12, HIGH) ;
 delay (200);
}

else if ( A_sensor_state == HIGH)
{
 if (moveUP == false) (moveDOWN == true) ; 

 digitalWrite (13, LOW ) ;
 delay (300);
 digitalWrite (12, LOW) ;
 delay (300);

}
}

One more question ... the END IF command is not recognized in the code , how to implement this ?

Like so:

}

C++ is not so lame that you need to tell it where the if statement ends.

I would find it easier to follow your code, and so would you I suspect, if the sensors were named in such a way as to indicate whether they were at the top or bottom of the stairs. Also, you are still not using the names of the pins instead of their numbers which makes the code harder to follow. int ledPin = 13 ;the later in the code  pinMode ( 13 , OUTPUT);and    digitalWrite (13, HIGH) ; It would also help to know why there are 2 LEDs and how they are positioned. Perhaps you could give them more meaningful names as well.

You originally asked

Is it possible doing it without using the delay or the timer function ?

but you have introduced delays into the code. Why ?

This construction    if (moveUP == true)  (moveDOWN == false) ;is unusual. Why the second set of brackets and did you mean to use == in both statements ?

    if (moveUP == true) 
  {
     moveDOWN == false ;  //should almost certainly be = not ==
  }

would be much better as the block of code to be executed if the test is true is more obvious and the layout works for single and multiple lines of code just as well.

would be much better as the block of code to be executed if the test is true is more obvious and the layout works for single and multiple lines of code just as well.

Loosing the == true part would be even better... 8)

PaulS: Loosing the == true part would be even better... 8)

Whilst that is true, I would be more in favour of that if the boolean variable were a verb, which would make it more readable so it would become, for instanceif (climbing)Sometimes it is the small things that make a difference to understanding what is going on.

This is what I accomplished till now....

http://www.youtube.com/watch?v=kgD31HBSNJQ&feature=youtu.be

As you can see the system seams working pretty well only when I fast move may hands in front of the sensors, BUT ....when I leave my hands for longer period in front of the sensors the LEDs switch on suddenly again ( see at the end of the video).

How can I correct this bug in the code or anyway to inactivate the sensors for a short time before being active again once activated ??

#define led_1 13    // 
#define led_2 12    // 
#define led_3 11   //  PINS for LEDS
#define led_4 10   //
#define led_5 9    //
#define led_6 8    //
#define led_7 7    //
#define PIR_UPPER2 2  // pin 2 for UPPER sensor
#define PIR_DOWN4 4  //  pin 3 for DOWN  sensor


int PIR_U = 0;
int PIR_D = 0;
int bUPPER = false;  
int bDOWN = false;
int time = 200;
int timeA= 200;

void LIGHTA()
{ 
  digitalWrite(led_1, HIGH);     // light LEDS
  delay (time);
  digitalWrite(led_2, HIGH);      // 
  delay (time); 
  digitalWrite(led_3, HIGH);   
  delay (time); 
  digitalWrite(led_4, HIGH);   
  delay (time); 
  digitalWrite(led_5, HIGH);   
  delay (time); 
  digitalWrite(led_6, HIGH);   
  delay (time); 
  digitalWrite(led_7, HIGH);   
  delay (time); 
}

void LIGHTB()
{ 
  digitalWrite(led_7, HIGH);     // accensione LED 1
  delay (timeA);
  digitalWrite(led_6, HIGH);      // accensione LED 2
  delay (timeA); 
  digitalWrite(led_5, HIGH);   
  delay (timeA); 
  digitalWrite(led_4, HIGH);   
  delay (timeA); 
  digitalWrite(led_3, HIGH);   
  delay (timeA); 
  digitalWrite(led_2, HIGH);   
  delay (timeA); 
  digitalWrite(led_1, HIGH);   
  delay (timeA); 
}

void OFFA ()
{
  digitalWrite(led_1, LOW);     // spegni LEDs sequenza  1 a 7
  delay (time);
  digitalWrite(led_2, LOW);     
  delay (time); 
  digitalWrite(led_3, LOW);     
  delay (time); 
  digitalWrite(led_4, LOW);     
  delay (time); 
  digitalWrite(led_5, LOW);     
  delay (time); 
  digitalWrite(led_6, LOW);     
  delay (time); 
  digitalWrite(led_7, LOW);     
  delay (time); 
}


void OFFB ()
{
  digitalWrite(led_7, LOW);     // spegni LEDs sequenza 7 a 1
  delay (timeA);
  digitalWrite(led_6, LOW);      
  delay (timeA); 
  digitalWrite(led_5, LOW);     
  delay (timeA); 
  digitalWrite(led_4, LOW);     
  delay (timeA); 
  digitalWrite(led_3, LOW);     
  delay (timeA); 
  digitalWrite(led_2, LOW);     
  delay (timeA); 
  digitalWrite(led_1, LOW);     
  delay (timeA); 
}

void setup() {
  pinMode(led_1, OUTPUT);
  pinMode(led_2, OUTPUT);
  pinMode(led_3, OUTPUT);
  pinMode(led_4, OUTPUT);
  pinMode(led_5, OUTPUT);
  pinMode(led_6, OUTPUT);
  pinMode(led_7, OUTPUT);

  pinMode(PIR_UPPER2, INPUT);
  pinMode(PIR_DOWN4, INPUT);
  
  Serial.begin(9600);
  Serial.println("start");

} 


void loop()

{ 
  PIR_U = digitalRead(PIR_UPPER2);
  PIR_D = digitalRead(PIR_DOWN4);
  delay (500);

  if (PIR_U== HIGH)  // UPPER sensor activated
  { 
    if (bDOWN==false) 
    { 
      bUPPER=true ; 
      LIGHTA (); 
      }
    else // (bDOWN==true)
    { 
      bDOWN=false; 
      OFFB ();
    }
  }

 delay(500);

  if (PIR_D == HIGH)   // DOWN sensor activated
  { 
    if (bUPPER==false) 
    { 
       bDOWN=true; 
      LIGHTB(); 
    }  
    else  // (bUPPER==false)
    { 
      bUPPER=false; 
      OFFA ();
    }
  }
  Serial.print("PIRA="); 
  Serial.print(PIR_U);
  Serial.print("  PIRB="); 
  Serial.print(PIR_D);
  Serial.print("  bUPPER="); 
  Serial.print(bUPPER);
  Serial.print("  bDOWN="); 
  Serial.print(bDOWN);
  Serial.println(""); 
  delay (500);

}

when I leave my hands for longer period in front of the sensors the LEDs switch on suddenly again

If you look back at reply #1 you will see that I suggested actions should be taken when a sensor [u]goes[/u] HIGH, ie changes from LOW to HIGH. Your current code takes actions when a sensor [u]is[/u] HIGH so if you leave your hand in place it will see the HIGH signal again and take the actions again.

What you need to do when a sensor is seen to be HIGH is to check that its previous state was LOW. To do this you need to remember the state of the sensor the last time you read it. This process is known as state change detection and there is an example of it in the IDE.

Nothing to do with this problem, but you can drastically reduce the complexity of your ON/OFF functions by using an array and passing parameters to a single function.

what happens when two people going in opposite directions use the stairs at the same time... Reminds me of that song from Fiddler on the Roof... If I were a Rich Man.... I'd have 3 staircases, one going up, one going down and one not going anywhere at all... Biddy, biddy.. Better I think to have one sensor diagonally placed so as to sense when the corridor formed by the stairs is occupied... No fancy logic involved... Obviously not tested P-Code... void setup() set the pins for sensor and led here void loop() { if (sensor ) { occupied = 1; // Delay flag, Stairs are in use led =1 ; // so turn on the light's } if (occupied and ! sensor) // If the stairs were in the past period (5 seconds) occupied wait 5 seconds; // blink W/O delay for the next person up or down occupied = 0; // reset the delay flag: led = 0; // turn off the Led

Not nearly complete but a fair outline of a simpler method?

Doc

UKHeliBob:

when I leave my hands for longer period in front of the sensors the LEDs switch on suddenly again

If you look back at reply #1 you will see that I suggested actions should be taken when a sensor [u]goes[/u] HIGH, ie changes from LOW to HIGH. Your current code takes actions when a sensor [u]is[/u] HIGH so if you leave your hand in place it will see the HIGH signal again and take the actions again.

How to write the code for this statement ......( when sensor goes from LOW to HIGH ), just learning here.

What you need to do when a sensor is seen to be HIGH is to check that its previous state was LOW. To do this you need to remember the state of the sensor the last time you read it. This process is known as state change detection and there is an example of it in the IDE.

This one is new for me. I didn't know about this function , I'll search for it.

Docedison:
what happens when two people going in opposite directions use the stairs at the same time…
Reminds me of that song from Fiddler on the Roof… If I were a Rich Man… I’d have 3 staircases, one going up, one going down and one not going anywhere at all… Biddy, biddy… Better I think to have one sensor diagonally placed so as to sense when the corridor formed by the stairs is occupied… No fancy logic involved…
Obviously not tested P-Code…
void setup()
set the pins for sensor and led here
void loop()
{
if (sensor )
{
occupied = 1; // Delay flag, Stairs are in use
led =1 ; // so turn on the light’s
}
if (occupied and ! sensor) // If the stairs were in the past period (5 seconds) occupied
wait 5 seconds; // blink W/O delay for the next person up or down
occupied = 0; // reset the delay flag:
led = 0; // turn off the Led

Not nearly complete but a fair outline of a simpler method?

Doc

Hi Doce ,
I know this could happen , but I started to KISS ( KEEP IT Super Simple) here, then I’ll improve system STEP by STEP.
Anyway thank you so much for your suggestion.