Central MN, USA
Offline
Faraday Member
Karma: 35
Posts: 5913
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #45 on: January 30, 2013, 10:11:31 am » |
Jeremy,
You are welcome! You're making progress of your own. I recommend you to write a sketch just for PIR and test it out then integrate with my sample code. You can have as many state as you want with the state machine. A good idea is to draw a flow chart to indicate how you want the machine to transition between each state (eg. 30 seconds in the disarmed state puts it back to some alert state). If you decide on the flow chart, writing code is just transcribing it into C.
|
|
|
|
|
Logged
|
|
|
|
|
UK Norfolk
Offline
Newbie
Karma: 0
Posts: 38
|
 |
« Reply #46 on: January 30, 2013, 01:59:50 pm » |
Hi liudr. I have already written a simple PIR sketch and will have a go at making another state. Thank you for the flow chart idea I will use it to develop the project. Hopefully I will post the finished code soon. If you want to have a laugh at what I am using for a Keypad have a look here. Thank you for all of your help to make this project work for me Jeremy
|
|
|
|
|
Logged
|
There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0
|
|
|
|
UK Norfolk
Offline
Newbie
Karma: 0
Posts: 38
|
 |
« Reply #47 on: February 06, 2013, 06:01:33 pm » |
Hi all. I am still unable to get the sensor to work in the sketch. I have tried all sorts but to no avail. I have been playing with state machines until I almost used my Arduino for a Frisbee. I managed to stop myself though. On another note I got one of those 4x4 membrane keypads yesterday for £3 on that well known auction site. I have stuck a few resistors on a breadboard and thanks to Liudr's libraries and sketch have it displaying all of the symbols perfectly. 1 2 3 A 4 5 6 B 7 8 9 C * 0 # D I just thought you would like to know that some things I do work Jeremy
|
|
|
|
|
Logged
|
There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0
|
|
|
|
UK Norfolk
Offline
Newbie
Karma: 0
Posts: 38
|
 |
