Password Change Arduino

Greetings! I am doing something in this project that when the second button is pressed for 3 seconds it will prompt to a change password where the user must input the old password then the new password if the old password is correct. My only problem is that when the old password is inputted, it automatically updates the old password as the new password. Here's the code and layout in tinkercad

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int s = 0;
int s1 = 0;
int p = 0;
int w = 0;
int column = 0;
int row = 1;
int last = 0;
int nt = 0;
const char chars[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' '};
int previousstate = LOW;
unsigned long duration = 3000;
unsigned long longpress;
bool wstate = false;
const int interval = 50;
unsigned long pt;
unsigned long time;
unsigned long current;
String inputpass;
String newpass;
int cond = 0;
String currentpass = "ab";

void setup() 
{
  pinMode(13, INPUT);
  pinMode(10, INPUT);
  pinMode(A0, INPUT);
  lcd.begin(16, 2);
  Serial.begin(9600);
  home();
}

void loop() 
{
  current = millis();
  s = digitalRead(13);
  p = analogRead(A0);
  w = digitalRead(10);
  s1 = map(p, 0, 1023, 0, 26);
  lcd.setCursor(column,row);
  lcd.cursor();
  delay(50);
  lcd.print(chars[s1]);
  
  if (s != last)
  {
	if (s == 1)
	{
		inputpass.concat(chars[s1]);
		column++;
		if (column == 16)
		{
			column = 0;
			row=1;
		}
	}
	last = s;
  }
	if(current - pt > interval) 
	{
    if (w == 1 && previousstate == 0 && !wstate) 
	{
      longpress = current;
      previousstate = 1;
      Serial.println("Button pressed");
    }
    time = current - longpress;
	
    if (w == 1 && !wstate && time >= duration) 
	{
      wstate = true;
      Serial.println("Button long pressed");
	  lcd.clear();
	  lcd.setCursor(0,0);
	  lcd.print("Enter old pass:");
	  column = 0;
	  row = 1;
	  cond = 1;
	  inputpass = "";
    }
    if (w == 0 && previousstate == 1) 
	{
      previousstate = 0;
      wstate = false;
      Serial.println("Button released");
      if (time < duration) 
	  {
        Serial.println("Button pressed shortly");
		if (cond == 1)
		{
			if (inputpass == currentpass)
			{
				lcd.clear();
				lcd.setCursor(0,0);
				lcd.print("Enter new pass:");
				column = 0;
				row = 1;
				newpass = inputpass;
				inputpass = currentpass;
				Serial.print(currentpass);
			}
			else
			{
             	cond = 0;
				lcd.clear();
				inputpass = "";
				column = 0;
				row = 1;
				home();
			}
		}
		else
		{
			if (inputpass == currentpass)
			{
				lcd.clear();
				lcd.setCursor(1,0);
				lcd.print("Vault lock is");
				lcd.setCursor(6,1);
				lcd.print("OPEN");
				column = 17;
			}
			else
			{
				lcd.clear();
				for (int l = 0; l <= 4; l++)
				{
					lcd.setCursor(4,0);
					lcd.print("Error!");
					delay(500);
					lcd.clear();
					delay(500);
				}
				inputpass = "";
				column = 0;
				row = 1;
				home();
			}
		}
      }
    }
    pt = current;
  }
}
void home()
{
  lcd.setCursor(0,0);
  lcd.print("Enter Password:"); 
}

What is your guessing at which place inside the code do you have to make a change?

With your initial posting it sounds like

"here is my code can somebody adapt it to make it ready to run?"

You the thread-opener should show some own effort by at least guessing in which place in the code the change might be

best regards Stefan

Sorry for making it sound like that. I literally spent hours trying to make it work but I guess it really just make me want somebody to make it. Sorry about that. Thanks for replying though!

So you tried something and you can describe what you have tried

what is not clear to me how does entering characters work?
best regards Stefan

In the layout, I use the potentiometer as its "typing" of letters from a to z plus space. Then I use the concat() to make the inputted char into String and compare it with the current password which is "ab".

English is not my native language so I have to apologize in advanced

The situation that I want to happen is to change the current password. My guess is I have a wrong code here or wrong placement of code here. Which results to not updating the old password with the new password

    if (w == 0 && previousstate == 1) 
	{
      previousstate = 0;
      wstate = false;
      Serial.println("Button released");
      if (time < duration) 
	  {
        Serial.println("Button pressed shortly");
		if (cond == 1)
		{
			if (inputpass == currentpass)
			{
				lcd.clear();
				lcd.setCursor(0,0);
				lcd.print("Enter new pass:");
				column = 0;
				row = 1;
				newpass = inputpass;
				inputpass = currentpass;
				Serial.print(currentpass);
			}
			else
			{
             	cond = 0;
				lcd.clear();
				inputpass = "";
				column = 0;
				row = 1;
				home();
			}
		}
		else
		{
			if (inputpass == currentpass)
			{
				lcd.clear();
				lcd.setCursor(1,0);
				lcd.print("Vault lock is");
				lcd.setCursor(6,1);
				lcd.print("OPEN");
				column = 17;
			}
			else
			{
				lcd.clear();
				for (int l = 0; l <= 4; l++)
				{
					lcd.setCursor(4,0);
					lcd.print("Error!");
					delay(500);
					lcd.clear();
					delay(500);
				}
				inputpass = "";
				column = 0;
				row = 1;
				home();
			}
		}
      }
    }
    pt = current;
  }

from the grammar you used I guess you are german.
There is a german subforum

You can report your posting to a moderator saying you want to have it moved to the german subforum. If it arrived there translate your own postings back to german.

best regards Stefan

1 Like

i find it difficult to follow your code and it would help if you code explain what it is intended to do and how it does it.

in general it would help having sub-functions for recognizing button presses or long presses, entering a password, comparing an entered password to an existing one, updating a password and doing whatever the password enables

don't understand why the bulk of the code is executed inside a 50 msec timing loop. a sub-function for recognizing longer button presses should deal with the length of a button press.

a conventional button routine is to check for a change in state and then state: pressed, released. could capture timestamp and check if expired or released early to return "pressed" or "long press"

using variables for button pins instead of hardcoded values and more meaningful and consistent names (e.g. next, nextLst) for button variables will make the code more readable, rather than s, last, w, previousstate

it's common to have a "next" and "select" button.

a next button could scroll thru a short list of operations such as unlocking or updating a password. the select button selects the operation.

the pot can be used to choose a character in a password and the next button to capture that character and allow entry of the next character.

the select button captures the entered password and executes the operation: compare or update password.

multiple next button presses can wrap around the string being entered to allow editing. probably need mechanism to abort


breaking things into sub-functions will make the code easier to read as well as write and debug

'+' for posting your code as requested.
Major '-' for posting a Fritzing pretty picture. Fritzing pictures are NOT a schematic and many here won't even bother to try to follow the rats-nest of wires. Draw a proper schematic- nothing fancy, even a hand-drawn schematic is better than any Fritzing picture. If you draw a proper schematic I promise that more people will look at your project.

The first thing in your code that stands out is- not a single comment. Anywhere. And, we're supposed to know what you were thinking when you wrote the code? I guarantee that if you revisit the project in a year, you won't know either. Again, if you comment your code to let us in on your thought process, more people will try to assist.

I think this is how the program should work.
The potentiometer connected to A0 is used to change a letter being displayed
The button at D13 is used to select the letter being currently displayed
The button at D10 is used to:
Short press open the Vault Lock if the password matches cond=0
Long press will go to change password. First by entering the old password cond=1
Short press if password matches prompt for new password
Short press to save new password

So to change password the following steps should be done.

  1. Long press D10
  2. Using the potentiometer and D13 to type the old password
  3. Short press D10
  4. Using the potentiometer and D13 to type the new password
  5. Short press D10

To make the program work a new value of cond must be defined. cond=2 is entering new password.

Please try the code below:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int s = 0;
int s1 = 0;
int p = 0;
int w = 0;
int column = 0;
int row = 1;
int last = 0;
int nt = 0;
const char chars[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' '};
int previousstate = LOW;
unsigned long duration = 3000;
unsigned long longpress;
bool wstate = false;
const int interval = 50;
unsigned long pt;
unsigned long time;
unsigned long current;
String inputpass;
String newpass;
int cond = 0;
String currentpass = "ab";

void setup()
{
  pinMode(13, INPUT);
  pinMode(10, INPUT);
  pinMode(A0, INPUT);
  lcd.begin(16, 2);
  Serial.begin(9600);
  home();
}

