Problems integrating two signals.

I am very new to Arduino and have no previous programming experience whatsoever.
I would like to;
Get two signals, from Analog Ports 0 and 1 (which I have wired), multiply them (one signal per port per reading time) and then take the sum of the products over a certain interval (integrate the product), and then Serial.println the integral(s).

Here is what I have;

int An[2];
const int SZ = 100;
float buffer[SZ];
int pos = 0;
void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
}

void loop() {
  An[0] = analogRead(A0);
  An[1] = analogRead(A1);
  buffer[pos] = An[0] * An[1];
  if (pos + 1 >= SZ) {
    pos = 0;
  }else {
    pos = pos + 1;
  }
  
  int i = 0;
  int sum;
  for (i=0 ; i <= SZ; i++) {
    sum = buffer[i];
    if (sum > 0) {
    Serial.println(sum);
    }
  }
}

Thank you!!

I can see two problems.

You aren't addressing the time dimension. If you're integrating over time, what you're working out is the sum of sample value times sample duration for all samples. The sample duration is essentially the interval between samples. You aren't measuring this or multiplying your sample value by it before integrating.

What you need to do is decide how long to wait between samples, use the 'blink without delay' technique (or just put a delay in your sketch if you don't need it to do anything else while it's waiting) to wait until it is time to take the next sample, and multiply each sample by the interval before summing.

In your integration loop, you aren't actually adding the new sample to the sum. You are just making the sum equal the new sample. You need something like:

sum = sum + buffer[i];

There is a shorter notation that has the same effect:

sum += buffer[i];

It would be possible to do the 'sum' at the point you take a new sample, rather than buffering the samples in an array and sum them at the end, but your way works just as well as long as you don't need a lot of samples (you only have 2kB of RAM to play with in total). For a hundred samples, it's fine. If you want to increase the number of samples significantly, better to integrate them as you go rather than buffer them all.

Would this data aquisition unit be tied to a PC? It might be easier to use the Arduino to aquire the data and pass the raw data to the PC and do the data storage and number crunching there. The Arduino has a limited amount of memory to store data, whereas a PC has, by comparison, unlimited storage. VB, VC#, Python, Java all would provide the tools necesary to to process and store the data.

You've also got to handle the product correctly. The product of two 16 bit integers is a 32 bit integer. You are assigning the product of the "int" back into an "int" which loses a lot of information. The sum variable should be a "long".

Pete

You could do the same thing quicker by avoiding adding 100 values on every loop(). But compared to over 200 usecs for 2 analog reads I am guessing you won't even get 50% faster... hard to say when they are 32-bit adds done on an 8-bit machine even if it is running a 16 MHz.

I'm only guessing that you want no more than 100 elements totaled, otherwise about half the lines below aren't needed. But then the total will grow. If 64-bit unsigned is used for the sum then it could still take a very long time to overflow while with 32-bit unsigned it shouldn't take too long to sit and watch happen.
The only thing is that if you want 100 elements totaled, should it not start printing until all 100 elements have been filled? If so then a flag and 1 conditional need to be added.

unsigned long sum = 0UL;  // will hold over 4000 * (1023 * 1023)
byte dataIndex = 0;
const byte maxIndex = 100; // byte is good to 255
unsigned long data[ maxIndex ];

void setup() {
  for ( dataIndex = 0; dataIndex < maxIndex; dataIndex++ )      data[ dataIndex ] = 0UL;
  dataIndex = 0;
  // initialize the serial communication:
  Serial.begin(9600);
}

void loop() {
  if ( dataIndex == maxIndex )  dataIndex = 0;

  sum -= data[ dataIndex ];  
  data[ dataIndex ] = analogRead(A0) * analogRead(A1);
  sum += data[ dataIndex++ ];  // as opposed to adding 100 values every time, loop() will process much quicker

  if (sum > 0UL ) {
    Serial.println(sum);
  }
}

To wait until 100 elements are full before printing:

unsigned long sum = 0UL;  // will hold over 4000 * (1023 * 1023)
byte dataIndex = 0;
const byte maxIndex = 100; // byte is good to 255
unsigned long data[ maxIndex ];
byte printFlag = 0;

void setup() {
  for ( dataIndex = 0; dataIndex < maxIndex; dataIndex++ )      data[ dataIndex ] = 0UL;
  dataIndex = 0;
  // initialize the serial communication:
  Serial.begin(9600);
}

void loop() {
  if ( dataIndex == maxIndex )  {
    dataIndex = 0;
    printFlag = 1;
  }

  sum -= data[ dataIndex ];  
  data[ dataIndex ] = analogRead(A0) * analogRead(A1);
  sum += data[ dataIndex++ ];  // as opposed to adding 100 values every time, loop() will process much quicker

  if (( sum > 0UL ) && printFlag ) {
    Serial.println(sum);
  }
}

I compiled (Arduino 0022) and ran the 2nd code with nothing connected to A0 or A1 (used unterminated floating returns as data) and it runs on my UNO. The printed values while not being the same did stay close as expected.

You might also want to increase the serial baud rate to 115200 to keep up with the output.