Converting a button to pir sensor epic fail

Guys i need help desperetly...

I have a project about motion activated led stair lights and i have a working 4 real program 4 arduino mega but... This program starts and stop with a button (Vgnd). So i need this program convert to prallax or whatever pir motion sensor program cause i couldnt do it. :slight_smile: :slight_smile: :slight_smile: Thx 4 helpers forum 4ever...

stairs_led.ino (9.58 KB)

Please post your sketch in code tags (use the </> button) - Right now to view attachments, we have to download them, and many of us read forum on mobile devices.

Secondly, please explain what the sketch is supposed to do, what it actually does, and what external devices are connected to it.

Have you ever used the for() function? It would greatly simplify your program.

for(byte x = 2; x < 19; x++}
{
   digitalWrite(x, HIGH);
   delay(tOn);
}

etc.

for(byte x = 18; x >1; x--}
{
   digitalWrite(x, HIGH);
   delay(tOn);
}

etc.

Also using the delay() function should be avoided, you may want to review the Blink Without Delay sketch for your future programming.

   //LED pin assignments
  int led1 = 2;
  int led2 = 3;
  int led3 = 4;
  int led4 = 5;
  int led5 = 6;
  int led6 = 7;
  int led7 = 8;
  int led8 = 9;
  int led9 = 10;
  int led10 = 11;
  int led11 = 12;
  int led12 = 13;
  int led13 = 14;
  int led14 = 15;
  int led15 = 16;
  int led16 = 17;
  int led17 = 18;
 
  // button pin assignments
  int b_up = 19; //button at bottom of stairs
  int b_down = 20; //button at top of stairs
 
  //LED delay settings
  int tOn = 400; //LED On delay
  int tOff = 750; //LED Off delay
  int del = 0; //Delay after action (used for debugging - set to 500-1000 to introduce a delay between each action)
 
  //variables
  static unsigned activeup = 0; //whether the system is active going upwards
  static unsigned activedown = 0; //whether the system is active going downwards
 