void loop()
{
  current = millis();
  s = digitalRead(13);
  p = analogRead(A0);
  w = digitalRead(10);
  s1 = map(p, 0, 1023, 0, 26);
  lcd.setCursor(column, row);
  lcd.cursor();
  delay(50);
  lcd.print(chars[s1]);

  if (s != last)
  {
    if (s == 1)
    {
      inputpass.concat(chars[s1]);
      column++;
      if (column == 16)
      {
        column = 0;
        row = 1;
      }
    }
    last = s;
  }
  if (current - pt > interval)
  {
    if (w == 1 && previousstate == 0 && !wstate)
    {
      longpress = current;
      previousstate = 1;
      Serial.println("Button pressed");
    }
    time = current - longpress;

    if (w == 1 && !wstate && time >= duration)
    {
      wstate = true;
      Serial.println("Button long pressed");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Enter old pass:");
      column = 0;
      row = 1;
      cond = 1;
      inputpass = "";
    }
    if (w == 0 && previousstate == 1)
    {
      previousstate = 0;
      wstate = false;
      Serial.println("Button released");
      if (time < duration)
      {
        Serial.println("Button pressed shortly");
//GERRY MOD
        if (cond == 2) {
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Password Saved.");
          currentpass = inputpass;
          inputpass = "";
          cond = 0;
        } else if (cond == 1)
//      if (cond == 1)
        {
          if (inputpass == currentpass)
          {
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("Enter new pass:");
            column = 0;
            row = 1;

//GERRY MOD
            cond = 2;
            //newpass = inputpass;
            //inputpass = currentpass;
            inputpass = "";
            //Serial.print(currentpass);
            
          }
          else
          {
            cond = 0;
            lcd.clear();
            inputpass = "";
            column = 0;
            row = 1;
            home();
          }
        }
        else
        {
          if (inputpass == currentpass)
          {
            lcd.clear();
            lcd.setCursor(1, 0);
            lcd.print("Vault lock is");
            lcd.setCursor(6, 1);
            lcd.print("OPEN");
            column = 17;
          }
          else
          {
            lcd.clear();
            for (int l = 0; l <= 4; l++)
            {
              lcd.setCursor(4, 0);
              lcd.print("Error!");
              delay(500);
              lcd.clear();
              delay(500);
            }
            inputpass = "";
            column = 0;
            row = 1;
            home();
          }
        }
      }
    }
    pt = current;
  }
}
void home()
{
  lcd.setCursor(0, 0);
  lcd.print("Enter Password:");
}

1 Like

bandage on bandage

You don't seem to have any distinct states differentiating inputing a new password from inputing a password for verification. So you just set the new one to the one just input for verification.

try using google translate

Wow! Thank you for this! I actually got it working yesterday and we have similar result!
Here's my version btw

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int s = 0;
int s1 = 0;
int p = 0;
int w = 0;
int column = 0;
int row = 1;
int last = 0;
const char chars[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' '};
int previousstate = LOW;
unsigned long duration = 3000;
unsigned long longpress;
bool wstate = false;
unsigned long time;
unsigned long current;
String inputpass;
String currentpass = "ab";
int cond = 0;

void setup() 
{
  pinMode(13, INPUT);
  pinMode(10, INPUT);
  pinMode(A0, INPUT);
  lcd.begin(16, 2);
  Serial.begin(9600);
  home();
}
  // I used Serial.print as a guide if S2 is being pressed, long pressed, short pressed, or released
void loop() 
{
  current = millis();
  s = digitalRead(13);
  p = analogRead(A0);
  w = digitalRead(10);
  s1 = map(p, 0, 1023, 0, 26);
  lcd.setCursor(column,row);
  lcd.cursor();
  delay(50);
  lcd.print(chars[s1]);
  
  if (s != last)
  {
	if (s == 1)
	{
		inputpass.concat(chars[s1]);
		column++;
		if (column == 16)
		{
			column = 0;
			row=1;
		}
	}
	last = s;
  }
    if (w == 1 && previousstate == 0 && !wstate) 
	{
      longpress = current;
      previousstate = 1;
      Serial.println("Button pressed");
    }
    time = current - longpress;
	
    if (w == 1 && !wstate && time >= duration) 
	{
      wstate = true;
      Serial.println("Button long pressed");
	  lcd.clear();
	  lcd.setCursor(0,0);
	  lcd.print("Enter old pass:");
	  column = 0;
	  row = 1;
	  cond = 1;
    }
    if (w == 0 && previousstate == 1) 
	{
      previousstate = 0;
      wstate = false;
      Serial.println("Button released");
      if (time < duration) 
	  {
        Serial.println("Button pressed shortly");
		if (cond == 1)
		{
			if (inputpass == currentpass)
			{
				lcd.clear();
				inputpass = "";
				lcd.setCursor(0,0);
				lcd.print("Enter new pass:");
				column = 0;
				row = 1;
				cond = 2;
			}
			else
			{
             	cond = 0;
				lcd.clear();
				inputpass = "";
				column = 0;
				row = 1;
				home();
			}
		}
		else if (cond == 2)
		{
			lcd.clear();
			currentpass = inputpass;
			column = 0;
			row = 1;
			home();
			cond++;
			inputpass = "";
		}
		else
		{
			if (inputpass == currentpass)
			{
				lcd.clear();
				lcd.setCursor(1,0);
				lcd.print("Vault lock is");
				lcd.setCursor(6,1);
				lcd.print("OPEN");
				column = 17;
				inputpass = "";
			}
			else
			{
				lcd.clear();
				for (int l = 0; l <= 4; l++)
				{
					lcd.setCursor(4,0);
					lcd.print("Error!");
					delay(500);
					lcd.clear();
					delay(500);
				}
				inputpass = "";
				column = 0;
				row = 1;
				home();
			}
		}
      }
    }
}
void home()
{
  lcd.setCursor(0,0);
  lcd.print("Enter Password:"); 
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.