Master I2C code repeating variables

Hey Ya’ll,

I’ve created an I2C system for reading a large amount of analog thermisters. Each slave arduino mega reads the voltage of a probe to each of its analog ports. The master then collects the data sequentially from each slave and writes each value to its own column in a csv file on an onboard SD card. However, the master repeats the first value in the place of the second before moving on to the rest of the values in order. (1,1,3,4,5…) I think i’ve isolated the problem to GETREADS(), the function that requests the data:

int numUnits = 5; //Total number of arduinos
int SlaveDeviceId;
int numPin;
long xInit;

   void GETREADS(){

  for (int i=2; i<=numUnits; i++){ //The master is number 1, so iteration starts at 2
    SlaveDeviceId=i;
    numPin=2;
    Wire.beginTransmission(SlaveDeviceId); Wire.write(numPin); delay(10);
    int available = Wire.requestFrom(SlaveDeviceId, (byte)2);
    if(available == 2) { xInit = Wire.read() << 8 | Wire.read();} else { Serial.print("ERROR 403: WRONG bytes ARDUINO 2>> "); Serial.println(available); } Wire.endTransmission();

    for (int j=(i-2)*16-1; j <= (i-2)*16+16; j++){
      available = Wire.requestFrom(SlaveDeviceId, (byte)2);
      Wire.beginTransmission(SlaveDeviceId); Wire.write(numPin); delay(10);
      if(available == 2) { x[j] = Wire.read() << 8 | Wire.read(); 
        Vo = (x[j]); G2 = G1 * (1023.0 / (float)Vo - 1.0); 
         x[j]=G2;
        } else { Serial.print("ERROR 403: WRONG bytes ARDUINO " + String(SlaveDeviceId) + " >> "); 
          Serial.println(available); 
        } 
      Wire.endTransmission();
      numPin=numPin+1;
    }
  }

And here is the data for the slaves:

#include <Wire.h>

const byte NODE_ADDRESS = 3;//<----------SLAVE NAME
byte LastMasterCommand = 0;
 
void setup(){
  Wire.begin(NODE_ADDRESS);       // Join i2c bus with Slave ID
  Wire.onReceive(receiveCommand); // Register talk event
  Wire.onRequest(slavesRespond);  // Register callback event
 
}
 
void loop(){
 // delay(100);
}
 
void receiveCommand(int howMany){
  LastMasterCommand = Wire.read(); // 1 byte (maximum 256 commands)
}
 
void slavesRespond(){
 
  int returnValue = 0;
 
  switch(LastMasterCommand){
    case 0:                     // No new command was received
      Wire.write("NA");
    break;
    
    case 1:                    // DEVICE ID
      returnValue = NODE_ADDRESS;
    break;
 
    case 2:                    // Return A0 sensor value
      returnValue = analogRead(A0);
    break;
 
    case 3:                    // Return A1 sensor value
      returnValue = analogRead(A1);
    break;
 
    case 4:                    // Return A2 sensor value
      returnValue = analogRead(A2);
    break;
 
    case 5:                    // Return A3 sensor value
      returnValue = analogRead(A3);
    break;
 
    case 6:                    // Return A4 sensor value
      returnValue = analogRead(A4);
    break;
 
    case 7:                    // Return A5 sensor value
      returnValue = analogRead(A5);
    break;
 
    case 8:                    // Return A6 sensor value
      returnValue =analogRead(A6);
    break;
 
    case 9:                    // Return A7 sensor value
      returnValue = analogRead(A7);
    break;
  
    case 10:                    // Return A8 sensor value
      returnValue = analogRead(A8);
    break;
 
    case 11:                    // Return A9 sensor value
      returnValue = analogRead(A9);
    break;
 
    case 12:                    // Return A10 sensor value
      returnValue = analogRead(A10);
    break;
 
    case 13:                    // Return A11 sensor value
      returnValue = analogRead(A11);
    break;
 
    case 14:                    // Return A12 sensor value
      returnValue = analogRead(A12);
    break;
 
    case 15:                    // Return A13 sensor value
      returnValue =analogRead(A13);
    break;
 
    case 16:                    // Return A14 sensor value
      returnValue = analogRead(A14);
    break;
 
    case 17:                    // Return A15 sensor value
      returnValue = analogRead(A15);
    break;
        
  }

 
 
  byte buffer[2];                 // Split int value into two bytes buffer
  buffer[0] = returnValue >> 8;
  buffer[1] = returnValue & 255;
  Wire.write(buffer, 2);          // Return response to last command
  LastMasterCommand = 0;          // Null last Master's command
}

Can anybody see why it’s repeating the first value twice?

Sorry, I have difficulty reading code that's all on one line. Maybe someone else?

The transaction involves writing to the slave. It does NOT also include reading from the slave.

PaulS:
The transaction involves writing to the slave. It does NOT also include reading from the slave.

Could you elaborate on that? I'm not sure I understand completely.

Crock_Pot_Connoisseur:
Could you elaborate on that? I'm not sure I understand completely.

That should have said transmission, not transaction.

Between the beginTransmission() call and the endTransmission() call, you do NOT read from the slave. You ONLY write to the slave, since the data isn't actually written until endTransmission() is called.