Pages: [1]   Go Down
Author Topic: How to build a string from analogRead values  (Read 426 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello , i want to work with an analog keypad , im getting values with analogRead and my code understands which key is pressed so i can store it at
Code:
int value = analogRead(1);
. Thats ok , now lets say that value has the value 4 , and the next value will be 3 , i want to construct 43 ... i think its not possible with int type , so i have to use String or char , right ?

Thanks
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have to decide if you wanted is 43 (a numerical value), or "43", a string of chars.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have to decide if you wanted is 43 (a numerical value), or "43", a string of chars.


numeric value is the best for me !
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Think about how this might help:
Code:
    number = number * 10 + value;

-br
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A decimal 43 is actually quite difficult to decode later. A hex 43 is far easier to decode and can be constructed easily:

Code:
value = (value << 4) | (analogRead(1) & 0x0f);

This will retain just the lowest four bits of analogRead().
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 86
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Think about how this might help:
Code:
    number = number * 10 + value;

-br


how simple and great...

dhenry thank you too !
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12577
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have to decide if you wanted is 43 (a numerical value), or "43", a string of chars.


numeric value is the best for me !

In that case "How to build a string from analogRead values" is a highly misleading title.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: