Read only the first row of data from a loop

Hi all,

I am working on this project where I am trying to gather data from pressure sensors.
My question is:
I am trying to just take the first row of data from the output and trying to store it in a separate array so that I can subtract this from the remaining rows of data .
I m sure it is something simple and I am just complicating it. Any help would be appreciated.
Thanks in advance
#define numberOfSensors 4 //define the number of sensors I am using
int sensorReading[4];
int sensor1[4];
int sensor2[4];
int runtime=1;

void setup() {
// initialize the serial port communication:
Serial.begin(9600);
}

void loop() {
long time =millis();
for (int thisSensor=0; thisSensor < numberOfSensors; thisSensor++)
{
sensorReading[thisSensor]=analogRead(thisSensor);
Serial.print(sensorReading[thisSensor]);
if (thisSensor<numberOfSensors-1)
{
Serial.print(",");
}
/sensor1[0]==sensorReading[0];
sensor1[1]==sensorReading[1];
sensor1[2]==sensorReading[2];
sensor1[3]==sensorReading[3];
/
//Serial.print(sensorReading[3]);
//Serial.print(sensor1[1]);
}

delay(1);
int temparray[4]={sensorReading[0],sensorReading[1],sensorReading[2],sensorReading[3]};
if (runtime)
{
for (int i=0;i<4;i++)
{
//sensor1_=temparray*;_
_ Serial.print(temparray);
Serial.print(",");
runtime–;
}
}*_

Serial.println(); //print a blank line - this line is important because Processing will start a serial event only after reading this blank line.
}
Output looks like this:
44,23,0,321
44,23,0,322
44,23,0,323
44,23,0,322
44,23,0,324
44,23,0,320
All I want to do is take the first row (44,23,0,321)-store it in a separate array and then take the next rows and subtract from this first row.
My_Sketch.ino (1.12 KB)

Please use code tags when posting code, to stop the forum software mangling it.

I suggest you put a for loop in setup() which reads the initial value from each sensor and stores it into your reference array. Then you can subtract those values from the sensor values you read subsequently in loop().

Maximum of one thread per question please.

http://forum.arduino.cc/index.php?topic=257877.0

All I want to do is take the first row (44,23,0,321)-store it in a separate array and then take the next rows and subtract from this first row.

Why the need for 2 arrays ?
Put the first set of readings in row 0 of a 2 dimension array then put each subsequent set of readings in row 1 of the same array before doing the subtraction.

UKHeliBob:
Put the first set of readings in row 0 of a 2 dimension array then …

That could be made to work too, but I think the initial set of readings is used for a different purpose to the subsequent ones and storing both sets in a single 2-d array hides / loses that distinction. Two separate arrays (one for the calibration values, the other for the runtime values) makes more sense to me.

Hi all,

Thank you so much for replying to my post.
This is what I did to my code. But I am not getting the desired output-i.e, just plain subtraction of arrays.
This is not possibly the best code, but please point my mistakes out. I did what PeterH told.

#define numberOfSensors 4 //define the number of sensors I am using
 int sensorReading[4];
 //int sensor1[4];
 int sensor2[4];
 int temparray[4];
 
 
void setup() {
  // initialize the serial port communication:
  Serial.begin(9600);
  for (int thisSensor=0; thisSensor < numberOfSensors; thisSensor++)
  {
    sensorReading[thisSensor]=analogRead(thisSensor);}
   int temparray[4]={sensorReading[0],sensorReading[1],sensorReading[2],sensorReading[3]};
   for (int i=0;i<4;i++)
    {
    //sensor1[i]=temparray[i];
    Serial.print(temparray[i]);
    Serial.print(",");
    }

}

void loop() {
  long time =millis();
  for (int thisSensor=0; thisSensor < numberOfSensors; thisSensor++)
  {
    sensorReading[thisSensor]=analogRead(thisSensor);
    //Serial.print(sensorReading[thisSensor]);
    if (thisSensor<numberOfSensors-1)
    {
      Serial.print(",");
    }
    Serial.print(sensorReading[0]);
    Serial.print(temparray[0]);
    sensor2[0]=sensorReading[0]-temparray[0];
    sensor2[1]=sensorReading[1]-temparray[1];
    sensor2[2]=sensorReading[2]-temparray[2];
    sensor2[3]=sensorReading[3]-temparray[3];
    int sensor2[4]={sensor2[0],sensor2[1],sensor2[2],sensor2[3]};
    for (int j=0;j<4;j++)
    {
    Serial.print(sensor2[j]);
    Serial.print(",");
    }
  }
    
delay(1);  
Serial.println();   //print a blank line - this line is important because Processing will start a serial event only after reading this blank line. 
}

I can’t help thinking that you are overengineering this.
Read the 4 initial values into an array in setup() as you have. Then, each time through loop() read each sensor and do the subtraction as part of the Serial.print().

Something like this

#define numberOfSensors 4 //define the number of sensors I am using
int firstReading[4];

void setup() 
{
  Serial.begin(115200);
  for (int thisSensor=0; thisSensor < numberOfSensors; thisSensor++)
  {
    firstReading[thisSensor]=analogRead(thisSensor);
  }
  for (int i=0;i<4;i++)
  {
    Serial.print(firstReading[i]);
    Serial.print(",");
  }
  Serial.println();
}

void loop() 
{
  for (int thisSensor = 0; thisSensor < numberOfSensors; thisSensor++)
  {
    Serial.print(firstReading[thisSensor] - analogRead(thisSensor));
    if (thisSensor != 3)
    {
      Serial.print(",");
    }
  }

  delay(1);  
  Serial.println();   //print a blank line - this line is important because Processing will start a serial event only after reading this blank line. 
}

Thanks a lot UKHeliBob. I know I was overcomplicating it; that is why asked for help here. I really appreciate it.