Pages: [1] 2   Go Down
Author Topic: how to enable and disable signal coming from sensor using keypad  (Read 882 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

gud day everyone..can anyone help me about that topic???? smiley-cry
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26508
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When is our assignment due?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i think thrs something wrong of my codes??? smiley-roll-blue
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26508
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't see any debug code.
Quote
think thrs something wrong of my codes???
I think you're asking why the compiler is complaining about your code.
Mostly, it doesn't like executable code that isn't in a function.
But that's just a guess, because you haven't provided any detail, and I'm not about to bother putting it through a compiler.

Edit: The code does after all look to be in functions - the formatting threw me.
« Last Edit: March 01, 2012, 05:35:19 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you want the PIR signal enabled and disabled or do you just want the Arduino to use or ignore the PIR?

Because just controlling power going to the PIR, on/off can control whether or not it will send signals. Of course you probably also don't want to read the PIR pin if it's powered off. Not hard to do but a detail to deal with.

You have PIR code that works? An example you have wired and tested and it works?
I have no PIR so I am blind in that respect.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes sir..i want to enable the signal if the password input by a keypad is valid....else it disable the signal...

here is my code in keypad:

Code:
#include <Password.h>
#include <LiquidCrystal.h>
#include <Keypad.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

Password password = Password( "4567" );
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3',},
  {'4','5','6',},
  {'7','8','9',},
  {'*','0',' ',}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {6, 7, 8, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {10, 13, 14}; //connect to the column pinouts of the keypad


// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );



void setup(){
                                          
lcd.begin(16, 2);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
keypad.setDebounceTime(250);
}

void loop(){
char key = keypad.getKey();
  if (key){
   lcd.setCursor(0, 0);
    lcd.print('*'); //display * when input the password
  
  }}



//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
switch (eKey){
case ' ': guessPassword(); break;

default:
password.append(eKey);
}
}}

void guessPassword(){
if (password.evaluate()){
delay(500);
lcd.setCursor(0, 1);
lcd.print("VALID PASSWORD"); //
password.reset(); //resets password after correct entry
delay(1000);
lcd.clear();
lcd.print("ACTIVATED!");
delay(2000);
lcd.clear();

}

else{
lcd.setCursor(0, 1);
lcd.print("INVALID PASSWORD");
password.reset(); //resets password after INCORRECT entry
delay(5000);
lcd.clear();
lcd.print("TRY AGAIN!");
delay(2000);
lcd.clear();
}
}

PIR Code + servo:

Code:
#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;                                                      // Variable to store the servo position in degrees
int pinPIRleft = 15;                                               // left infrared sensor, digital pin                                          
int pinPIRright = 16;                                              // right sensor, digital pin                                        

void setup() {
  pinMode(pinPIRleft, INPUT);                                    // set sensors as inputs
  pinMode(pinPIRright, INPUT);
  myservo.attach(17);    
}
 
void loop(){
  if (((pos < 180) && (digitalRead(pinPIRright) == HIGH)&& (digitalRead(pinPIRleft) == HIGH))){      //PIR detected motion
     pos += 1;                                                   // increment servo degrees by +1
      myservo.write(pos);                                         // write the position to the servo
    }
    else {}// do nothing
  
    if  ((pos >= 1) && (digitalRead(pinPIRleft) == LOW) && (digitalRead(pinPIRright) == LOW)) {    //no motion detected
      pos -= 1;
      myservo.write(pos);
    }
    else{}
  
}

Moderator edit: CODE TAGS AGAIN.

how can i combine the codes of keypad to PIR and servo..my project is when u input password then if it is valid..thats the time that the PIR will activate or on...else PIR will ignore signals...and when i enter again the password the PIR will off..
« Last Edit: March 01, 2012, 07:26:08 am by AWOL » Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

To combine, you want all the parts in each without duplicating function or trying to use 1 pin for 2 things and common sense errors like that.

There should only be 1 setup() and 1 loop(). Each should have all the working parts from setup() and loop() in both of the original (and this is important, working and tested) programs.

like this for setup():
Code:
void setup(){
                                           
lcd.begin(16, 2);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
keypad.setDebounceTime(250);

  pinMode(pinPIRleft, INPUT);                                    // set sensors as inputs
  pinMode(pinPIRright, INPUT);
  myservo.attach(17);   
}

All the parts to make variables from both need to be there, and any duplicate names you need to change one and every line that uses it also. Any pins that both use the same pin for different purpose, one must be changed to a different pin and that different pin tested in the original code. There must be no "collisions" but all the pieces must be there.

Do you really need to turn the PIR off or just not have the servo move in response to the PIR?
There is a very real difference in the code, especially if the PIR does not have an OFF function.

