verry strange problem with Serial.println

I am making an arduino based Code Lock with muptiple passwords and function to add new passwords on the fly.
So here is the problem:
I am using ISIS and Simulino to simulate the Arduino. I have placed several Serial.println to debug my program and at the end where everything seems to be ok I deleted all of them and then something just stopped working. Then I started to delete them one by one to see which one fools me. So here is the code the s**t is Serial.println((char)currentEEPROMCell); located on row 128 (use Ctrl+F to find it 'couse the rows are not displayed here). As you see all others 'println's are commented only this one isn't.
I am also planing to make some sound effects and it will be 100% complete

#include <Keypad.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>

#define lockPin 2
const String newPasswordKey = "s*123#s";
const int errorDelayTime = 1500;
const byte ROWS = 4;
const byte COLS = 4;
char Keys[ROWS][COLS] = {
  {'1','2','3','c'},
  {'4','5','6','d'},
  {'7','8','9','a'},
  {'*','0','#','s'}
};

byte rowPins[ROWS] = {12, 11, 10, 9};
byte colPins[COLS] = {8, 7, 6, 5};

Keypad _keyboard = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS);

LiquidCrystal lcd(A5, A4, A3, A2, A1, A0);
String inputStr = "";
boolean checkStr = false;
byte currentCursorPosition = 0;
boolean makeNewPassword = false;

void setup(){
  lcd.begin(16, 2);
  //Serial.begin(9600);
  pinMode(lockPin, OUTPUT);
}
  
void loop(){
  char button = _keyboard.getKey();
  
  if (button){
    if (button == 'c'){
        inputStr = "";
        makeNewPassword = false;
        lcd.clear();
    }
    else if (button == 'd'){
      if(inputStr.length()>0){
        byte currentPosition = inputStr.length()-1;
        inputStr.setCharAt(currentPosition, ' ');
        inputStr.trim();
        lcd.setCursor(currentPosition, 1);
        lcd.write(" ");
        lcd.setCursor(currentPosition, 1);
      }
    }
    else if (button == 'a'){
      checkStr = true;
    }
    else{
      inputStr += button;
      lcd.setCursor(inputStr.length()-1, 1);
      if(makeNewPassword){
        lcd.print(button);
      }
      else{
        lcd.print("*");
      }
    }
    //Serial.println(inputStr);
  }
  
  if(inputStr.length()>=4 && checkStr == true){
    int inputStrLen = inputStr.length();
    if (inputStr == newPasswordKey){
     lcd.clear(); 
     lcd.write("New Password:");
     lcd.setCursor(0,1);
     makeNewPassword = true;
    }
    else if(makeNewPassword == true){
      //Serial.println("saving new password");
      if(inputStr.indexOf('s')>-1){
        //ERROR
        //Serial.println("forbidden character");
        //sound signal
        lcd.clear();
        lcd.print("forbidden character");
        delay(errorDelayTime);
        lcd.clear();
      }
      else{
        //read the new password and save it to the EEPROM
        for(int address = 0; address<1024; address++){
          if((EEPROM.read(address) == 0xFF) && (EEPROM.read(address+1) == 0xFF)){
            //Serial.println("End of passwords!");
            for(int Nchar=0; Nchar<inputStrLen; Nchar++){
              //Serial.println("Writing in EEPROM");
              EEPROM.write(address+1+Nchar, inputStr[Nchar]);
            }
            lcd.clear();
            //sound signal
            lcd.print("PASSWORD SAVED");
            delay(errorDelayTime);
            lcd.clear();
            break;
          }
          if(address==1021){
            //No space for new passwords
            //sound signal
            lcd.clear();
            lcd.print("OUT OF MEMORY");
            delay(errorDelayTime);
            lcd.clear();
            break;
          }
        }
      }

      //Serial.println("Password saved");
      makeNewPassword == false;
    }
    
    else{
      //Serial.println("Checking password");
     //check passwords
     byte currentEEPROMCell;
     String EEPROMPassword = "";
     for(int readAddress; readAddress < 1024; readAddress++){
       //Serial.print("reaing from EEPROM("+ (String)readAddress +")=");
       currentEEPROMCell = EEPROM.read(readAddress);
       Serial.println((char)currentEEPROMCell);
       if(currentEEPROMCell!= 0xFF){
         EEPROMPassword += (char)currentEEPROMCell;
       }
       else{
         if(inputStr == EEPROMPassword){
           //sound signal
           lcd.clear();
           lcd.print("ACCESS GRANTED");
           digitalWrite(lockPin, HIGH);
           delay(2500);
           digitalWrite(lockPin, LOW);
           lcd.clear();
           break;
         }
         else if(EEPROM.read(readAddress+1) == 0xFF){
           //sound signal
           lcd.clear();
           lcd.print("ACCESS DENIED");
           delay(errorDelayTime);
           lcd.clear();
           break;
         }
         else{
           EEPROMPassword = "";
         }
       }
     }
     EEPROMPassword = "";
    }
    inputStr = "";
  }
  else if(checkStr == true){
    //String is too short
    lcd.clear();
    lcd.print("SHORT PASSWORD");
    delay(errorDelayTime);
    lcd.clear();
    inputStr = "";
  }
  
  checkStr = false;
  
}

