arduino serial communication

Hi All,

I’m having problem with my attached code. The attached code will collect serial data from arduino.

The problems are:

  1. I have set the loop to collect 250 data points from each analog inputs, but during the start of program the code collects 500 data points for each channel. Why this happens?

  2. In serial monitor the data is printed as
    14 , 1023
    15 , 927
    16 , 253
    17 , 132
    Is it possible to print the above data as 14 , 1023 , 15 , 927 , 16 , 253 , 17 , 132.

Any help would be appreciated.

Thanks in advance:)

test.ino (531 Bytes)

reva23:

  1. I have set the loop to collect 250 data points from each analog inputs, but during the start of program the code collects 500 data points for each channel. Why this happens?

I don’t think for(int pin = A0; pin < A4; pin++) is a valid arduino-C language construction. But let’s assume this isn’t an issue.

What do you mean by “the code collects 500 data points”? The for loop should run exactly 250 times for each pin so you should see something like:

, value[0]
, value[1]

, value[249]
, value[0]
, value[1]

, value[249]


, value[0]
, value[1]

, value[249]

reva23:
2) In serial monitor the data is printed as
14 , 1023
15 , 927
16 , 253
17 , 132
Is it possible to print the above data as 14 , 1023 , 15 , 927 , 16 , 253 , 17 , 132.

Just substitute Serial.println(value_) with the 2 lines: Serial.print(value*), Serial.print(" ");*_

Sorry, the program prints 500 data points for each channel during the initial startup.

Also I believe the for loop runs for every 1s. But how can I ensure the same is happening or not. can we print the loop execution timing?

This program prints all channel data for every 1s how can I change this arduino code such that the program collects data from each channel for every 1s

reva23:

  1. I have set the loop to collect 250 data points from each analog inputs, but during the start of program the code collects 500 data points for each channel. Why this happens?

The first for-loop will display 4x250 value pairs (pin plus value); I counted them :slight_smile: As expected.

reva23:
Also I believe the for loop runs for every 1s. But how can I ensure the same is happening or not. can we print the loop execution timing?

It does not. You can use the following approach:

void loop()
{
  static unsigned long lastMillis = millis();
  unsigned long currentMillis;
  for (int pin = A0; pin < A4; pin++)
  {
    for (int i = 0; i < 250; i++)
    {
      value[i] = analogRead(pin);
      Serial.print(pin);
      Serial.print(", ");
      Serial.print(value[i]);
      Serial.print(", ");
    }
  }

  // measure and display the time spend in the loop
  currentMillis = millis();
  unsigned long readduration = currentMillis - lastMillis;
  Serial.println(readduration);

  // delay for demo
  delay(5000);
}

It takes roughly 9 seconds due to the print/println functions; without it, roughly 100ms for the 1000 readings.

reva23:
This program prints all channel data for every 1s how can I change this arduino code such that the program collects data from each channel for every 1s

Be aware that you will exceed the memory if your read 4 channels with 250 readings per channel; 4 * 250 * sizeof(int) equals 2000 bytes which does not leave you much / enough for other stuff like serial print. So either reduce your number of readings or read them e.g one channel during one second and send that data, next the next channel and send that data etc.

You also need to take into account the duration of the serial communication; you probably want to increase the baudrate.

You can use a two-dimensional array to store your readings. The below code does 100 readings per channel (and stores the result in a 4x100 array) and delays for 5 seconds after each reading is completed and send to a connected device (computer).

#define MAXREADINGS 100

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

  Serial.print("A0: ");
  Serial.println(A0);
  Serial.print("A1: ");
  Serial.println(A1);
}

int values[4][MAXREADINGS];
void loop()
{

  static unsigned long lastMillis = 0;
  unsigned long currentMillis = millis();

  if (currentMillis - lastMillis > 5000)
  {
    for (int pin = A0; pin < A4; pin++)
    {
      for (int i = 0; i < MAXREADINGS; i++)
      {
        values[pin - A0][i] = analogRead(pin);
        //values[pin - A0][i] = i;
      }
    }

    // measure and display the time spend in the loop
    currentMillis = millis();
    unsigned long readduration = currentMillis - lastMillis;
    Serial.print(F("Read duration: "));
    Serial.println(readduration);


    for (int pin = A0; pin < A4; pin++)
    {
      for (int i = 0; i < MAXREADINGS; i++)
      {
        Serial.print(pin);
        Serial.print(", ");
        Serial.print(values[pin - A0][i]);
        Serial.print(", ");
      }
      Serial.println("\n=========\n");
    }

    unsigned long printduration = millis() - currentMillis;
    Serial.print(F("Print duration: "));
    Serial.println(printduration);

// FOR YOU TO DECIDE
    lastMillis = currentMillis();   // does not take into account the time spend in the serial comms
    lastMillis = millis();          // does take into account the time spend in the serial comms
  }
}

I hope this gets you in the way.

ElusivePi:
I don’t think for(int pin = A0; pin < A4; pin++) is a valid arduino-C language construction. But let’s assume this isn’t an issue.

It’s fully valid and does what is expected :wink:

Can anyone tel me the solution for this question?

In serial monitor the data is printed as 14 , 1023 15 , 927 16 , 253 17 , 132 Is it possible to print the above data as 14 , 1023 , 15 , 927 , 16 , 253 , 17 , 132.

Yes. Don't use Serial.println() until you get to the end of the line.

You can use Serial.println(); on a line on its own to cause a new line.

...R

reva23:
Can anyone tel me the solution for this question?

In serial monitor the data is printed as
14 , 1023
15 , 927
16 , 253
17 , 132
Is it possible to print the above data as 14 , 1023 , 15 , 927 , 16 , 253 , 17 , 132.

That answer was already given in post #2 :wink: