Looped Array to print as csv

Hi everyone,
I’m trying to work out how I can serial print an int array in csv format.
Can someone lead me in the right direction for an easy way to do this?

Demo Code:

int Numbers[8] = {34, 35, 36, 37, 38, 39, 40, 41};

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
      for (int i = 0; i < 8; i++)
      {
        Serial.println(Numbers[i]);
      }
      delay(500);
}

Need it to print like this: 34, 35, 36, 37, 38, 39, 40, 41

Thanks in advance

You'll need one of these "Serial.print (',');"

TolpuddleSartre: You'll need one of these "Serial.print (',');"

And, you need to NOT use println().

Alrigh, what’s the problem? If you really want comma seperated instead of new line seperated you just do:

#define numberof(x) (sizeof(x)/sizeof(x[0]))

int numbers[8] = {34, 35, 36, 37, 38, 39, 40, 41}; //any reason for it being int instead of byte?

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  for (byte i = 0; i < numberof(numbers); i++)
  {
    Serial.print(numbers[i]);
    //if not last number, add a comma
    if(i != (numberof(numbers) - 1))
    {
      Serial.print(",");
    }
    //and a new line after all numbers`
    else
    {
      Serial.println();
    }
  }
  delay(1000);
}
    if(i = (numberof(numbers) - 1))

Some people would use <. YMMV.

Uhmmm, yeah. A ! disappeared :confused: You can do it with < as well, just think != represents what it does in plain text / the comment better.

septillion:
Alrigh, what’s the problem? If you really want comma seperated instead of new line seperated you just do:

#define numberof(x) (sizeof(x)/sizeof(x[0]))

int numbers[8] = {34, 35, 36, 37, 38, 39, 40, 41}; //any reason for it being int instead of byte?

void setup()
{
 Serial.begin(9600);
}

void loop()
{
 for (byte i = 0; i < numberof(numbers); i++)
 {
   Serial.print(numbers[i]);
   //if not last number, add a comma
   if(i != (numberof(numbers) - 1))
   {
     Serial.print(",");
   }
   //and a new line after all numbers`
   else
   {
     Serial.println();
   }
 }
 delay(1000);
}

I can see where this is going & its what I need thanks.
But its only printing:
34, 34, 34, 34, etc

PS. still trying to get my head around byte & bits etc

All good, worked out where "!" was missing. Cheers bud

Hi Again,
How would i do this for a multidimensional array?
I have this:

  for (int i = 0; i < 8; i++)
  {
  if (AlarmAddress[1][i] == 1)
    Serial.print(AlarmAddress[0][i]);
        if(i != 8 - 1)
        {
          Serial.print(",");
        }
        else
        {
          Serial.println();
        }
  }

septillion:
Alrigh, what’s the problem? If you really want comma seperated instead of new line seperated you just do:

#define numberof(x) (sizeof(x)/sizeof(x[0]))

int numbers[8] = {34, 35, 36, 37, 38, 39, 40, 41}; //any reason for it being int instead of byte?

void setup()
{
 Serial.begin(9600);
}

void loop()
{
 for (byte i = 0; i < numberof(numbers); i++)
 {
   Serial.print(numbers[i]);
   //if not last number, add a comma
   if(i != (numberof(numbers) - 1))
   {
     Serial.print(",");
   }
   //and a new line after all numbers`
   else
   {
     Serial.println();
   }
 }
 delay(1000);
}

Thanks Again

How would i do this for a multidimensional array? I have this:

Do you want the whole array on one line, with commas? Or should each row be on a separate line?

PaulS: Do you want the whole array on one line, with commas? Or should each row be on a separate line?

The whole array on one line would be good. Thanks PaulS

You are using the value in one row to determine whether to print the value in the other row. You are not printing both rows.

You will need to make two passes through the array, counting the number of values to print on the first pass, and then printing them on the second pass.

For the second pass, replace the 8 with the number of ones counted on the first pass.

PaulS:
You are using the value in one row to determine whether to print the value in the other row. You are not printing both rows.

You will need to make two passes through the array, counting the number of values to print on the first pass, and then printing them on the second pass.

For the second pass, replace the 8 with the number of ones counted on the first pass.

Im about to test it just have to rig up, is this what you mean?

  for (int i = 0; i < 8; i++)
  {    
  if (AlarmAddress[1][i] == 1)
    for (int j = 0; j < i; j++)
    {
    Serial.print(AlarmAddress[0][i]);
        if(j != i - 1)
        {
          Serial.print(",");
        }
        else
        {
          Serial.println();
        }
     }
  }

Nope that wasn't it! I'm a little lost sorry, I keep thinking of storing the filtered results from the first pass in another array & calling that in a second for loop, but there has to be a better way I,m not seeing.

int anArray[2][3] = {{12, 13, 14}, {15, 16, 17}};

void setup()
{
  Serial.begin(115200);
  byte bytesPerEntry = sizeof(anArray[0][0]);
  byte cols = (sizeof(anArray[0]) / bytesPerEntry);
  Serial.print("cols : ");
  Serial.println(cols);
  byte rows = ((sizeof(anArray) / cols) / bytesPerEntry);
  Serial.print("rows : ");
  Serial.println(rows);
  for (int r = 0; r < rows ; r++)
  {
    for (int c = 0; c < cols; c++)
    {
      Serial.print(anArray[r][c]);
      Serial.print(",");
    }
  }
}

void loop()
{
}

Thanks UKHeliBob

It works well & easyish enough (for me) to understand.
I’m trying to filter out the array using a column that’s either 1 or 0 when I apply a filtering if statement to it i cant get the commas right this the best I came up with:
Has your code modified:

  int anArray[3][2] = {{12, 1}, {14, 0}, {16, 1}};

  byte bytesPerEntry = sizeof(anArray[0][0]);
  byte cols = (sizeof(anArray[0]) / bytesPerEntry);
  byte rows = ((sizeof(anArray) / cols) / bytesPerEntry);
  byte count = 0;
void setup()

{
  Serial.begin(115200);
}
void loop() 
{
  for (int r = 0; r < rows ; r++)
  {
    if (anArray[r][1] == 1)
    {
      count++;
      Serial.print(anArray[r][0]);
      if (r <= count)
      {
        Serial.print(",");
      }

      if (r == rows - 1)
      {
        count = 0;
      }
    }
  }
  Serial.println();
delay(500);
}

I'm trying to filter out the array using a column that's either 1 or 0

Do you mean that if the entry is either 0 or 1 then don't output it or something else ?

Yeh I use this array with 2 columns first column is the pin number of something I’m monitoring the second is the alarm state of that pin. If an alarm is triggered the second column will change from 0 to 1 & the first column defines where the issue is (pin number). This is the filter:

if (anArray[r][1] ==1);

spark_noob: This is the filter:

if (anArray[r][1] ==1);

Nope.

This is without any function. (if something do nothing)

Whandall: Nope.

This is without any function. (if something do nothing)

My bad was writing from phone

if (anArray[r][1] ==1)