Go Down

Topic: Inline println (Read 661 times) previous topic - next topic

Hawk_08

Hello,

I want to know if it is possible to print in array in the serial monitor in the form 0 0 0 0 0 0 for a 6 element array.

Currently I am using the following line of code. Combo_try[6] = {0,0,0,0,0,0}
Code: [Select]
Serial.println(combo_try[6]);

What I am getting is :
362
36
0รพ0
0
0
0
...

Thanks you

M.

Arrch

Post all of your code, not just snippets. Or better yet, post the minimum amount of code that (1) compiles and (2) demonstrates the problem.

btricha2

you need to post all of your code,

but one obvious thing to check, is to make sure the baud rate of your serial port, and the baud rate of the monitor match

WizenedEE


I want to know if it is possible to print in array in the serial monitor in the form 0 0 0 0 0 0 for a 6 element array.


No, the print class (like Serial) can't print arrays; just numbers and strings. You can easily make a loop though:
Code: [Select]

int ary[6];
Serial.print('[');
Serial.print(ary[0]);
for (int i=1; i<6; i++) {
 Serial.print(", ");
 Serial.print(ary[i]);
}
Serial.println(']');

tuxduino

Excellent code, ready to be put into a function... :)

Code: [Select]

int someList[6];

#define ARY_LEN(a) (sizeof(a)/sizeof(a[0]))

void printArray(int ary[], int numElements) {
    Serial.print('[');
    if (numElements > 0) {
        Serial.print(ary[0]);
        for (int i=1; i < numElements; i++) {
            Serial.print(", ");
            Serial.print(ary[i]);
        }
    }
    Serial.println(']');
}

void setup() {
    Serial.begin(115200);
    printArray(someList, ARY_LEN(someList));
}

void loop() {
}

AWOL

Code: [Select]
Serial.println(combo_try[6]);
It may by now be obvious to you, but a six element array doesn't have a subscript 6.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

tuxduino


Code: [Select]
Serial.println(combo_try[6]);
It may by now be obvious to you, but a six element array doesn't have a subscript 6.


He was probably trying to tell print() that the array had 6 elements.

Hawk_08

Thank you everyone,

It was exactly the information I was looking for!

M.

Hawk_08

I have two questions on this part of the code :
1. Is this used to calculate the size of the array with the position in memory?
2. What is 'a' is some general form to use when defining a variable or a relation?


Code: [Select]


#define ARY_LEN(a) (sizeof(a)/sizeof(a[0]))




Thx!

M.

Arrch


I have two questions on this part of the code :
1. Is this used to calculate the size of the array with the position in memory?


It's used to calculate the number of items in an array.

sizeof(a) returns the number of bytes that the array occupies. So for example int myArray[10] would occupy 20 bytes because an int occupies 2 bytes.
sizeof(a[0]) returns the size of the first element of the array, so in our int myArray[10] example, it would return 2. Meaning the entire macro returns 20 / 2 which is the number of elements in the array.

Code: [Select]
2. What is 'a' is some general form to use when defining a variable or a relation?
'a' is simple the replacing text in the macro. When you call the macro like this:

Code: [Select]
ARY_LEN(myArray)

The preprocessor converts it to this:

Code: [Select]
(sizeof(myArray)/sizeof(myArray[0]))

Go Up