« Reply #48 on: February 07, 2013, 07:39:41 pm » |
Hi I have now got it working but before I throw my hat in the air there is one slight problem. The PIR sensor takes 5 seconds to calibrate. When you power the system it works just fine but when you press the arming button on the keypad the alarm goes off because the sensor is giving a High signal. Can anyone suggest anything that will stop it please. Jeremy /* This sketch uses a 16 key 4X4 analog keypad. The password is 1234 press # to check the password. if correct the Red LED goes out and the Green Led comes on to rearm the alarm press * If when the system is in the disarmed state the C key is pressed both LED's light and you can enter a new 4 digit password. The sensor part is still a work in progress. */
#include <phi_interfaces.h>
#define buttons_per_column 16 // Each analog pin has five buttons with resistors. #define buttons_per_row 1 // There are two analog pins in use.
byte keypad_type=Analog_keypad; char mapping[]={ '1','2','3','A','4','5','6','B','7','8','9','C','*','0','#','D'}; // This is an analog keypad. byte pins[]={ 0}; // The pin numbers are analog pin numbers. int values[]={ 204,370,480,559,146,333,455,541,78,292,428,521,0,245,397,499}; //These numbers need to increase monotonically. phi_analog_keypads panel_keypad(mapping, pins, values, buttons_per_row, buttons_per_column); multiple_button_input* pad1=&panel_keypad;
// pin assignments int speakerOut = 11; int redLedpin = 12; int greenLedpin = 10; int alarmPin=3; int sensePin=A4;
// variables char* inputString = "0000"; // holds the user input char password[5] = "1234"; // holds the default correct password that can later be changed int curpos = 0; // cursor position int locked = 1; // status of the system: if locked = 1, if unlocked = 0 int motion; long previousMillis = 0; long interval = 5000; int alarmPinstate = LOW;
void setup(){ Serial.begin(9600); pinMode(sensePin,INPUT);// sensor pinMode(speakerOut, OUTPUT);// keypad beeps and if passcode accepted tone pinMode(redLedpin, OUTPUT);// system armed pinMode(alarmPin,OUTPUT); pinMode(greenLedpin, OUTPUT);//system dissarmed
} void scanning(){ motion=analogRead(sensePin); //Serial.println(motion);// used for debugging unsigned long startTime = 0; unsigned long interval = 5000;
if ((locked==1)&&(motion>=500)){// if the alarm is on read the sensor if (startTime + interval < millis()) { digitalWrite(alarmPin, HIGH); }//added } else {//added digitalWrite (alarmPin, LOW);
}
}
void loop(){
if(locked ==1)//if the alarm is on { scanning(); digitalWrite(redLedpin, HIGH);
digitalWrite(greenLedpin, LOW);
}
if(locked ==0)// if the alarm is off {
digitalWrite(redLedpin, LOW);// if this pin is low the alarm will be off digitalWrite(greenLedpin, HIGH); scanning(); }
// receive input from keypad byte temp=panel_keypad.getKey();
if (temp != NO_KEY){ // a key is pressed Serial.write(temp); playKeyTone(); // play a beep to acknowledge that key pressed
switch(temp) // look for the special keys to initiate an event { case '#': parseString(); // try unlock routine curpos = 0; break; case '*': // lock the system
Serial.println("Armed");
locked = 1; clearPassword(); // clear the password so that user needs to enter 4 digits again before unlocking curpos = 0; // set cursor back to start of 4 digits break; case 'C': changePassword(); // change password routine curpos = 0; break; }
if(temp != '*' && temp != '#' && temp != 'C') { inputString[curpos] = temp; // if the key was none of the above three special ones, add the digit to the password string curpos = curpos + 1; // move cursor to next position }
if(curpos > 3) // if the cursor has reached the end of 4 digits, return it to the start { curpos = 0; }
} }
void playKeyTone(){ // beep key press int elapsedtime = 0; while (elapsedtime < 100) { digitalWrite(speakerOut,HIGH); delayMicroseconds(500);
digitalWrite(speakerOut, LOW); delayMicroseconds(500); elapsedtime++; } }
void playWarningTone(){ // long beep for wrong password int elapsedtime = 0; while (elapsedtime < 200) { digitalWrite(speakerOut,HIGH); delayMicroseconds(1000);
digitalWrite(speakerOut, LOW); delayMicroseconds(1000); elapsedtime++; } }
void playSuccessTone(){ // short beep for correct password int elapsedtime = 0; while (elapsedtime < 10) { digitalWrite(speakerOut,HIGH); delayMicroseconds(1000);
digitalWrite(speakerOut, LOW); delayMicroseconds(1000);
digitalWrite(speakerOut,HIGH); delayMicroseconds(1000);
digitalWrite(speakerOut, LOW); delayMicroseconds(1000); elapsedtime++; } }
void parseString() // parse password to see if it matches { Serial.println("Code Entered"); Serial.println(inputString); if(matchString(password, inputString) == 1) // did they match? { Serial.println("SYSTEM IS OFF. PRESS * TO ARM"); locked = 0; // correct code entered, unlock system playSuccessTone(); return; } else playWarningTone(); // nope, wrong password Serial.println("Wrong Password"); }
void changePassword() { Serial.println("PASSWORD CHANGE MODE"); Serial.println("ENTER NEW PASSWPORD"); if(locked == 0) // only get into this mode if unlocked! (otherwise anyone can change password without knowing correct one! that would be dumb!) { digitalWrite(redLedpin, HIGH); // turn on RED LED digitalWrite(greenLedpin, HIGH); // turn on GREEN LED char pkey = NO_KEY; int c = 0; while(c < 4) {
pkey = panel_keypad.getKey(); // get new password 4 digits if(pkey != NO_KEY) { playKeyTone(); password[c] = pkey; // store it in the password string Serial.println(pkey); c = c + 1; }
} Serial.println("THANK YOU YOUR NEW PASSWORD IS:"); Serial.println(password);
} }
int matchString(char* string1, char*string2) // function to match two strings, returns 1 if they match, 0 if they dont {
for(int i=0; i < 5; i++) { if(string1[i] != string2[i]) { return 0; } } return 1; }
void clearPassword() // clears the entered password after system is locked (so person can't just press # to unlock, needs to enter 4 digit code again!) { for(int a=0; a < 4; a++) { inputString[a] = '0'; } }
|
|
|
|
|
Logged
|
There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0
|
|
|
|
Central MN, USA
Offline
Faraday Member
Karma: 35
Posts: 5913
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #49 on: February 08, 2013, 11:18:13 am » |
I really don't know much about PIR sensors. If there is human activity, does it stay HIGH? If I can take that as true, I recommend this:
Maybe have a timer that counts 5 seconds. When the system is in armed state and high is sensed from PIR, start this timer and put the system in an additional state called alert. In the alert state, all it does is to wait for the timer to expire while reading PIR and confirming it's still high. After successfully passing 5 seconds with PIR high, go to alarm state? If system fails to see PIR stay HIGH for 5 seconds, go back to armed state. Will this work?
|
|
|
|
|
Logged
|
|
|
|
|
UK Norfolk
Offline
Newbie
Karma: 0
Posts: 38
|
 |
