Serial.print and keypad adding extra characters to a char array

Hi there everybody! Long time tinkerer, first time poster. So I apologize for any formatting errors or other things. I also know some folks prefer to see the full code and some will only want to see a certain code snippet. I am going to attach just the code snippet in body and the full code as an attachment. I am writing today because I am working on a program that is meant to do the following:
[Objectives]

  • Use a keypad to input a time that will then be stored and used for time intervals for cycling a motor
  • The time must be between 1 and 99 minutes, the program will just sit and wait that amount of time, then cycle the motor, then wait again
  • Store the time to a char array and then convert that to an integer using atoi
  • Allow users to overwrite the input and reset if they enter something wrong only when initially entering the time, not after the time is entered. I currently am just using a delay for cycle times and am aware the program cannot listen for interrupts or anything

When users are entering their time, they can push a button, the program will store that keypress into the char array, and increase the index of the char array. Then the user can either press * to confirm, # to reset, or another number and the program will write that new input into the second position of the char array.

Each time, it is supposed to show the user the new value of char array via Serial.print so the user can visually confirm that their input was received.

Eventually I’ll be using an LCD but am just using Serial while I wait on a part. And lately I have been getting some strange displays for the char array. You can see these strange ones in the attachment.

Here is the code that is running this function of gathering input.

void setTimer() {
//Serial.setCursor(0,0);
if(arrIndex == 0) {
Serial.println(“Enter time between cycles, in minutes between 1 and 99:”);
delay(1000);
}
else{
Serial.println(“Enter next digit or press * to confirm or # to reset”);
}

while (customKey == NO_KEY){
customKey = customKeypad.getKey();
delay(50);
if(customKey == ‘*’) {
//Serial.clear;
// Serial.setCursor(0, 0);
Serial.println(“Input confirmed, a 1 minute timer will countdown before the first cycle is started.”);
Serial.print(“The time you entered was: “);
Serial.print(userInput);
Serial.println(” minutes. If this is not correct, press pound(#)”);
delay(1000);
timeBetweenCycles = atoi(userInput);
Serial.println(timeBetweenCycles);
delay(2000);
// countdownTimer();
// convertedTime = userInput * 60 * 1000; //gets us from minutes to milliseconds for the delay function
timeSet = 1;

// return;
}

else if(customKey == ‘#’) {
//Serial.clear;
//Serial.setCursor(0, 0);
Serial.println(“Resetting…”);
delay(500);
for (arrIndex = 0; arrIndex < 2; arrIndex++) {
userInput[arrIndex] = 0;
}
delay(1000);
arrIndex = 0;
timeSet = 0;
// return;
}

else if (customKey){
Serial.print("The key pushed was: ");
Serial.println(customKey);
delay(1000);
if(arrIndex < 2) {
userInput[arrIndex] = customKey;
Serial.print("The array index is: ");
Serial.println (arrIndex);
Serial.print(“Input received, “);
Serial.print(userInput);
Serial.println(” press * to confirm or # to reset”);
delay(1000);
arrIndex = arrIndex + 1;
}
else {
for (arrIndex = 0; arrIndex < 2; arrIndex++) {
userInput[arrIndex] = 0;
}
Serial.println(“Length of input is greater than allowed, resetting…”);
delay(1000);
arrIndex = 0;
timeSet = 0;
//return;
}
}
else {
//Serial.println(“You did not put in an accepted value”);
timeSet = 0;
// setTimer();
}

}
customKey = NO_KEY;
}

timerSketchv3_with_keypad_input.ino (5.02 KB)

A first time poster should read the sticky threads at the top of the forum. That would make apologies for incorrect formatting unnecessary. Please eschew your defiance of the forum protocols. It will discourage many knowledgeable people from helping you.

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The “Code: [Select]” feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

When you first enter your function setTimer(), you do not clear out the userInput array, so who knows what is in there. Try setting the entire array to 0 like you do with a reset.

You also don't set customKey before your while() statement so who knows if that condition will be true or not.

Thank you aarg! I saw that right as I posted and couldn't find a way to edit my post. I apologize!

And thank you blh64, that fixed it!

sdiller:
Thank you aarg! I saw that right as I posted and couldn’t find a way to edit my post. I apologize!

You still haven’t used code tags. Those are quote tags.