Pages: [1]   Go Down
Author Topic: Inline println  (Read 582 times)
0 Members and 1 Guest are viewing this topic.
Quebec
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Serial.println(combo_try[6]);

What I am getting is :
362
36
0þ0
0
0
0
...

Thanks you

M.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int ary[6];
Serial.print('[');
Serial.print(ary[0]);
for (int i=1; i<6; i++) {
  Serial.print(", ");
  Serial.print(ary[i]);
}
Serial.println(']');
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Excellent code, ready to be put into a function... smiley

Code:
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() {
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26324
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Quebec
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you everyone,

It was exactly the information I was looking for!

M.
Logged

Quebec
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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


Thx!

M.
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
ARY_LEN(myArray)

The preprocessor converts it to this:

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

Pages: [1]   Go Up
Jump to: