# Making a 4 digit Passcode

Hey Guys!

I'm working on a project that uses a 4 digit passcode using numbers 1-5 to unlock something. However, I can't figure out a way to get a working passcode. I have seen that there is a library available for this; however, I would like to do this myself (I'm not very experienced, and I want to learn as much as I can). Would someone be able to give me guidance on how I could do this?

Thanks!

Have you decided on a way for the digits to be entered?

Paul

Yes. I'm using capacitive sensors made from tinfoil, which I'm basically done with.

micwall:
Yes. I'm using capacitive sensors made from tinfoil, which I'm basically done with.

You now have the 4 digits. What is the problem?
Paul

Paul_KD7HB:
You now have the 4 digits. What is the problem?
Paul

I'm not sure how to code it so I can enter in a four digit code and decide whether or not it is the correct passcode.

What code do you have to read the capacitive sensors and determine which digit was pressed?

``````#include <CapacitiveSensor.h>
CapacitiveSensor sensor1 = CapacitiveSensor(3, 4);  //initiate each sensor
CapacitiveSensor sensor2 = CapacitiveSensor(5, 6);
CapacitiveSensor sensor3 = CapacitiveSensor(7, 8);
CapacitiveSensor sensor4 = CapacitiveSensor(9, 10);
CapacitiveSensor sensor5 = CapacitiveSensor(11, 12);

int threshold1 = 75;  //thresholds for each sensor
int threshold2 = 75;
int threshold3 = 75;
int threshold4 = 75;
int threshold5 = 75;

void setup() {
Serial.begin(115200);         //bowl of serial
}

void loop() {
long value1 = sensor1.capacitiveSensor(15); //assign a value for each sensor
long value2 = sensor2.capacitiveSensor(15);
long value3 = sensor3.capacitiveSensor(15);
long value4 = sensor4.capacitiveSensor(15);
long value5 = sensor5.capacitiveSensor(15);
}
``````

It’s not much, but here’s what I’ve got so far. Also, just to clarify, there will be 5 sensors, each for a digit 1-5. The passcode will consist of 4 digits.

Edit: Sorry if that was confusing - I used ‘code’ to refer to both the coding and the passcode! Edits have been made : )

And there is this https://github.com/PaulStoffregen/CapacitiveSensor <<< Has examples and links to other related things.

After reading about this thing, you should post a schematic with resistor values. Resistor values are used as part of the code to determine the thing.

Thanks you, but all I would like to know is how to code it to record a passcode that is entered in. Just something simple and general, not necessarily specific to capacitive sensors. Thank you though!

Really? Okay here is the pseudocode for using the code:

``````Repeat {
x = 4 digits from input
if x is equal to the passcode
{
unlock
}
else
{
tell user passcode is incorrect
}
}
``````

To enter a code:

``````index = 0
while index < 4
{
code[index] = input key
}
``````

Saving keypad entries is very common (because many applications work with strings of key values). It would behoove you to research existing keypad sketches, as many (most?) of them must do this.

micwall:
I'm not sure how to code it so I can enter in a four digit code and decide whether or not it is the correct passcode.

Others have been there. Try a site search on 'combination lock'.

Thank you for the help! Could you explain what is happening in here??

``````index = 0
while index < 4
{
code[index] = input key
}
``````

Thanks!

micwall:
Thank you for the help! Could you explain what is happening in here??

``````index = 0
``````

while index < 4
{
code[index] = input key
}

``````

Thanks!
``````

Sure. I did forget something important, wow that was dumb. Should be:

``````index = 0
while index < 4
{
code[index] = input key
index = index + 1
}
``````

First time through, index = 0 so code gets the first key you enter
Second time, index = 1 so code gets the value of the next key

Last time, index = 3 so code gets the last value

At this point code contains the first, second, third, and last values that you entered.
After index reaches 4, while is finished and you have your key code stored in the code array.

Here is a reference for arrays:
https://www.arduino.cc/reference/en/language/variables/data-types/array/

Thank you for the help everyone! This all is very helpful, and I think that I have all that I need. Thanks again!