I'm not sure that I understand what your problem is. There is a huge bug in your program, though.

     for(int readAddress; readAddress < 1024; readAddress++){

The first part of the for statement is an initialization section. You have failed to initialize readAddress. For all you know, the value in readAddress when the loop starts could be 17364. That's more than 1024, so the for loop would not be executed.

Ok so I make it like this:

     for(int readAddress = 0; readAddress < 1024; readAddress++){

And now everything seems to run propperly thank you for that. But I still don't understand why when I have the Serial.println there it also runs correctly, I just dont get it :expressionless:

But I still don't understand why when I have the Serial.println there it also runs correctly, I just dont get it

Maybe it is something to do with this:

I am using ISIS and Simulino to simulate the Arduino

mayght be, mayght be not ... Who knows...
What ever everything is working correctly now thanks to PaulS

After a few days brake, I wanted to show the project to a frient and i found that it still dont run correctly. This time it's the function for adding new password, after saving it, it doesn't exit the mode and continues to save the input string to the EEPROM. here is the latest code :

#include <Keypad.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>

#define lockPin 2
const String newPasswordKey = "s*123#s";
const int errorDelayTime = 1500;
const byte ROWS = 4;
const byte COLS = 4;
char Keys[ROWS][COLS] = {
  {'1','2','3','c'},
  {'4','5','6','d'},
  {'7','8','9','a'},
  {'*','0','#','s'}
};

byte rowPins[ROWS] = {12, 11, 10, 9};
byte colPins[COLS] = {8, 7, 6, 5};

Keypad _keyboard = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS);

LiquidCrystal lcd(A5, A4, A3, A2, A1, A0);
String inputStr = "";
boolean checkStr = false;
byte currentCursorPosition = 0;
boolean makeNewPassword = false;

void setup(){
  lcd.begin(16, 2);
  pinMode(lockPin, OUTPUT);
}

void loop(){
  char button = _keyboard.getKey();

  if (button){
    if (button == 'c'){
      inputStr = "";
      makeNewPassword = false;
      lcd.clear();
    }
    else if (button == 'd'){
      if(inputStr.length()>0){
        byte currentPosition = inputStr.length()-1;
        inputStr.setCharAt(currentPosition, ' ');
        inputStr.trim();
        lcd.setCursor(currentPosition, 1);
        lcd.write(" ");
        lcd.setCursor(currentPosition, 1);
      }
    }
    else if (button == 'a'){
      checkStr = true;
    }
    else{
      inputStr += button;
      lcd.setCursor(inputStr.length()-1, 1);
      if(makeNewPassword){
        lcd.print(button);
      }
      else{
        lcd.print("*");
      }
    }
  }

  if(inputStr.length()>=4 && checkStr == true){
    int inputStrLen = inputStr.length();
    if (inputStr == newPasswordKey){
      lcd.clear(); 
      lcd.write("New Password:");
      lcd.setCursor(0,1);
      makeNewPassword = true;
    }
    else if(makeNewPassword == true){
      if(inputStr.indexOf('s')>-1){
        //ERROR
        //sound signal
        lcd.clear();
        lcd.print("forbidden character");
        delay(errorDelayTime);
        lcd.clear();
      }
      else{
        //read the new password and save it to the EEPROM
        for(int address = 0; address<1024; address++){
          if((EEPROM.read(address) == 0xFF) && (EEPROM.read(address+1) == 0xFF)){
            for(int Nchar=0; Nchar<inputStrLen; Nchar++){
              EEPROM.write(address+1+Nchar, inputStr[Nchar]);
            }
            lcd.clear();
            //sound signal
            lcd.print("PASSWORD SAVED");
            delay(errorDelayTime);
            lcd.clear();
            break;
          }
          if(address==1021){
            //No space for new passwords
            //sound signal
            lcd.clear();
            lcd.print("OUT OF MEMORY");
            delay(errorDelayTime);
            lcd.clear();
            break;
          }
        }
      }

      makeNewPassword == false;
    }

    else{
      //check passwords
      byte currentEEPROMCell;
      String EEPROMPassword = "";
      for(int readAddress=0; readAddress < 1024; readAddress++){
        currentEEPROMCell = EEPROM.read(readAddress);
        if(currentEEPROMCell!= 0xFF){
          EEPROMPassword += (char)currentEEPROMCell;
        }
        else{
          if(inputStr == EEPROMPassword){
            //sound signal
            lcd.clear();
            lcd.print("ACCESS GRANTED");
            digitalWrite(lockPin, HIGH);
            delay(2500);
            digitalWrite(lockPin, LOW);
            lcd.clear();
            break;
          }
          else if(EEPROM.read(readAddress+1) == 0xFF){
            //sound signal
            lcd.clear();
            lcd.print("ACCESS DENIED");
            delay(errorDelayTime);
            lcd.clear();
            break;
          }
          else{
            EEPROMPassword = "";
          }
        }
      }
      EEPROMPassword = "";
    }
    inputStr = "";
  }
  else if(checkStr == true){
    //String is too short
    lcd.clear();
    lcd.print("SHORT PASSWORD");
    delay(errorDelayTime);
    lcd.clear();
    inputStr = "";
  }

  checkStr = false;

}

Please explain this:

        byte currentPosition = inputStr.length()-1;
        inputStr.setCharAt(currentPosition, ' ');
        inputStr.trim();

Find the length of the string. Replace the NULL that terminates the wrapped string, making it no longer a string. Then, call a method that expects to operate on the wrapped string. Why?

it doesn't exit the mode

What mode? And, why should it exit that mode? Whatever "it" is.

        //read the new password and save it to the EEPROM

The code that follows does not read the new password.

Haven't you been asked before to put each { on a new line, and use Tools + Auto Format? If not, consider yourself asked. Your code will be a lot easier to read.

Your method of storing passwords in EEPROM leaves a lot to be desired. You seem to be trying to find two consecutive addresses that have not been written to, and then storing a string starting at the second location. Apparently, this is meant to leave 0xFF as a separator between the strings.

Fixed length passwords (up to some limit), with some address storing the number of saved passwords, would be far easier to implement. Writing then simply means reading the number of saved passwords, multiplying by the length of a password, and viola, you know where to write. Go back and update the number of stored passwords after saving another one.

Reading passwords is easy, too, since you again know where each one starts.

First of all I want to sorry about my bad English and to say that this is my first attempt in Arduino programing. I have been watching videos and reding info in internet for quite a while, but never actually writing some code. I have a verry little experiance with C# lenguage and a little bit PHP, but nothing serious, only small programs doing simple things and no more than 100-150 rows of code.

Please explain this:....

The idea here is to remove the last character from the string, and I did not figure out a better way to do this. I also did not know that the last character of of variables of type String is NULL If you know way of removing last character from String please tell me!

it doesn't exit the mode

I am trying to explain that when I enter the special code for adding new passwords, and add the new password the program("it")should get back to the normal operation. I mean displaing '*'s when a character is pressed and compare the input string to the passwords saved in EEPROM, insted of that the program continues to show exact same character as the pressed button is and to save the string to the EEPROM. the interesting fact here is that I think this worked before I remove the "Serial.println"s

The only thing that I can tell about the rest of your post is this : :blush:

The idea here is to remove the last character from the string

Lets look at a simple case, where you press 4 keys, say 4, 3, 2, and 8. and then the d key.

The String will then wrap a string containing '4', '3', '2', '8', and 0. The length of the string will be 4, so you are putting a space where the 8 is (not the NULL as I was thinking earlier). Why do you want to remove the '8' from the string? Then, you trim() the String, removing the space you just added. It would have been simpler to set the nth character to NULL ('\0'). Then, there is no need to trim() the String.

It doesnt work, I tested it.
inputStr.setCharAt(currentPosition, '\0');
If I have a String "1324" and I press "d" button I will have a String containing {'1', '2','3', '\0'} which are still 4 characters, and the \0 will be savet to the EEPROM like oxFF, wich is my password separator.
But I think this is not really a problem, I have a working solution for delete button, but I still have the problem after saving a password.

If I have a String "1324" and I press "d" button I will have a String containing {'1', '2','3', '\0'}

Why are you wanting to ignore the last character?

The NULL is not saved in EEPROM.

Dude leave this, it works fine with my code. I have a bigger problem described above and I need to finish this project before this friday(15.11.'13), so if you like to help me pleace concentrate your skills over it.
Thank you for the effort!

I still cant find the problem :\

I still can't see your current code or your debug output.

This is my current code

#include <Keypad.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>

#define lockPin 2
const String newPasswordKey = "s*123#s";
const int errorDelayTime = 1500;
const byte ROWS = 4;
const byte COLS = 4;
char Keys[ROWS][COLS] = {
  {'1','2','3','c'},
  {'4','5','6','d'},
  {'7','8','9','a'},
  {'*','0','#','s'}
};

byte rowPins[ROWS] = {12, 11, 10, 9};
byte colPins[COLS] = {8, 7, 6, 5};

Keypad _keyboard = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS);

LiquidCrystal lcd(A5, A4, A3, A2, A1, A0);
String inputStr = "";
boolean checkStr = false;
byte currentCursorPosition = 0;
boolean makeNewPassword = false;

void setup(){
  lcd.begin(16, 2);
  pinMode(lockPin, OUTPUT);
}

void loop(){
  char button = _keyboard.getKey();

  if (button){
    if (button == 'c'){
      inputStr = "";
      makeNewPassword = false;
      lcd.clear();
    }
    else if (button == 'd'){
      if(inputStr.length()>0){
        byte currentPosition = inputStr.length()-1;
        inputStr.setCharAt(currentPosition, ' ');
        inputStr.trim();
        lcd.setCursor(currentPosition, 1);
        lcd.write(" ");
        lcd.setCursor(currentPosition, 1);
      }
    }
    else if (button == 'a'){
      checkStr = true;
    }
    else{
      inputStr += button;
      lcd.setCursor(inputStr.length()-1, 1);
      if(makeNewPassword){
        lcd.print(button);
      }
      else{
        lcd.print("*");
      }
    }
  }

  if(inputStr.length()>=4 && checkStr == true){
    int inputStrLen = inputStr.length();
    if (inputStr == newPasswordKey){
      lcd.clear(); 
      lcd.write("New Password:");
      lcd.setCursor(0,1);
      makeNewPassword = true;
    }
    else if(makeNewPassword == true){
      if(inputStr.indexOf('s')>-1){
        //ERROR
        //sound signal
        lcd.clear();
        lcd.print("forbidden character");
        delay(errorDelayTime);
        lcd.clear();
      }
      else{
        //read the new password and save it to the EEPROM
        for(int address = 0; address<1024; address++){
          if((EEPROM.read(address) == 0xFF) && (EEPROM.read(address+1) == 0xFF)){
            for(int Nchar=0; Nchar<inputStrLen; Nchar++){
              EEPROM.write(address+1+Nchar, inputStr[Nchar]);
            }
            lcd.clear();
            //sound signal
            lcd.print("PASSWORD SAVED");
            delay(errorDelayTime);
            lcd.clear();
            break;
          }
          if(address==1021){
            //No space for new passwords
            //sound signal
            lcd.clear();
            lcd.print("OUT OF MEMORY");
            delay(errorDelayTime);
            lcd.clear();
            break;
          }
        }
      }

      makeNewPassword == false;
    }

    else{
      //check passwords
      byte currentEEPROMCell;
      String EEPROMPassword = "";
      for(int readAddress=0; readAddress < 1024; readAddress++){
        currentEEPROMCell = EEPROM.read(readAddress);
        if(currentEEPROMCell!= 0xFF){
          EEPROMPassword += (char)currentEEPROMCell;
        }
        else{
          if(inputStr == EEPROMPassword){
            //sound signal
            lcd.clear();
            lcd.print("ACCESS GRANTED");
            digitalWrite(lockPin, HIGH);
            delay(2500);
            digitalWrite(lockPin, LOW);
            lcd.clear();
            break;
          }
          else if(EEPROM.read(readAddress+1) == 0xFF){
            //sound signal
            lcd.clear();
            lcd.print("ACCESS DENIED");
            delay(errorDelayTime);
            lcd.clear();
            break;
          }
          else{
            EEPROMPassword = "";
          }
        }
      }
      EEPROMPassword = "";
    }
    inputStr = "";
  }
  else if(checkStr == true){
    //String is too short
    lcd.clear();
    lcd.print("SHORT PASSWORD");
    delay(errorDelayTime);
    lcd.clear();
    inputStr = "";
  }

  checkStr = false;

}

What you mean 'debug output'?

What you mean 'debug output'?

I mean the output in the serial monitor from the Serial.print embedded in your code that show you where your code is going and what the states of key variables are.

You need to summarize what your problem is, too. You've not clearly defined why you are manipulating the array of characters entered. That manipulation seems to be the crux of your problem.

OMG I AM SOO DAMN STUPID!!!!
I found my problem I was trying to assign value FALSE to the bool variable makeNewPassword with '==' operator instead of '=' and I am really surprised that nobody s?w it. It is on row 110. :blush:
Thank you for the help and sorry for wasting your time!!!

What you mean 'debug output'?

The output from Serial.print commands placed in your code to show the value of variables at that point and proving that certain code blocks are actually being executed.

IT is working. Dont worry about it, now the only thing left is to make some sounds and replace the '//sound signal' comments with them :slight_smile: