Go Down

Topic: Setting a 5 digit count using 4x4 matrix keypad & the Arduino Uno (Read 2338 times)previous topic - next topic

GautamD #30
Oct 06, 2017, 09:42 am
A slight correction from:

Code: [Select]

total = ((count[ 5 ] - '0')  + (count[ 4 ] - '0') * 10 + (count[ 3 ] - '0') * 100 + (count - '0') * 1000 + (count - '0') * 10000UL);

to:

Code: [Select]

total = ((count[ 5 ] - '0')  + (count[ 4 ] - '0') * 10 + (count[ 3 ] - '0') * 100 + (count - '0') * 1000 + (count - '0') * 10000UL);

6v6gt #31
Oct 06, 2017, 09:52 am
There is no problem with atoi() and numbers which are bigger would fit in an int (max 65,536) if you follow the same structure:
Code: [Select]

total = . . . + . . . + atoi( count ) * 10000UL);

GautamD #32
Oct 06, 2017, 10:18 am
Suppose I want to enter a count 99,999 then using:

Code: [Select]

total = ((count[ 5 ] - '0')  + (count[ 4 ] - '0') * 10 + (count[ 3 ] - '0') * 100 + (count - '0') * 1000 + (count - '0') * 10000UL);

It would mean:

count = 9
count = 9*10
count = 9*100
count = 9*1000
count = 9*10000UL

I am right?

Gautam

6v6gt #33
Oct 06, 2017, 11:10 am
Sort of.

Except that I now see there is an error in that count should go from 0 to 4, not 1 to 5 so you should change the code accordingly.

It is like if you pressed the '9' key 5 times on a pocket calculator.
The first press give the number of 10,000's, the second press gives the number of 1,000's etc. etc.

The complete number, that is, 99,999 is an integer which can be used in the following calculation.

GautamD #34
Oct 06, 2017, 12:15 pm
Should be this way? Using this I have an int now? And don't need to use atol()?

Code: [Select]

total = ((count[ 0 ] - '0') * 10000UL  + (count[ 1 ] - '0') * 1000 + (count[ 2 ] - '0') * 100 + (count - '0') * 10 + (count - '0'));

What is PaulS trying to say here:

Correct. But, there are assumptions in that code that atol() does not make.

6v6gt #35
Oct 06, 2017, 01:06 pm
Using atoi() would be even better as I said in post #28 part 2 and presumably for the same reason that Pauls recommended it.

Code: [Select]

total = ((count[ 0 ] - '0') * 10000UL  + (count[ 1 ] - '0') * 1000 + (count[ 2 ] - '0') * 100 + (count - '0') * 10 + (count - '0'));

you could write:
Code: [Select]

total = ( atoi(count) * 10000UL  + atoi(count) * 1000 + atoi(count)  * 100 + atoi(count)  * 10 + atoi(count) );

If the data is clean, both methods will work fine.

GautamD #36
Oct 06, 2017, 01:13 pm
what do you mean by " if data is clean?"

both methods?

AWOL #37
Oct 06, 2017, 01:44 pm
Quote
what do you mean by " if data is clean?"
That the data is a valid decimal digit.

GautamD #38
Oct 06, 2017, 01:59 pm
Thanks @AWOL , the data is going to be clean.

GautamD #39
Oct 06, 2017, 02:03 pm
So after using the following method, 'total' is an now an unsigned long int and can be saved in the EEPROM?

Code: [Select]

total = ( atoi(count) * 10000UL  + atoi(count) * 1000 + atoi(count)  * 100 + atoi(count)  * 10 + atoi(count) );

AWOL #40
Oct 06, 2017, 02:06 pm
Assuming count is a two dimensional array of strings containing single digit strings, yes.

GautamD #41
Oct 06, 2017, 02:14 pm
Okay. Currently as I enter the digits for the count they are displayed from left to right. How to display them   from right to left?

PaulS #42
Oct 06, 2017, 03:08 pm
If count is an array of chars, capable of holding 6 elements, and contains values like so:

Code: [Select]
count = '3';
count = '5';
count = '0';
count = '0';
count = '7';
count = '\0'; // A NULL terminator

then

Code: [Select]
unsigned long val = atol(count);

would result in val containing 35007.

If count contained a 'E', then val would contain 350.

With the code you have been trying, val would contain some meaningless garbage if any element in the array was not a digit.
The art of getting good answers lies in asking good questions.

GautamD #43
Oct 06, 2017, 07:30 pm
If count is an array of chars, capable of holding 6 elements, and contains values like so:

Code: [Select]
count = '3';
count = '5';
count = '0';
count = '0';
count = '7';
count = '\0'; // A NULL terminator

then

Code: [Select]
unsigned long val = atol(count);

would result in val containing 35007.

If count contained a 'E', then val would contain 350.

With the code you have been trying, val would contain some meaningless garbage if any element in the array was not a digit.
@PaulS I want to implement it the way you are suggesting. I want to obtain a count which can be any number between 0 to 99,999 and then storing it in the Arduino EEPROM for comparison. What changes I shall make to my current code since you say that my current code is not helping me achieve what I want. Thanks.

Gautam

PaulS #44
Oct 09, 2017, 12:26 pm
Quote
What changes I shall make to my current code since you say that my current code is not helping me achieve what I want.
You want to get from point A (what the code does now) to point B (the code does what you want it to do). I have no ideal where point A is (you have not said what you current code looks like OR what it actually does) or where point B is (I do not understand what you are trying to actually store in EEPROM).

I can't help you get from point A to point B.
The art of getting good answers lies in asking good questions.

Go Up