Go Down

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

GautamD

A slight correction from:

Code: [Select]

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



to:

Code: [Select]

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

6v6gt

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[1] ) * 10000UL);

GautamD

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[2] - '0') * 1000 + (count[1] - '0') * 10000UL);


It would mean:

count[5] = 9
count[4] = 9*10
count[3] = 9*100
count[2] = 9*1000
count[1] = 9*10000UL

I am right?

Gautam

6v6gt

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

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[3] - '0') * 10 + (count[4] - '0'));



What is PaulS trying to say here:

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

6v6gt

Using atoi() would be even better as I said in post #28 part 2 and presumably for the same reason that Pauls recommended it.

So instead of:
Code: [Select]

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




you could write:
Code: [Select]

total = ( atoi(count[0]) * 10000UL  + atoi(count[1]) * 1000 + atoi(count[2])  * 100 + atoi(count[3])  * 10 + atoi(count[4]) );



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


GautamD

what do you mean by " if data is clean?"


both methods?

AWOL

Quote
what do you mean by " if data is clean?"
That the data is a valid decimal digit.

GautamD


GautamD

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[0]) * 10000UL  + atoi(count[1]) * 1000 + atoi(count[2])  * 100 + atoi(count[3])  * 10 + atoi(count[4]) );

AWOL

Assuming count is a two dimensional array of strings containing single digit strings, yes.

GautamD

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

If count is an array of chars, capable of holding 6 elements, and contains values like so:

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


then

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

would result in val containing 35007.

If count[3] 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

If count is an array of chars, capable of holding 6 elements, and contains values like so:

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


then

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

would result in val containing 35007.

If count[3] 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

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