Show Posts
|
|
Pages: [1] 2 3
|
|
4
|
Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please
|
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'; } }
|
|
|
|
|
5
|
Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please
|
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);
}
}
|
|
|
|
|
6
|
Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please
|
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
|
|
|
|
|
7
|
Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please
|
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'; } }
|
|
|
|
|
8
|
Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please
|
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
|
|
|
|
|
10
|
Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please
|
on: January 29, 2013, 07:12:06 pm
|
|
Hi HazardsMind. I did put the sensor in in various places in the get password case because i could only get a reading on it there. I found it caused problems with the keypad function. The problem seems to be in the case get password part of the sketch where the program sits for the majority of the time waiting for a key press.I wherever the sensor is in the sketch the keypad is slow to respond and I noticed that when the alarm was triggered the led on pin 11 was very dim. I have tried giving the motion sensor its own power supply with the grounds connected but this makes no difference. I tried the led on a different pin with the same result. I was trying to work out how to have the sketch sit waiting in a different part lets call it case check_sensor but I am not able to do this. My programming skills have been learned over the last few months with some library books and the internet. I used to drive a Bus and my hobby is electronics and now I am learning to program my arduino. The trouble with me is I always think "How hard can it be?" Learning to write code is like learning to speak Chinese with a Spanish phrase book. But I am picking it up slowly. Jeremy
|
|
|
|
|
12
|
Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please
|
on: January 29, 2013, 06:18:29 pm
|
Hi and a big thank you to liudr. Here is the code I am now using. I have got over the keypad press turning the alarm off. I have added another state called disarmed and the system stops at that point until the tactile button is pressed. This is fine but the motion sensor is a different story. I have tried instead to use a digital pin to read the sensor as it gives a clear 1 if motion detected and a 0 if not. I have tried using it in different places in the sketch but it is causing problems with the key presses(it was doing the same with an analog input) Also I can use a tactile button to simulate the motion sensor because it keeps going high with me moving my hands around. It is getting there but not quite. I will keep trying to get the sensor working though. Jeremy #include <phi_interfaces.h>
#define buttons_per_column 12 // 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','4','5','6','7','8','9','*','0','#'}; // This is an analog keypad. byte pins[]={0}; // The pin numbers are analog pin numbers. int values[]={ 58, 96, 145, 226, 335, 447, 558, 680, 790, 890, 941, 970}; //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;
#define alarm_pin 13 enum States {get_password, alarm, cleared,dissarmed}; States system_state=get_password;
unsigned long alarm_starting, cleared_starting; char password[]="11111";// easy password for testing char buffer[20]; int pointer=0;
int led=11;// led to simulate alarm int PIR=A5;// motion sensor cannot get to work so not used int tripped;// value to hold pir int pirPin=5;/*I was using this pin as pir gives a digital 1 or 0 cannot get it to work in the sketch it just seems to stall the keypad */ int buttonPin=2;//small tactile button to turn system on int buttonState;//state of the tactile button void setup() { Serial.begin(9600); pinMode(alarm_pin,OUTPUT); digitalWrite(alarm_pin,LOW); // digitalWrite(A0,HIGH); // DO this only if you are using phi-3 shield. pinMode(alarm_pin,OUTPUT); digitalWrite(alarm_pin,LOW); pinMode(led,OUTPUT); pinMode(pirPin,INPUT); pinMode(buttonPin,INPUT); }
void loop() { byte temp; switch (system_state) { case get_password: if (millis()-alarm_starting>=10000)// if alarm is tripped this turns led off // I changed the time just for testing { digitalWrite(led,LOW); } // do get password stuff temp=panel_keypad.getKey(); // Use phi_keypads object to access the keypad if (temp!=NO_KEY) { Serial.write(temp); buffer[pointer++]=temp; if (pointer==5) { buffer[pointer]=0; pointer=0; if (!strcmp(buffer,password)) { Serial.println("Authenticated!"); system_state=dissarmed;//cleared; //dissarmed turns the system off cleared_starting=millis(); digitalWrite(led,LOW); // Open a door or something } else { Serial.println("Unauthorized!"); system_state=alarm; alarm_starting=millis(); digitalWrite(alarm_pin,HIGH); } } } break; case dissarmed: digitalWrite(led,LOW); buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { //system off until button pressed. system_state=get_password; } break; case alarm: digitalWrite(led,HIGH); if ((panel_keypad.getKey()!=NO_KEY)||(millis()-alarm_starting>=10000)) { system_state=get_password; digitalWrite(alarm_pin,LOW); } break; case cleared: if ((!panel_keypad.getKey())||(millis()-cleared_starting>=10000)) { system_state=get_password; // Close the door or something. } break; } }
|
|
|
|
|
13
|
Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please
|
on: January 29, 2013, 10:01:23 am
|
Good! Yes, that feature can be turned on and off inside the alarm case. I don't kow what you exactly wanted. Hi liudr. What I wanted is to attach a PIR sensor and have the value it senses for movement set the alarm buzzer off for say 30 seconds. The keypad needs the correct code to disarm the alarm buzzer and ignore the PIR sensing or turn off the PIR sensor using a transistor. If i use a PNP transistor setting a digital pin HIGH via a resistor to the base of the transistor would cut the power to the PIR sensor. if (pointer==5) { buffer[pointer]=0; pointer=0; if (!strcmp(buffer,password)) { Serial.println("Authenticated!"); system_state=cleared; cleared_starting=millis(); II hope this is a help Jeremy // Open a door or something digitalWrite (tripped,LOW); digitalWrite(alarmOff,HIGH); } } else { Serial.println("Unauthorized!"); system_state=alarm; alarm_starting=millis(); digitalWrite(alarm_pin,HIGH); digitalWrite(tripped,HIGH); digitalWrite (alarmOff,LOW); } } }[code] [/code]
|
|
|
|
|
14
|
Using Arduino / Project Guidance / Re: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please
|
on: January 29, 2013, 08:16:59 am
|
|
Hi liudr. Thank you for the revised code. This does work but when the alarm is tripped any keypad press turns the alarm off. I tried it this morning. I am reading all I can find about state machines too. Perhaps I should leave it today as I have had more injections into my spine and they hurt badly. I may have a look later if I feel better. I hope I am not taking up too much of your time on this. Regards Jeremy
|
|
|
|
|