« Reply #50 on: February 10, 2013, 09:27:14 am » |
Hi Liudr. The PIR is basically just a switch which sends out a low when there is no movement and a High when movement is detected. It will stay high for about 5 seconds after it stops seeing motion. The time the PIR stays high can be adjusted using an on board trimmer pot. This I will eventually make it 30 seconds for the siren (alarm Pin) to sound. Obviously i will have to change the timing on this part of the sketch void scanning(){ motion=analogRead(sensePin); //Serial.println(motion);// used for debugging unsigned long startTime = 0; unsigned long interval = 6000;
if ((locked==1)&&(motion>=500)){// if the alarm is on read the sensor if (startTime + interval < millis()) { digitalWrite(alarmPin, HIGH); }//added } else {//added digitalWrite (alarmPin, LOW);
}
} the unsigned long interval will be changed to accommodate the time needed for the PIR to go low. I have tried to add a timer state called alert as you suggested but have not been able to get it to work. I know I am missing something simple but I just cannot sort it out. I will keep trying though. Jeremy
|
|
|
|
|
Logged
|
There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0
|
|
|
|
Central MN, USA
Offline
Faraday Member
Karma: 35
Posts: 5913
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #51 on: February 10, 2013, 05:24:16 pm » |
Jeremy,
Maybe I misread your last post. Did you mean if you start the system and immediately push arm then the alarm will sound due to the fact the PIR sensor calibrates at startup and outputs HIGH during the calibration? If this is the case, I think there could be a different state machine sequence:
Start up (run a timer of say 5 seconds, any button push is responded by a buzz, "I'm busy starting up!!") -(timer runs out)-> disarmed (waiting for button push to enter armed mode) -- etc.
So if you manually create start up state that transitions to disarmed state after 5 seconds, then you can avoid alarm. Just my 2 cents.
|
|
|
|
|
Logged
|
|
|
|
|
UK Norfolk
Offline
Newbie
Karma: 0
Posts: 38
|
 |
« Reply #52 on: February 10, 2013, 06:51:30 pm » |
YES YES YES It works. The answer was staring me in the face. It did not need any more states just 2 lines of code, a Capacitor and a resistor. All I did was add a 220uf cap via 100k resistor to the yellow led. I then put a wire from the capacitor to A5. the current draw is so tiny but the cap does slowly charge. When you arm the system the reading on A5 drops over time to allow the PIR to settle down before the capval drops to 3. I am aware that I owe you a massive thank you Liudr for your patience with a novice and for you spending your time to write some code for me. Also for pointing me in the right direction. Again Thank you Jeremy void scanning(){ capVal=analogRead(cap);//This Has Fixed It go down to the next if statement //Serial.println (capVal); motion=analogRead(sensePin); //Serial.println(motion);// used for debugging unsigned long startTime = 0; unsigned long interval = 5000;
if ((locked==1)&&(motion>=500)&&(capVal<3)){//HERE IS THE FIX if (startTime + interval < millis()) { digitalWrite(alarmPin, HIGH); }//added } else {//added digitalWrite (alarmPin, LOW);
}
}
|
|
|
|
|
Logged
|
There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0
|
|
|
|
UK Norfolk
Offline
Newbie
Karma: 0
Posts: 38
|
 |
