OK
It appears that I cannot put a real “void loop ()” loop in Setup after all, but I can do a loop by other means. The code in post#8 works but I cannot use it. It requires me to specify the number of numbers, which re-incurs the problem, and I don’t know what to do with the output anyway.
I can start a “while” from Setup, but I cannot get out of it. I was looking for a key “#” to do this. The result is what I want, an int, and It can be any number of digits. The
inputString.reserve(4);
line is ineffective, although I think it is in the right position. I’m not quibbling about this, the result at toInt is all I need.
#include <Keypad.h>
const byte numRows= 4;
const byte numCols= 3;
char keymap[numRows][numCols]={
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[numRows] = {33,43,41,37};
byte colPins[numCols]= {35, 31, 39};
Keypad keypad = Keypad( makeKeymap(keymap), rowPins, colPins, numRows, numCols );
char key = "";
String inputString;
int inputInt;
int numShots;
void setup() {
Serial.begin(9600);
Serial.println("Starting");
inputString.reserve(4); // maximum number of digit for a number is 10, change if needed
while (key !='#') {
NumIN();
}
Serial.println(inputInt);
numShots = inputInt;
Serial.print("numShots ln27 = ");
Serial.print(numShots);
}
void loop() {
Serial.println(inputInt);
Serial.print("numShots ln33= ");
Serial.print(numShots);
delay(5000);
}
void NumIN() {
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key >= '0' && key <= '9') { // only act on numeric keys
inputString += key; // append new character to input string
} else if (key == '#') {
if (inputString.length() > 0) {
inputInt = inputString.toInt(); // YOU GOT AN INTEGER NUMBER
inputString = ""; // clear input
Serial.print("inputInt in srtn ln50 ");
Serial.println(inputInt);//This is OK to here
}
} else if (key == '*') {
inputString = ""; // clear input
}
}
}
I guess all I need is an elegant exit, but I certainly need it. While a switch case in the loop should work, I now realise it’s a really bad idea.