void setup() { //make with the setups
 
  //Set Serial
  Serial.begin(9600);
  Serial.println("");
  Serial.println("Activated");
  delay(del);
 
  //Set LED pins as output
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  pinMode(led9, OUTPUT);
  pinMode(led10, OUTPUT);
  pinMode(led11, OUTPUT);
  pinMode(led12, OUTPUT);
  pinMode(led13, OUTPUT);
  pinMode(led14, OUTPUT);
  pinMode(led15, OUTPUT);
  pinMode(led16, OUTPUT);
  pinMode(led17, OUTPUT);
 
  //Set button pins as input
  pinMode(b_up, INPUT_PULLUP);
  pinMode(b_down, INPUT_PULLUP);
 
  Serial.println("Pins set");
  delay(del);
 
  //Clear variables
  int activedown = 0;
  int activeup = 0;
 
  //Print variables to Serial
  Serial.print("active up: ");
  Serial.println(activeup);
  Serial.print("active down: ");
  Serial.println(activedown);
  delay(del);
 
  Serial.println("Variables set");
  delay(del);
 
  Serial.println("");
  Serial.println("Ready");
}
 
 
void loop() {  //forever in your loopings
     
      //Set variables based on pin state
      int up = digitalRead(b_up);
      int down = digitalRead(b_down);
   
      Serial.print("active up: ");
      Serial.println(activeup);
      Serial.print("active down: ");
      Serial.println(activedown);
      delay(del);
   
  if (up == LOW){ //Bottom button pressed
   
      Serial.println("Bottom button pressed!");
      delay(del);
     
      //Set variables
      Serial.print("active up: ");
      Serial.println(activeup);
      Serial.print("active down: ");
      Serial.println(activedown);
      delay(del);
     
          if (activeup == 1 || activedown == 1){ //checks if system active
               
                //If active
                Serial.println("System active!");
                delay(del);
               
                OffDown(); //function call
                       
          } else {
           
            //if not active            
            Serial.println("System activated (up)");
            delay(del);
     
            OnUp(); //function call
           
          }
     
     
  }
     
  if (down == LOW){
     
      Serial.println("Top button pressed");
      delay(del);
     
      Serial.print("active up: ");
      Serial.println(activeup);
      Serial.print("active down: ");
      Serial.println(activedown);
      delay(del);
     
      if (activeup == 1 || activedown == 1){
           
                Serial.println("System active!");
                delay(del);
               
                OffUp(); //function call
                       
          } else  {
       
            Serial.println("System activated (down)");
            delay(del);
     
            OnDown(); //function call
        
}
 
void OnUp(){ //Switches lights on going up
 
  Serial.println("Switching on lights (up)");
               delay(del);
               
               //set LEDs on in turn
               digitalWrite(led1, HIGH);
               delay(tOn);
               digitalWrite(led2, HIGH);
               delay(tOn);
               digitalWrite(led3, HIGH);
               delay(tOn);
               digitalWrite(led4, HIGH);
               delay(tOn);
               digitalWrite(led5, HIGH);
               delay(tOn);
               digitalWrite(led6, HIGH);
               delay(tOn);
               digitalWrite(led7, HIGH);
               delay(tOn);
               digitalWrite(led8, HIGH);
               delay(tOn);
               digitalWrite(led9, HIGH);
               delay(tOn);
               digitalWrite(led10, HIGH);
               delay(tOn);
               digitalWrite(led11, HIGH);
               delay(tOn);
               digitalWrite(led12, HIGH);
               delay(tOn);
               digitalWrite(led13, HIGH);
               delay(tOn);
               digitalWrite(led14, HIGH);
               delay(tOn);
               digitalWrite(led15, HIGH);
               delay(tOn);
               digitalWrite(led16, HIGH);
               delay(tOn);
               digitalWrite(led17, HIGH);
               
               activeup = 1; //Set system as active up
               
               Serial.print("active up: ");
               Serial.println(activeup);
               Serial.print("active down: ");
               Serial.println(activedown);
               delay(del);
               
               Serial.println("System now active (up)");
               delay(del);
} //end OnUp
 
void OffUp(){ //switches lights off going up
 
         Serial.println("Switching lights off (Up)");
         delay(del);
           
         
         //sets LEDs off in turn
         digitalWrite(led1, LOW);
         delay(tOff);
         digitalWrite(led2, LOW);
         delay(tOff);
         digitalWrite(led3, LOW);
         delay(tOff);
         digitalWrite(led4, LOW);
         delay(tOff);
         digitalWrite(led5, LOW);
         delay(tOff);
         digitalWrite(led6, LOW);
         delay(tOff);
         digitalWrite(led7, LOW);
         delay(tOff);
         digitalWrite(led8, LOW);
         delay(tOff);
         digitalWrite(led9, LOW);
         delay(tOff);
         digitalWrite(led10, LOW);
         delay(tOff);
         digitalWrite(led11, LOW);
         delay(tOff);
         digitalWrite(led12, LOW);
         delay(tOff);
         digitalWrite(led13, LOW);
         delay(tOff);
         digitalWrite(led14, LOW);
         delay(tOff);
         digitalWrite(led15, LOW);
         delay(tOff);
         digitalWrite(led16, LOW);
         delay(tOff);
         digitalWrite(led17, LOW);
         
         //set system as being off
         activeup = 0;
         activedown = 0;
         
         Serial.print("active up: ");
         Serial.println(activeup);
         Serial.print("active down: ");
         Serial.println(activedown);
         delay(del);
         
         Serial.println("System not active");
         delay(del);
         
} //End OffUp
 
void OnDown(){ //switches lights on going down
 
         Serial.println("Switching lights on (down)");
         delay(del);
               
         //set LEDs on in turn
         digitalWrite(led17, HIGH);
         delay(tOn);
         digitalWrite(led16, HIGH);
         delay(tOn);
         digitalWrite(led15, HIGH);
         delay(tOn);
         digitalWrite(led14, HIGH);
         delay(tOn);
         digitalWrite(led13, HIGH);
         delay(tOn);
         digitalWrite(led12, HIGH);
         delay(tOn);
         digitalWrite(led11, HIGH);
         delay(tOn);
         digitalWrite(led10, HIGH);
         delay(tOn);
         digitalWrite(led8, HIGH);
         delay(tOn);
         digitalWrite(led7, HIGH);
         delay(tOn);
         digitalWrite(led6, HIGH);
         delay(tOn);
         digitalWrite(led5, HIGH);
         delay(tOn);
         digitalWrite(led4, HIGH);
         delay(tOn);
         digitalWrite(led3, HIGH);
         delay(tOn);
         digitalWrite(led2, HIGH);
         delay(tOn);
         digitalWrite(led1, HIGH);
         
         activedown = 1; //sets system as active down
         
         Serial.print("active up: ");
         Serial.println(activeup);
         Serial.print("active down: ");
         Serial.println(activedown);
         delay(del);
         
         Serial.println("System now active (down)");
         delay(del);
 
} //End OnDown
 
void OffDown(){ //switches lights off going down
 
       Serial.println("Switching lights off (down)");
       delay(del);      
       
       
       //sets LEDs off in turn
       digitalWrite(led17, LOW);
       delay(tOff);
       digitalWrite(led16, LOW);
       delay(tOff);
       digitalWrite(led15, LOW);
       delay(tOff);
       digitalWrite(led14, LOW);
       delay(tOff);
       digitalWrite(led13, LOW);
       delay(tOff);
       digitalWrite(led12, LOW);
       delay(tOff);
       digitalWrite(led11, LOW);
       delay(tOff);
       digitalWrite(led10, LOW);
       delay(tOff);
       digitalWrite(led9, LOW);
       delay(tOff);
       digitalWrite(led8, LOW);
       delay(tOff);
       digitalWrite(led7, LOW);
       delay(tOff);
       digitalWrite(led6, LOW);
       delay(tOff);
       digitalWrite(led5, LOW);
       delay(tOff);
       digitalWrite(led4, LOW);
       delay(tOff);
       digitalWrite(led3, LOW);
       delay(tOff);
       digitalWrite(led2, LOW);
       delay(tOff);
       digitalWrite(led1, LOW);
       
       //set system as not active
       activedown = 0;
       activeup = 0;
       
       Serial.print("active up: ");
       Serial.println(activeup);
       Serial.print("active down: ");
       Serial.println(activedown);
       delay(del);
       
       Serial.println("System not active");
       delay(del);    
 
 
}//End OffDown

I have to reply my code in 2 posts cause max 9000 characters allowed sorry...

İ need this buttons on pin 19 and 20 to convert any IR(pir) sensör Thx...

This is isis simulator on this program(button version)

You want to have two PIR/motion sensors to replace the two switches, is this correct?

Have you used a HC-SR04 Ultrasonic distance module?

Yeah.. I know this HC-SR04 module. HC-SR04 or HC-SR501 it doesnt matter (I planning to use HC-SR501 by the way). İts not hardware issue. İts programming failure that i try and never solved.

int b_up = 19; //button at bottom of stairs
int b_down = 20; //button at top of stairs

I need to change that code to PIR module codes. My project is Led Stair Light sequently with PIR modules...

Change:

//Set button pins as input
  pinMode(b_up, INPUT_PULLUP);
  pinMode(b_down, INPUT_PULLUP);

To:

//Set pir pins as input
  pinMode(pir_up, INPUT);
  pinMode(pir_down, INPUT);

Actually, you don't even need the above code since pins default to input. Change all other instances of b_up and b_down to pir_up and pir_down.

Assuming you are using the common PIR detector available and used, the output of the detector will go high when detection is active so you will have to look for a HIGH on the pins you had your buttons connected to rather than a low.
Change:

if (up == LOW){ //Bottom button pressed

To:

if (up == HIGH){ //Bottom pir detection

Likewise, change all other instances of looking for a low at the (previous) button pins to looking for a HIGH.

Be aware that those common PIR detectors can have a rather long active output even after whatever set it off is gone. there should be a pot for output active adjustment. Also, there should be a jumper for retriggering. Those two adjustments may affect your desired results.

  • Scotty

I am newbie that arduino programming as my profile says but it is the answer what i want pal.. Beam me up scottyjr :slight_smile: you are my man THX ALOT.....

SOLVED

So if u use

//Set button pins as input
  pinMode(b_up, INPUT_PULLUP);
  pinMode(b_down, INPUT_PULLUP);

u can connect your button to the gnd directly and control your program with a (low=Vgnd) button but

if u use

//Set pir pins as input
  pinMode(pir_up, INPUT);
  pinMode(pir_down, INPUT);

this code u must connect 10k ohm resistor pin to the ground and control your program with a (high=5vdc) button...

i Just warn.. :slight_smile:

P.S. Why just dont telling me Scottyjr beam me up again

If your button connects ground to the arduino input pin when depressed you need a pullup resistor to ensure that the voltage seen at the input pin sees either a HIGH (5V) via the connection to 5V through the resistor or a LOW (0V) via the switch. If you don't have a pullup resistor, the voltage at the input pin might float (be anywhere between 0 and 5V). The ATMega IC has some handy dandy internal resistors that you can use instead of using external resistors. You tell the ATMega to use those internal resistors by using INPUT_PULLUP.

The PIR sensor is 'smarter' than a switch in that it will output either ground or 5V at it's output; it's output will not 'float' so there's no need for pullup resistors.

  • Scotty