0
Offline
Jr. Member
Karma: 0
Posts: 86
Arduino rocks
|
 |
« on: April 21, 2009, 02:10:58 pm » |
Hi...
I have an array, and I wish to take values from multiple cells and combine them into 1 value.
I obviously have the syntax incorrect, how can I concatenate the 3 values below into a single value...
Here is the sample code...
//create 1 number controlNumber = 0; controlNumber = (userInput[7],BYTE) && (userInput[6],BYTE) && (userInput[5],BYTE); Serial.print(controlNumber); // fetch the final results from the array
|
|
|
|
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #1 on: April 21, 2009, 02:24:47 pm » |
Why not use the good old addition?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 86
Arduino rocks
|
 |
« Reply #2 on: April 21, 2009, 02:26:36 pm » |
Just wanted to see if this could be done... Can you suggest a way to do this?
|
|
|
|
|
Logged
|
|
|
|
|
Connecticut, US
Offline
Edison Member
Karma: 1
Posts: 1036
Whatduino
|
 |
« Reply #3 on: April 21, 2009, 02:47:08 pm » |
What does the input array represent? Decimal digits in ASCII? Decimal digit values? Bytes that belong to a larger multi-byte value?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 86
Arduino rocks
|
 |
« Reply #4 on: April 21, 2009, 02:52:34 pm » |
decimal digit values...
But does it matter?
In that, I might want to "concatenate" other types as well - as long as the data types correspond.
|
|
|
|
|
Logged
|
|
|
|
|
Connecticut, US
Offline
Edison Member
Karma: 1
Posts: 1036
Whatduino
|
 |
« Reply #5 on: April 21, 2009, 03:02:14 pm » |
Well, there's a big difference in the operations that "concatenate" numbers. ASCII digits: int total = 0; for (int i = 0; i < number_of_digits; i++) total = total*10 + (digits[i] - '0'); Decimal digits: int total = 0; for (int i = 0; i < number_of_digits; i++) total = total*10 + digits[i]; Bytes, matching byte order: union { long total; byte a,b,c,d; }; a = bytes[0]; b = bytes[1]; c = bytes[2]; d = bytes[3]; Bytes, inverse byte order: union { long total; byte a,b,c,d; }; a = bytes[3]; b = bytes[2]; c = bytes[1]; d = bytes[0]; There are probably fifty ways I could code, depending on what you have. The original question is just too vague to be meaningful.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 86
Arduino rocks
|
 |
« Reply #6 on: April 21, 2009, 08:52:23 pm » |
Ok.. I have tried a lot of variations with the code examples you provided and could not get them to produce what I need. Here is more info. I have an array with 3 values: (userInput[5],BYTE) = 4; (userInput[6],BYTE) = 3; (userInput[7],BYTE) = 9;
int controNumber = 0;
I would like to assign the value of 439 to controlNumber.
If I have another array,
(userInput[5],BYTE) = 6; (userInput[6],BYTE) = 7; (userInput[7],BYTE) = '';
I would like to assign the value of 67 to controlNumber.
I hope that clarrifies things..
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 86
Arduino rocks
|
 |
« Reply #7 on: April 22, 2009, 01:11:29 pm » |
Hi... Haven't heard from anyone - wondering can this be done?
|
|
|
|
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #8 on: April 22, 2009, 02:14:17 pm » |
The code snippet from halley works fine. byte userInput[3] = { 4 , 3 , 9 };
void setup(){ Serial.begin(9600); int total = 0; for (int i = 0; i<3; i++){ total = total*10 + userInput; } Serial.println(total); }
void loop(){/*nothing*/}
This prints 439.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 86
Arduino rocks
|
 |
« Reply #9 on: April 22, 2009, 03:38:07 pm » |
Hi...
Thanks for verifying the code !!!...
I must have typed something incorrectly...
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 86
Arduino rocks
|
 |
« Reply #10 on: April 22, 2009, 04:01:09 pm » |
Ok... I've gotten 2 of the examples working and now am trying to see how the union one works... I typed in the following code but it did not result in any statements being printed... Any ideas?
//union ASCII digits: Code: void setup(){ byte userInput[4] = {'2','4','2','4'}; union { long total; byte a, b, c, d; }; a = userInput[0]; b = userInput[1]; c = userInput[2]; d = userInput[3]; Serial.println(total); Serial.println(total,BYTE); Serial.println(total,DEC); } void loop(){}
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 86
Arduino rocks
|
 |
« Reply #11 on: April 22, 2009, 04:07:48 pm » |
Also, come to think of it, how do I concatenate alpha characters...
I tried the following but it did not work..
//ASCII character digits: Code: byte userInput[8] = { '0', '0', '0', '0', '0', 'E' , 'B' , 'D' }; void setup(){ Serial.begin(9600); char total; for (int i = 5; i<8; i++){ total = total + (userInput); } Serial.println(total); } void loop(){}
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 86
Arduino rocks
|
 |
« Reply #12 on: April 22, 2009, 04:48:20 pm » |
Hi again... I found a bug with the process...
Since I am collecting data dynamically, I don't always get 3 digits, maybe 1, 2 or 3...
So, running this sample code, my output becomes : 132 and 430 when I should be getting: 132 and 43...
here is the code:
byte userInput[3] = {NULL,NULL,NULL};
int total = 0; void setup(){ Serial.begin(9600); userInput[1] = NULL; userInput[2] = NULL; userInput[3] = NULL; userInput[1] = 1; userInput[2] = 3; userInput[3] = 2; total = 0; for (int i = 1; i<4; i++){ total = total*10 + userInput; } Serial.println(total); userInput[1] = NULL; userInput[2] = NULL; userInput[3] = NULL; userInput[1] = 4; userInput[2] = 3; total = 0; for (int i = 1; i<4; i++){ total = total*10 + userInput; } Serial.println(total); }
void loop(){/*nothing*/}
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 86
Arduino rocks
|
 |
« Reply #13 on: April 22, 2009, 04:52:51 pm » |
ok, I think I handled that last problem by wrapping an if statement.
for (int i = 1; i<4; i++){ if (userInput != NULL){ total = total*10 + userInput; }
But, I still can't get the alpha concatenation to work...
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 13
Arduino rocks
|
 |
« Reply #14 on: October 28, 2009, 05:50:20 pm » |
One of my teammates figured it out. Apparently the timing is a little off, why it is, I am not sure. We added 4 to each of our default times and now it works properly.
|
|
|
|
|
Logged
|
|
|
|
|
|