I²C Optimization

Hi guys,
i programmed something to read out several I²C pressure sensors in parallel.
As i checked everything it runs not as fast as i hoped for.
So I wonder if it is possible to optimize the sketch.
As Iam doing always the same, shouldn´t it be possible to process multiple steps at once?!

Best regards :slight_smile:

#include <Wire.h>

 int c=0;
 int g=0;
 int h=0;
 int i=0;
 int d=0;
 int e=0;
 int f=0;
 
char dat[12];


void setup() {
Wire.begin();
  Serial.begin(250000);
}

void loop() {

 // Drucksensoren 

 Wire.requestFrom(123,2);
 while (Wire.available())
{
c=c*256+Wire.read();
}

Wire.requestFrom(124,2);
while (Wire.available())
{
d=d*256+Wire.read();
}

Wire.requestFrom(125,2);
while (Wire.available())
{
e=e*256+Wire.read();
}

Wire.requestFrom(110,2);
while (Wire.available())
{
f=f*256+Wire.read();
}

/*
Wire.requestFrom(122,2);
 
while (Wire.available())
{
g=g*256+Wire.read();
}
*/
Wire.requestFrom(126,2);
while (Wire.available())
{
h=h*256+Wire.read();
}

dat[0] = byte(lowByte(c));
dat[1] = byte(highByte(c));
dat[2] = byte(lowByte(d));
dat[3] = byte(highByte(d));
dat[4] = byte(lowByte(e));
dat[5] = byte(highByte(e));
dat[6] = byte(lowByte(f));
dat[7] = byte(highByte(f));
dat[8] = byte(lowByte(g));
dat[9] = byte(highByte(g));
dat[10] = byte(lowByte(h));
dat[11] = byte(highByte(h));
//dat[12] = byte(lowByte(i));
//dat[13] = byte(highByte(i));  

Serial.write(dat,12);
Serial.println();
//delayMicroseconds(1000);
};

You can only address one device at once* on the I2C bus. *Except you do some fancy hacking that is probably not worth the time you have to put into.

For prettier code, put the always identical request & calculation into a method.

For faster execution, write into your data array in a serial manner and try doing the calculations on there more efficiently. However, the compiler usually does a pretty good job anyway.

Furthermore, you may want to exchange some of the Arduino-specific macros/functions with more native stuff. E.g. the assignment of each low+high byte of a variable may be faster by accessing the data array as another data type and then assigned the 16bit var directly...

it runs not as fast as i hoped for.

What did you expect?