« Reply #53 on: February 11, 2013, 08:25:12 am » |
Here is the completed code although I have set the state to unlocked when the system starts now because I intend to have a rechargeable battery in the case the alarm will be housed in. If I were to run it without I would set the state to locked. I have added another pin for the capacitor charging but would stress the need for a large value resistor(I used 100k) for the capacitor. Jeremy /* This sketch uses a 16 key 4X4 analog keypad. The password is 1234 press # to check the password. if correct the Red LED goes out and the Green Led comes on to rearm the alarm press * If when the system is in the disarmed state the C key is pressed both LED's light and you can enter a new 4 digit password. The sensor was triggering the alarm when armed from the unlocked state but was solved by using a capacitor to hold a small charge which decays slowly when the alarm is set allowing the PIR sensor time to settle down. */ //Thank you Liudr for your guidance and help with the code. // Your phi interface is a valuable set of tools.
#include <phi_interfaces.h>
#define buttons_per_column 16 // Each analog pin has five buttons with resistors. #define buttons_per_row 1 // There are two analog pins in use.
byte keypad_type=Analog_keypad; char mapping[]={ '1','2','3','A','4','5','6','B','7','8','9','C','*','0','#','D'}; // This is an analog keypad. byte pins[]={ 0}; // The pin numbers are analog pin numbers. int values[]={ 204,370,480,559,146,333,455,541,78,292,428,521,0,245,397,499}; //These numbers need to increase monotonically. phi_analog_keypads panel_keypad(mapping, pins, values, buttons_per_row, buttons_per_column); multiple_button_input* pad1=&panel_keypad;
// pin assignments int speakerOut = 11; int redLedpin = 12; int greenLedpin = 10; int alarmPin=3; int capCharger=2; int sensePin=A4; int cap=A5; int capVal;
// variables char* inputString = "0000"; // holds the user input char password[5] = "1234"; // holds the default correct password that can later be changed int curpos = 0; // cursor position int locked = 0; // status of the system: if locked = 1, if unlocked = 0 int motion; long previousMillis = 0; long interval = 10000; int alarmPinstate = LOW;
void setup(){ Serial.begin(9600); pinMode(sensePin,INPUT);// sensor pinMode(speakerOut, OUTPUT);// keypad beeps and if passcode accepted tone pinMode(redLedpin, OUTPUT);// system armed pinMode(alarmPin,OUTPUT); pinMode(greenLedpin, OUTPUT);//system dissarmed pinMode(capCharger,OUTPUT); /* The cap charger will charge a capacitor that stops the PIR sensor from showing an alert when setting the alarm. The pir shows high when power is applied so the cap stops the alarm triggering as soon as the alarm is powered on. */ } void scanning(){ capVal=analogRead(cap);//This Has Fixed It Serial.println (capVal);
motion=analogRead(sensePin); //Serial.println(motion);// used for debugging unsigned long startTime = 0; unsigned long interval = 5000;
if ((locked==1)&&(motion>=500)&&(capVal<100)){// if the alarm is on read the sensor if (startTime + interval < millis()) { digitalWrite(alarmPin, HIGH); } } else { digitalWrite (alarmPin, LOW);
}
}
void loop(){
if(locked ==1)//if the alarm is on { scanning(); digitalWrite(redLedpin, HIGH); digitalWrite(capCharger,LOW); digitalWrite(greenLedpin, LOW);
}
if(locked ==0)// if the alarm is off {
digitalWrite(redLedpin, LOW);// if this pin is low the alarm will be off digitalWrite(greenLedpin, HIGH); digitalWrite(capCharger,HIGH);// this starts the capacitor charging scanning(); }
// receive input from keypad byte temp=panel_keypad.getKey();
if (temp != NO_KEY){ // a key is pressed Serial.write(temp); playKeyTone(); // play a beep to acknowledge that key pressed
switch(temp) // look for the special keys to initiate an event { case '#': parseString(); // try unlock routine curpos = 0; break; case '*': // lock the system
Serial.println("Armed");
locked = 1; clearPassword(); // clear the password so that user needs to enter 4 digits again before unlocking curpos = 0; // set cursor back to start of 4 digits break; case 'C': changePassword(); // change password routine curpos = 0; break; }
if(temp != '*' && temp != '#' && temp != 'C') { inputString[curpos] = temp; // if the key was none of the above three special ones, add the digit to the password string curpos = curpos + 1; // move cursor to next position }
if(curpos > 3) // if the cursor has reached the end of 4 digits, return it to the start { curpos = 0; }
} }
void playKeyTone(){ // beep key press int elapsedtime = 0; while (elapsedtime < 100) { digitalWrite(speakerOut,HIGH); delayMicroseconds(500);
digitalWrite(speakerOut, LOW); delayMicroseconds(500); elapsedtime++; } }
void playWarningTone(){ // long beep for wrong password int elapsedtime = 0; while (elapsedtime < 200) { digitalWrite(speakerOut,HIGH); delayMicroseconds(1000);
digitalWrite(speakerOut, LOW); delayMicroseconds(1000); elapsedtime++; } }
void playSuccessTone(){ // short beep for correct password int elapsedtime = 0; while (elapsedtime < 10) { digitalWrite(speakerOut,HIGH); delayMicroseconds(1000);
digitalWrite(speakerOut, LOW); delayMicroseconds(1000);
digitalWrite(speakerOut,HIGH); delayMicroseconds(1000);
digitalWrite(speakerOut, LOW); delayMicroseconds(1000); elapsedtime++; } }
void parseString() // parse password to see if it matches { Serial.println("Code Entered"); Serial.println(inputString); if(matchString(password, inputString) == 1) // did they match? { Serial.println("SYSTEM IS OFF. PRESS * TO ARM"); locked = 0; // correct code entered, unlock system playSuccessTone(); return; } else playWarningTone(); // nope, wrong password Serial.println("Wrong Password"); }
void changePassword() { Serial.println("PASSWORD CHANGE MODE"); Serial.println("ENTER NEW PASSWPORD"); if(locked == 0) // only get into this mode if unlocked! (otherwise anyone can change password without knowing correct one! that would be dumb!) { digitalWrite(redLedpin, HIGH); // turn on RED LED digitalWrite(greenLedpin, HIGH); // turn on GREEN LED char pkey = NO_KEY; int c = 0; while(c < 4) {
pkey = panel_keypad.getKey(); // get new password 4 digits if(pkey != NO_KEY) { playKeyTone(); password[c] = pkey; // store it in the password string Serial.println(pkey); c = c + 1; }
} Serial.println("THANK YOU YOUR NEW PASSWORD IS:"); Serial.println(password);
} }
int matchString(char* string1, char*string2) // function to match two strings, returns 1 if they match, 0 if they dont {
for(int i=0; i < 5; i++) { if(string1[i] != string2[i]) { return 0; } } return 1; }
void clearPassword() // clears the entered password after system is locked (so person can't just press # to unlock, needs to enter 4 digit code again!) { for(int a=0; a < 4; a++) { inputString[a] = '0'; } }
|
|
|
|
|
Logged
|
There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0
|
|
|
|
Central MN, USA
Offline
Faraday Member
Karma: 35
Posts: 5913
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #54 on: February 11, 2013, 11:16:48 am » |
Thank you for posting the complete code Jeremy. I was glad to help you with some questions but you worked out the entire solution. I was just covering the capacitors in my electronics class. So I'll use what you did as an example to do hardware debouncing on buttons. Please post some pictures and/or videos when you're done, preferably on the Exhibition forum!
|
|
|
|
|
Logged
|
|
|
|
|
|