BTW, when you use if() you do not have to include else. Else is optional. There is no need for else do nothing.

What I don't understand is how you can write that much code and not be able to merge the two.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 i can use  1 pin for 2 PIR sir???is that possible??
i'l try it before sir..but only 1 PIR is working...


if thrs a way sir to off the PIR...but how can i do that sir..to not move the servo even thers a motion detected????

it is possible to merge the 2 codes sir??how??

Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No.

But if you try to use 1 pin for PIR and the same pin for LCD then you need to use a different pin for one of those.
That is common sense.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i know that sir..

but sir my problem is how do i combine the code so that if i enter password thats the time that
PIR will detect or shall we say on..or the servo will on????
and when i enter again the password either of the 2 will off???

give me some idea sir to solve that problem...
tnx sir..
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If it was me, I would have a byte variable starting as 0 that would flag whether the PIR and servo code would run at all. Something like

Code:
  if ( runPIR ) {
//    PIR and servo loop code goes here
  }

where runPIR would only be non-zero when the password is authorized. The flag would act as a software switch.

And BTW, I am not "sir". I was enlisted and I worked for my living.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

master i write this code..but its not working..
only the password is ok..


 
Code:
if (password.evaluate())
{
  delay(500);
  lcd.setCursor(0, 1);
  lcd.print("VALID PASSWORD"); //
  password.reset(); //resets password after correct entry
  delay(1000);
  lcd.clear();
  lcd.print("ACTIVATED!");
  delay(2000);
  lcd.clear();

///something wrong in this code??
  val = digitalRead(pinPIRleft);  // read input value
      if (val == HIGH) {            // check if the input is HIGH
        if (pirState == LOW) {
        Serial.println("Motion detected!");               // we have just turned on
        myservo.write(180);                                //180deg
        pirState = HIGH;
    }
  } else {
        if (pirState == HIGH){                          
         Serial.println("Motion ended!");  // we have just turned of
         myservo.write(0);                 //0deg
         pirState = LOW;
    }
  }

}

else{
  lcd.setCursor(0, 1);
  lcd.print("INVALID PASSWORD");
  password.reset(); //resets password after INCORRECT entry
  delay(5000);
  lcd.clear();

  lcd.print("TRY AGAIN!");
  delay(2000);
  lcd.clear();
}
}

Moderator edit: CODE TAGS
« Last Edit: March 05, 2012, 05:49:45 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26508
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm getting a little bored adding code tags to your posts every time you post some code - how about you do it for yourself?

Quote
i write this code..but its not working..
Anyone else in favour of a PayPal swear-box for every time this phrase is posted?   smiley-evil
Proceeds to go to my favourite charity, the Forum Moderators' Beer-Fund.
« Last Edit: March 05, 2012, 05:56:13 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

only the password is ok..

The PIR check code only runs inside if (password.evaluate()) {}. That if() encompasses too much code. All it should do is run the display and set/clear a variable to store Activated status. The PIR check should always run when Activated is true, using a different if() than the password.evaluate() so they don't only work at the same time. I assume you want the PIR to work -after- the password is entered? So they need to be separated.

 
Code:
if (password.evaluate())
{
  delay(500);
  lcd.setCursor(0, 1);
  lcd.print("VALID PASSWORD"); //
  password.reset(); //resets password after correct entry
  delay(1000);
  lcd.clear();
  lcd.print("ACTIVATED!");
  delay(2000);
  lcd.clear();

// note: if (password.evaluate) should end here after setting a variable TRUE for activated status
// note: and then the else with all the INVALID PASSWORD code moved up to here

// note: and then another if() checking for activated status TRUE to hold the PIR code

  val = digitalRead(pinPIRleft);  // read input value
      if (val == HIGH) {            // check if the input is HIGH
        if (pirState == LOW) {
        Serial.println("Motion detected!");               // we have just turned on
        myservo.write(180);                                //180deg
        pirState = HIGH;
    }
  } else {
        if (pirState == HIGH){                          
         Serial.println("Motion ended!");  // we have just turned of
         myservo.write(0);                 //0deg
         pirState = LOW;
    }
  }

}

else{
  lcd.setCursor(0, 1);
  lcd.print("INVALID PASSWORD");
  password.reset(); //resets password after INCORRECT entry
  delay(5000);
  lcd.clear();

  lcd.print("TRY AGAIN!");
  delay(2000);
  lcd.clear();
}
}

Don't think sequential code please! Make code and data to run the parts you need as you want them to act:
Keypad -- get user entry, filter errors/run display, set or clear activated status.
PIR -- if activated status is set then check PIR and display results.

In time you might even get those delays out.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes sir I want the PIR to work after the password is entered...
what should i do????
Logged

Pages: [1] 2   Go Up
Jump to: