I am new at coding, is there a way to convert the strings you get from a keypad into numbers. I need the keypad to give me a number representing a time, then translate that into

``````delay(TIME * 1000);
``````

which would be set to a pin. This is probably easy, but I'm new. thanks.

Could I use .toInt()? if so, how would I set it up in code?

There might be an example in the keypad library. Have you installed the library and got any of the examples to work with your hardware?

.toInt() won't help. That useful for Strings but capital-S Strings are virtually useless on an Arduino.

You need to think about the problem and describe it in the tiniest detail. How many digits do you want to enter? More than one? Less than five? How do you 'start' an entry and how does the Arduino know when you've finished? Conventionally, a keypad with * and # keys uses the * as start and # as end but you can use anything you want.

Each digit that you receive from the keypad is actually a character representing the number. So, if you enter 5 you actually receive '5'. If you subtract '0' from each digit as received then the result will be the real number.

So how do you deal with multi digit numbers ?
Something like this

``````initialise the target number to zero
while the received character is between '0' and '9'
multiply the target number by 10
subtract '0' from the digit received
add the result to the target number
end of while  //end of number key received
``````

I think I've gotten past this part, but I'll ask anyway.
in examples I've seen, a row in the matrix is coded as {'1', '2', '3'}; could I just set it as {1, 2, 3}; to have it as a number to start with, or would this not work?

in examples I've seen, a row in the matrix is coded as {'1', '2', '3'}; could I just set it as {1, 2, 3}; to have it as a number to start with, or would this not work?

Yes, you could assign the values to be returned to be 1, 2, 3, etc.

You STILL need to deal with the user wanting to enter 12 or 452.

PaulS:
You STILL need to deal with the user wanting to enter 12 or 452.

I'm not quite sure I get what you are saying, stop ME from entering 12, or stop the code from thinking I put 12?

I'm not quite sure I get what you are saying, stop ME from entering 12, or stop the code from thinking I put 12?

Neither.

What is the purpose of using the keypad to enter numbers? Are you ONLY concerned about single digit numbers? The implication in your first post is that you do need to be able to enter multi-digit numbers, like 12 or 452.

If that is not the case, forget I said anything amount multi-digit numbers. It it IS the case, then you need to deal with multi-digit numbers, like 12 or 452.

How you do that using int values or char values is not much different.

The purpose is to fill 4 variables that take a single digit, then combine them. What I have right now is;

`````` if (count <= 4) {
char key = kpd.waitForKey();
if(key){{ // Check for a valid key.

digitalWrite(ledpin, HIGH);
delay(1000*key);
digitalWrite(ledpin, LOW);

count = count + 1;
}
if (count == 1) {
tens = tens*key ;}
if (count == 2) {
ones = ones*key ;}
if (count == 3) {
tenths = tenths*key ;}
if (count == 4){
hundredths = hundredths*key;
}
}
Time = tens + ones + tenths + hundredths ; }
``````

count is initially set to 0 before void setup, the delay and blink is so I know what key was pressed. because I do not need '#' or '*', I've ignored addressing them.
From this, how could I make sure that each input is only 1 digit. Also, for some reason, it accepts 5 numbers instead of 4.

``````  if(key){{ // Check for a valid key.
``````

Why are there 2 { there?

From this, how could I make sure that each input is only 1 digit.

Any given key will only return a single value, so this is already handled.

Also, for some reason, it accepts 5 numbers instead of 4.

Because the initial if statement in that snippet executes the body of the statement when count is 0, 1, 2, 3, and 4. Even with my shoes on, I can see that that is 5 values.

The extra { is to account for a small bit of code at the end about a button.

PaulS:
Because the initial if statement in that snippet executes the body of the statement when count is 0, 1, 2, 3, and 4. Even with my shoes on, I can see that that is 5 values.

Ya, I realized this about 5 minutes ago. I forgot it took a number at value 0 as well. Thank you, It now works as intended.

Actually, the 0 is not registering. is there a reason?

JTMMcBroom:
Actually, the 0 is not registering. is there a reason?

The problem is almost certainly in the code that you have not posted.

So the zero is not working, and it's not hardware, as I just ran a program to test each key.

``````const int start = 2;
const int ledpin = 13;
const int motorcontrol = 4;
int count = 0;
int tens = 10;
int ones = 1;
float tenths = .1;
float hundredths = .01;
float Time = 0;
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{1,2,3},
{4,5,6},
{7,8,9},
{'#',0,'*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 };

void setup() {
pinMode(ledpin, OUTPUT);
pinMode(start, INPUT);
Serial.begin(9600);
}

void loop() {
if (count < 4) {
char key = kpd.waitForKey();
if(key){{ // Check for a valid key.

digitalWrite(ledpin, HIGH);
delay(1000*key);
digitalWrite(ledpin, LOW);

count = count + 1;
}
if (count == 1) {
tens = tens*key ;}
if (count == 2) {
ones = ones*key ;}
if (count == 3) {
tenths = tenths*key ;}
if (count == 4){
hundredths = hundredths*key;

}
}
Time = tens + ones + tenths + hundredths ; }
else {
digitalWrite(ledpin, LOW);}
if (buttonstate == HIGH && count < 5) {
digitalWrite(ledpin, HIGH);
delay(1000*Time);

}
else {
digitalWrite(ledpin, LOW);
}
}
``````

The point of the keypad is to assign numbers to each of the 4 place values. 0 does not work, nor does it take up one of the 4 slots. Every other number works and the program runs successfully.

What do you think `if(key)` does?

It may help to read: constants - Arduino Reference

It says if a valid key from the matrix is pressed, do whatever is below. 0 is a valid key (or should be), and it is a number, so shouldn"t it work?

I see that you took no action about the first comment in reply #8. You also ignored my request to format your code with ctrl-T.

There was a part of the code I did not copy because it had not pertained to the question. and can you please guide me to where you suggested ctrl-T, and if you didn't there, explain what you are talking about. I'm new to this, so I may not get what you are talking about, sorry.

When the sketch is sitting in the IDE press CTRL T.

.

JTMMcBroom:
There was a part of the code I did not copy because it had not pertained to the question. and can you please guide me to where you suggested ctrl-T, and if you didn't there, explain what you are talking about. I'm new to this, so I may not get what you are talking about, sorry.

In the IDE menu, it's Tools->Auto Format.

OK, new code

``````const int start = 2;
const int ledpin = 13;
const int motorcontrol = 4;
int count = 0;
int tens = 10;
int ones = 1;
float tenths = .1;
float hundredths = .01;
float Time = 0;
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{1,2,3},
{4,5,6},
{7,8,9},
{'#',0,'*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 };

void setup() {
pinMode(ledpin, OUTPUT);
pinMode(start, INPUT);
Serial.begin(9600);
}

void loop() {
if (count < 4) {
char key = kpd.waitForKey();
if(key){{ // Check for a valid key.

digitalWrite(ledpin, HIGH);
delay(1000*key);
digitalWrite(ledpin, LOW);

count = count + 1;
}
if (count == 1) {
tens = tens*key ;}
if (count == 2) {
ones = ones*key ;}
if (count == 3) {
tenths = tenths*key ;}
if (count == 4){
hundredths = hundredths*key;

}
}
Time = tens + ones + tenths + hundredths ; }
else {
digitalWrite(ledpin, LOW);}
if (buttonstate == HIGH && count < 5) {
digitalWrite(ledpin, HIGH);
delay(1000*Time);

}
else {
digitalWrite(ledpin, LOW);
}
}
``````

it still does not read a 0 from the keypad. I don't get it, why would it read every other number, but not 0?