Hey, I am not a pro in the usage of arduino and its components and i have been facing a problemrelated to my project. Im making a security system using sensor and arduino according to this site https://howtomechatronics.com/projects/arduino-security-alarm-system-project/. Now the guy here uses a arduino mega while i use a uno. what changes would u say i should do in the program. i have changed lcd pin nos,keypad pin nos and snesor and buzzer pin nos. Lcd to A0 to A5, keypad from 1-8, sensor 9 & 10,buzzer 11. Is this the only changes that i need to make. because the lcd and keypad is working fine but when i activate the alarm nd wave my hand over the sensor. the alarm doesnt activate. i have checked in other posts and downloaded newping library. should i include newping library with #include in the header file, if so should i add anything else because i tried adding but error meassage comes. Thank u fr the help.
If you could post your properly formatted code in CODE TAGS, that would go a long way towards helping you, help us, help you.
This is he code i used
alarm_system.ino (7.29 KB)
if u need it in text format here it is
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#include <Keypad.h>
#define buzzer 11
#define trigPin 9
#define echoPin 10
long duration;
int distance, initialDistance, currentDistance, i;
int screenOffMsg =0;
String password="1234";
String tempPassword;
boolean activated = false; // State of the alarm
boolean isActivated;
boolean activateAlarm = false;
boolean alarmActivated = false;
boolean enteredPassword; // State of the entered password to stop the alarm
boolean passChangeMode = false;
boolean passChanged = false;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keypressed;
//define the cymbols on the buttons of the keypads
char keyMap[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {1, 2, 3, 4}; //Row pinouts of the keypad
byte colPins[COLS] = {5, 6, 7, 8}; //Column pinouts of the keypad
Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
void setup() {
lcd.begin(16,2);
pinMode(buzzer, OUTPUT); // Set buzzer as an output
pinMode(trigPin, INPUT); // Sets the trigPin as an Output
pinMode(echoPin, OUTPUT); // Sets the echoPin as an Input
}
void loop() {
if (activateAlarm) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Alarm will be");
lcd.setCursor(0,1);
lcd.print("activated in");
int countdown = 9; // 9 seconds count down before activating the alarm
while (countdown != 0) {
lcd.setCursor(13,1);
lcd.print(countdown);
countdown--;
tone(buzzer, 700, 100);
delay(1000);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Alarm Activated!");
initialDistance = getDistance();
activateAlarm = false;
alarmActivated = true;
}
if (alarmActivated == true){
currentDistance = getDistance() + 10;
if ( currentDistance < initialDistance) {
tone(buzzer, 1000); // Send 1KHz sound signal
lcd.clear();
enterPassword();
}
}
if (!alarmActivated) {
if (screenOffMsg == 0 ){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("A - Activate");
lcd.setCursor(0,1);
lcd.print("B - Change Pass");
screenOffMsg = 1;
}
keypressed = myKeypad.getKey();
if (keypressed =='A'){ //If A is pressed, activate the alarm
tone(buzzer, 1000, 200);
activateAlarm = true;
}
else if (keypressed =='B') {
lcd.clear();
int i=1;
tone(buzzer, 2000, 100);
tempPassword = "";
lcd.setCursor(0,0);
lcd.print("Current Password");
lcd.setCursor(0,1);
lcd.print(">");
passChangeMode = true;
passChanged = true;
while(passChanged) {
keypressed = myKeypad.getKey();
if (keypressed != NO_KEY){
if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
keypressed == '8' || keypressed == '9' ) {
tempPassword += keypressed;
lcd.setCursor(i,1);
lcd.print("");
i++;
tone(buzzer, 2000, 100);
}
}
if (i > 5 || keypressed == '#') {
tempPassword = "";
i=1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Current Password");
lcd.setCursor(0,1);
lcd.print(">");
}
if ( keypressed == '') {
i=1;
tone(buzzer, 2000, 100);
if (password == tempPassword) {
tempPassword="";
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set New Password");
lcd.setCursor(0,1);
lcd.print(">");
while(passChangeMode) {
keypressed = myKeypad.getKey();
if (keypressed != NO_KEY){
if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
keypressed == '8' || keypressed == '9' ) {
tempPassword += keypressed;
lcd.setCursor(i,1);
lcd.print("");
i++;
tone(buzzer, 2000, 100);
}
}
if (i > 5 || keypressed == '#') {
tempPassword = "";
i=1;
tone(buzzer, 2000, 100);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set New Password");
lcd.setCursor(0,1);
lcd.print(">");
}
if ( keypressed == '') {
i=1;
tone(buzzer, 2000, 100);
password = tempPassword;
passChangeMode = false;
passChanged = false;
screenOffMsg = 0;
}
}
}
}
}
}
}
}
void enterPassword() {
int k=5;
tempPassword = "";
activated = true;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" *** ALARM *** ");
lcd.setCursor(0,1);
lcd.print("Pass>");
while(activated) {
keypressed = myKeypad.getKey();
if (keypressed != NO_KEY){
if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
keypressed == '8' || keypressed == '9' ) {
tempPassword += keypressed;
lcd.setCursor(k,1);
lcd.print("");
k++;
}
}
if (k > 9 || keypressed == '#') {
tempPassword = "";
k=5;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" *** ALARM *** ");
lcd.setCursor(0,1);
lcd.print("Pass>");
}
if ( keypressed == '') {
if ( tempPassword == password ) {
activated = false;
alarmActivated = false;
noTone(buzzer);
screenOffMsg = 0;
}
else if (tempPassword != password) {
lcd.setCursor(0,1);
lcd.print("Wrong! Try Again");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" *** ALARM *** ");
lcd.setCursor(0,1);
lcd.print("Pass>");
}
}
}
}
// Custom function for the Ultrasonic sensor
long getDistance(){
//int i=10;
//while( i<=10 ) {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration*0.034/2;
//sumDistance += distance;
//}
//int averageDistance= sumDistance/10;
return distance;
}
And here is properly formatted in code tags
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#include <Keypad.h>
#define buzzer 11
#define trigPin 9
#define echoPin 10
long duration;
int distance, initialDistance, currentDistance, i;
int screenOffMsg = 0;
String password = "1234";
String tempPassword;
boolean activated = false; // State of the alarm
boolean isActivated;
boolean activateAlarm = false;
boolean alarmActivated = false;
boolean enteredPassword; // State of the entered password to stop the alarm
boolean passChangeMode = false;
boolean passChanged = false;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keypressed;
//define the cymbols on the buttons of the keypads
char keyMap[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {1, 2, 3, 4}; //Row pinouts of the keypad
byte colPins[COLS] = {5, 6, 7, 8}; //Column pinouts of the keypad
Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
void setup() {
lcd.begin(16, 2);
pinMode(buzzer, OUTPUT); // Set buzzer as an output
pinMode(trigPin, INPUT); // Sets the trigPin as an Output
pinMode(echoPin, OUTPUT); // Sets the echoPin as an Input
}
void loop() {
if (activateAlarm) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Alarm will be");
lcd.setCursor(0, 1);
lcd.print("activated in");
int countdown = 9; // 9 seconds count down before activating the alarm
while (countdown != 0) {
lcd.setCursor(13, 1);
lcd.print(countdown);
countdown--;
tone(buzzer, 700, 100);
delay(1000);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Alarm Activated!");
initialDistance = getDistance();
activateAlarm = false;
alarmActivated = true;
}
if (alarmActivated == true) {
currentDistance = getDistance() + 10;
if ( currentDistance < initialDistance) {
tone(buzzer, 1000); // Send 1KHz sound signal
lcd.clear();
enterPassword();
}
}
if (!alarmActivated) {
if (screenOffMsg == 0 ) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("A - Activate");
lcd.setCursor(0, 1);
lcd.print("B - Change Pass");
screenOffMsg = 1;
}
keypressed = myKeypad.getKey();
if (keypressed == 'A') { //If A is pressed, activate the alarm
tone(buzzer, 1000, 200);
activateAlarm = true;
}
else if (keypressed == 'B') {
lcd.clear();
int i = 1;
tone(buzzer, 2000, 100);
tempPassword = "";
lcd.setCursor(0, 0);
lcd.print("Current Password");
lcd.setCursor(0, 1);
lcd.print(">");
passChangeMode = true;
passChanged = true;
while (passChanged) {
keypressed = myKeypad.getKey();
if (keypressed != NO_KEY) {
if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
keypressed == '8' || keypressed == '9' ) {
tempPassword += keypressed;
lcd.setCursor(i, 1);
lcd.print("*");
i++;
tone(buzzer, 2000, 100);
}
}
if (i > 5 || keypressed == '#') {
tempPassword = "";
i = 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Current Password");
lcd.setCursor(0, 1);
lcd.print(">");
}
if ( keypressed == '*') {
i = 1;
tone(buzzer, 2000, 100);
if (password == tempPassword) {
tempPassword = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set New Password");
lcd.setCursor(0, 1);
lcd.print(">");
while (passChangeMode) {
keypressed = myKeypad.getKey();
if (keypressed != NO_KEY) {
if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
keypressed == '8' || keypressed == '9' ) {
tempPassword += keypressed;
lcd.setCursor(i, 1);
lcd.print("*");
i++;
tone(buzzer, 2000, 100);
}
}
if (i > 5 || keypressed == '#') {
tempPassword = "";
i = 1;
tone(buzzer, 2000, 100);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set New Password");
lcd.setCursor(0, 1);
lcd.print(">");
}
if ( keypressed == '*') {
i = 1;
tone(buzzer, 2000, 100);
password = tempPassword;
passChangeMode = false;
passChanged = false;
screenOffMsg = 0;
}
}
}
}
}
}
}
}
void enterPassword() {
int k = 5;
tempPassword = "";
activated = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" *** ALARM *** ");
lcd.setCursor(0, 1);
lcd.print("Pass>");
while (activated) {
keypressed = myKeypad.getKey();
if (keypressed != NO_KEY) {
if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
keypressed == '8' || keypressed == '9' ) {
tempPassword += keypressed;
lcd.setCursor(k, 1);
lcd.print("*");
k++;
}
}
if (k > 9 || keypressed == '#') {
tempPassword = "";
k = 5;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" *** ALARM *** ");
lcd.setCursor(0, 1);
lcd.print("Pass>");
}
if ( keypressed == '*') {
if ( tempPassword == password ) {
activated = false;
alarmActivated = false;
noTone(buzzer);
screenOffMsg = 0;
}
else if (tempPassword != password) {
lcd.setCursor(0, 1);
lcd.print("Wrong! Try Again");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" *** ALARM *** ");
lcd.setCursor(0, 1);
lcd.print("Pass>");
}
}
}
}
// Custom function for the Ultrasonic sensor
long getDistance() {
//int i=10;
//while( i<=10 ) {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
//sumDistance += distance;
//}
//int averageDistance= sumDistance/10;
return distance;
}
should i include newping library with #include in the header file
Yes. Even better is to look at the newPing examples and see how they do the thing.
Where are the requested code tags? Or were you so much in a rush to try and get help you tried to shortcut your way in by simply not bothering to read the "how to use this forum" sticky, telling you how to properly ask a question?
This is definitely an issue:
pinMode(trigPin, INPUT); // Sets the trigPin as an Output
pinMode(echoPin, OUTPUT); // Sets the echoPin as an Input
Thank u fr ur quick replies. I have to submit this in two days and this is the only problem right nw. Acc to the program after i select activate alarm in my led using keypad, after it says alarm activated the sensor actually has to see a motion and the alarm has to buzz and ask for password. but its not changing. that is the problem i have right now. By formatted code do u mean the correct code because it is also not working. Im sry bt im new to this. I apologize fr the inconvenience. i ve actually tried adding newping as a library but its sayin "used the library. exit status 1. Error compiling for arduino board. Thank u fr replying back quick.
Im sry 'wvmarle' but yes i was in a hurry as i said before and im new to this. what do u mean by code tags.
also the issue u found was done by me really because trigpin,output and echopin,input was actually what i did. since it didnt work i did the opposite since i saw it somewhere and i accidently send that prog. here. Thanks fr getting back quickly btw.
if anybody could get the probblem pls get back. im 99% sure its a problem of the program. because this program was originally for a mega. I updated it for my uno. also this program is old. so as ive seen in other posts the sensor had some problem with library. what shld i update in this program to get my sensor working. i dont think its the problem of my sensor since i checked it by myself and in an newping library example it was working ok. If anybody wants to check the site im doing the project from. This the link- https://howtomechatronics.com/projects/arduino-security-alarm-system-project/.
once again tnx fr the help.
TitaniumGT:
Im sry 'wvmarle' but yes i was in a hurry as i said before and im new to this. what do u mean by code tags.
You obviously didn't bother to read the "How to use this Forum" sticky.
I did read it.I guess i didntread the code tags one. Im sry fr the trouble caused, But i dont know where in the program is the fault.Pls read my above reply and help. Thanks
Read reply #5 again. Study how the HCSR04 works. Read reply #5 again.
Ok I dont think u guys are understanding my problem. Ill simplify it for u. Forget abt everything. This is a simple program i took from the newping library and send tothe arduino.
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(sonar.convert_cm(uS)); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println("cm");
}
Basically this is the program written just below the official newping library download. I ve run it and even though this time the sensor is detecting my hand, when i open the serial monitor some jumbled characters are only being printed. now this is this the problem of the program or the sensor
The baud rate in the sketch is set to 115200. What is the serial monitor baud rate?
I get this in serial monitor with the code in reply #10.
Ping: 0cm
Ping: 24cm
Ping: 0cm
Ping: 9cm
Ping: 11cm
Ping: 11cm
Ping: 11cm
Tnx... When I set it to 115200 baud it does remove the gibberish. But the problem still lies where the ping just changes very fast with values 0cm and 176cm(max. I think). Mind u this is without me putting anything btw the sensor. The serial monitor is automatically showing values 0,176 respect. Does this mean my sensor has some problems because urs only returned correct values and when u put an object in front.
It appears you're trying to read the sensor as often as possible, without waiting for echoes of the previous ping to die out (10-100 ms typically).
Also I noticed a delay() call in your code. Also bad.
Thnk u so much fr getting back bt could u be a bit more descriptive. I'm nt gud at this. Like r u saying it's program problem and not sensor problem. Could u tell me the changes I have to make to the program I have to make descriptively. Tnx. Or else just link me to a program I can check out to see if my sensor is working properly... Thnk u so much once again and sry fr the trouble
Use the blink without delay method to do non blocking timing with millis().
Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().
An example with the delay replaced with millis() timing:
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup()
{
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 500; // ping every 500 milliseconds
if (millis() - timer >= interval)
{
timer = millis();
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(sonar.convert_cm(uS)); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println("cm");
}
}
tnx so much fr taking time to write it. Nw the ping is coming and automatically scrolling down at a slower rate. bt the vale is showing zeroexcept when i put my hand on it it says 176, when i like tap on it diff. values come like 177 186 96,etc. is this normalor does this mean that it is broken
The sensor will read 0 for out of range, ie the timeout elapses before a good echo is received or over the maximum distance (#define MAX_DISTANCE 200).
when i put my hand on it it says 176,
Is your hand 176mm away from the sensor? Does the range change when you move your hand?
I think your first priority is to remove the LCD related code, and simply focus on getting values to be seen in the Serial Monitor window. Those important values would be the duration of the 'high voltage' time of the 'echo' waveform.
Also --- run the code on the UNO from here ..... to see if your ultrasonic sensor is working properly.
https://forum.arduino.cc/index.php?msg=4325455
Just change the trig and echo pin settings in that code, and run it. Then copy/paste some results.
TitaniumGT:
I did read it.I guess i didntread the code tags one. Im sry fr the trouble caused, But i dont know where in the program is the fault.Pls read my above reply and help. Thanks
As given to you as a hint from wvmarle ------ you have to think about whether the TRIGGER pin of your arduino for this particular application is meant to be an INPUT ..... or an OUTPUT.
Same for the 'echo' pin.
The code below means that you don't appear to have even thought about the conflicting words 'input' and 'output' on the SAME line.
pinMode(trigPin, INPUT); // Sets the trigPin as an Output
pinMode(echoPin, OUTPUT); // Sets the echoPin as an Input
TitaniumGT:
since it didnt work i did the opposite since i saw it somewhere
It's definitely beneficial to avoid that sort of thing. That is, if you know in advance that something is meant to be an output, or something else is meant to be an input ------ then totally avoid reversal of their actual known function.
In this case - no harm done. In some other cases ------ bad things could happen, that could affect property or even people's lives.
Forgetting to restore the code back to the correct code means somebody needs to come along to pick up the pieces.