Pages: [1]   Go Down
Author Topic: simple concatenation  (Read 2829 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 87
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not use the good old addition?
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 87
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just wanted to see if this could be done... Can you suggest a way to do this?
Logged

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Jr. Member
**
Karma: 0
Posts: 87
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, there's a big difference in the operations that "concatenate" numbers.

ASCII digits:
Code:
int total = 0;
for (int i = 0; i < number_of_digits; i++)
   total = total*10 + (digits[i] - '0');

Decimal digits:
Code:
int total = 0;
for (int i = 0; i < number_of_digits; i++)
   total = total*10 + digits[i];

Bytes, matching byte order:
Code:
union { long total; byte a,b,c,d; };
a = bytes[0];
b = bytes[1];
c = bytes[2];
d = bytes[3];

Bytes, inverse byte order:
Code:
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 Offline
Jr. Member
**
Karma: 0
Posts: 87
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Jr. Member
**
Karma: 0
Posts: 87
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi... Haven't heard from anyone - wondering can this be done?
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The code snippet from halley works fine.

Quote

 
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 Offline
Jr. Member
**
Karma: 0
Posts: 87
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi...

Thanks for verifying the code !!!...


I must have typed something incorrectly...
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 87
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Jr. Member
**
Karma: 0
Posts: 87
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Jr. Member
**
Karma: 0
Posts: 87
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Jr. Member
**
Karma: 0
Posts: 87
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: