I've spent the last few hours thrawling through this forum and the greater internet and can't for the life of me find anything quite like what I need to do, so my sincere apologies if there's a thread for this already! Also, my apologies in advance, I'm both a programming and forum n00b so this could be painful for you guys... Sorry!
I have an integer array of the form num[6] = {0,1,2,3,4,5}; and I need to change it to one integer of value num = 12345;. I've tried everything I could find, from concat() to itoa() and I keep getting errors saying that they're integers and not constant characters.
Here's my code (note that i only need the first 6 values from the "str" array and the "-48" is to change the value to from the ASCII character value to the real number, i.e. 50 is actually 2):
int str[12];
int num[6];
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
for (int i = 0; i<12; i++){
str[i] = Serial.read();
}
for (int i = 0; i<6; i++){
num[i] = (str[i] - 48);
Serial.print(num[i]);
}
Serial.println();
}
delay(500);
}
Please let me know if there's anything else you need from me!
You also have to get your array indices correct - num[1] is not the first element in an array.
But doing it that way would be far less efficient than the method PaulS is suggesting as that way you only do one multiply by 10 each loop rather than an increasing number of times each loop as the pow() function would do. Using an int as a for loop index when it only goes up to five is also very inefficient.
unsigned long number=num[0];
for (byte i=1;i<6;i++)
{
number = number*10+num[i];
}
Note that it has to be an unsigned long as you want 6 base10 digits.