Help me with this please

Greetings ,

I have Arduino UNO and I connected 4x4 keypad and two led green and red and also a servo motor .

If I enter 789 the green led will be ON and servo motor rotates by 90 degree . If the password is wrong , nothing happened .

I want to add HC-SR04 ULTRASONIC MOTION SENSOR to my project, and it will be ON and detects motion
and if any motion detected it will make alarm sound . But if the password entered is correct it will be OFF .

Please help me with this issue

Here is my code (Not my own code copied from a website and edited it ) :

#include <Keypad.h>
#include <Servo.h>
Servo servo_Motor;
char* password = “789”;
int position = 0;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {

{‘1’, ‘2’, ‘3’, ‘A’},
{‘4’, ‘5’, ‘6’, ‘B’},
{‘7’, ‘8’, ‘9’, ‘C’},
{’’, ‘0’, ‘#’, ‘D’}
};
byte rowPins[ROWS] = { 10,9,8,7 };
byte colPins[COLS] = { 6,5,4,3 };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int redPin = 12;
int greenPin = 13;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
servo_Motor.attach(11);
setLocked(true);
}
void loop()
{
char key = keypad.getKey();
if (key == '
’ || key == ‘#’)
{
position = 0;
setLocked(true);
}
if (key == password[position])
{
position ++;
}
if (position == 3)
{
setLocked(false);
}
delay(100);
}
void setLocked(int locked)
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
servo_Motor.write(11);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
servo_Motor.write(90);
}
}

Thanks in advance

if (key == password[position]) 
 { 
 position ++; 
 } 
 if (position == 3) 
 { 
 setLocked(false); 
 }

That's real secure. Just bang the keys in order three times, and the door pops open.

I want to add HC-SR04 ULTRASONIC MOTION SENSOR to my project

Permission granted.

and if any motion detected it will make alarm sound

Regardless of whether or not the door is open? How is the Arduino supposed to make an "alarm sound"?

But if the password entered is correct it will be OFF

Add a call to shutTheFxxkUp(), and put the appropriate code in the function.

Please read the How-to-post note at the top of the Forum on the proper way to post code here. Also, before you post using code tags, use Ctrl-T in the IDE to reformat your code in a more-standard format. This will help us help you.

PaulS:

if (key == password[position]) 

{
position ++;
}
if (position == 3)
{
setLocked(false);
}



That's real secure. Just bang the keys in order three times, and the door pops open.
Permission granted.
Regardless of whether or not the door is open? How is the Arduino supposed to make an "alarm sound"?
Add a call to shutTheFxxkUp(), and put the appropriate code in the function.

If the code is not secure , that not my fault . I copied it from a website and started to learn from scratch step by step ... I started learning arduino two months ago ONLY . So dont be fool and start scoffing .. If you are professional you can help the beginners not scoff .

If the code is not secure , that not my fault .

That's won't make you feel any better when someone breaks in, will it?

If you are professional you can help the beginners

I'm trying, but I can't figure out what you need help with. And, I thought you might want to know that I spotted that problem in less than 10 seconds, so anyone else could, too. And, others might want to exploit the problem.

If the code is not secure , that not my fault

But the fact remains that it is insecure,

Shouldn't something happen if the user enters an incorrect character ? As written the program waits patiently until you hit upon the correct combination then opens the lock. Try working through the program on paper noting the value of variables and discover what will happen with a password of "789" if the user enters 123456789

I want to add HC-SR04 ULTRASONIC MOTION SENSOR to my project, and it will be ON and detects motion
and if any motion detected it will make alarm sound . But if the password entered is correct it will be OFF .

So start by looking at some code for the HC-SR04 and try to understand it. Next try to integrate it into your current sketch (and e.g send the measured distance to the PC vial the serial port).

Once you can measure distance, you need to remember it and check if it has changed. If it has changed, there was movement and you can send that information over the serial port.

In the next step, you can check the status of e.g. the red led; if it's on, the system is locked and you 'must' send the message over the serial port, if the red led is off, the system is not locked and you should not send a warning over the serial port.

And in the last step you add the hardware for the alarm and replace sending over the serial port by activation of the alarm.

PaulS:
That's won't make you feel any better when someone breaks in, will it?
I'm trying, but I can't figure out what you need help with. And, I thought you might want to know that I spotted that problem in less than 10 seconds, so anyone else could, too. And, others might want to exploit the problem.

I'm just learning therefore the code maybe looks very simple for prof. programmer . I will try to make it more secured

The key is to count every keystroke, and store all the data, not just count the valid keystrokes. Compare the stored data to the correct password when the "enter" key is pressed. Of course, this means that you need to have some key defined as an enter key, and one as a reset key, rather than 2 reset keys.

Look at the Password library for some ideas.