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)
