multiple SHT15 temp/humidity data acq. question

Hi,
I’ve got two SHT15 temp/humidity sensors hooked up to one Arduino Diecimila, which are working great now. Example program from Sparkfun(based on Maurice Ribble’s) works perfect for me for a single sensor. Since I didn’t learn very much about Arduino language, only simple duplication of the Maurice program by replacing variable names was possible with me. It worked fine without hitch. However, it made the program quite lengthy.
Well, I’ve been trying to make it shorter by using
for(n=0;n<3;n++)
syntax. (Right now, I have total of three SHT15’s)
The program was compiled O.K., and uploaded to Arduino also O.K. However, ACK errors are showing up when the program is executed, and no temp/humidity values are showing up.
Any suggestions on the program would be welcome.

----program attached— a bit lenghty, :wink: ----

// sensirion SHT15 humidity/temperature sensor (sparkfun breakout, based on Maurice Ribble and
// Wayne’s Raging Reality site)
// 3 sensors using for-loop
// April 9, 2009
int temperatureCommand = B00000011; // command used to read temperature
int humidityCommand = B00000101; // command used to read humidity
int ack; // track acknowledgment for errors
int val;
float temperature;
float humidity;

void setup() {
int n, dev_clk[3]={0,2,4}, clockPin; // pins used for clock
int m, dev_dat[3]={1,3,5}, dataPin ; // pins used for data

for(n=0;n<1;n++)
{
dataPin=dev_dat[n];
digitalWrite(dataPin,LOW);
pinMode(dataPin, INPUT);// sets the digital pin as input(logic 1)
}
Serial.begin(9600); // open serial at 9600 bps
}

void loop() {
// read the temperature and convert it to centigrades
int n, dev_clk[3]={0,2,4}, clockPin; // pins used for clock
int m, dev_dat[3]={1,3,5}, dataPin ; // pins used for data
for(n=0; n<3;n++)
{
sendCommandSHT(temperatureCommand, dataPin, clockPin);
waitForResultSHT(dataPin);
val = getData16SHT(dataPin, clockPin);
skipCrcSHT(dataPin, clockPin);
temperature = (float)val * 0.01 - 40;
Serial.print("temperature: ");
Serial.print((long)temperature, DEC);

// read the humidity
sendCommandSHT(humidityCommand, dataPin, clockPin);
waitForResultSHT(dataPin);
val = getData16SHT(dataPin, clockPin);
skipCrcSHT(dataPin, clockPin);
humidity = -4.0 + 0.0405 * val + -0.0000028 * val * val;
Serial.print("humidity: ");
Serial.print((long)humidity, DEC);
}
delay(300000); // wait for 5 Minutes for next reading
}

// commands for reading/sending data to a SHTx sensor

int shiftIn(int dataPin, int clockPin, int numBits) {
int ret = 0;

for (int i=0; i<numBits; ++i) {
digitalWrite(clockPin, HIGH);
//delay(10); not needed :slight_smile:
ret = ret*2 + digitalRead(dataPin);
digitalWrite(clockPin, LOW);
}
return(ret);
}

// send a command to the SHTx sensor
void sendCommandSHT(int command, int dataPin, int clockPin) {
int ack;

// transmission start
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
digitalWrite(dataPin, HIGH);
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, LOW);
digitalWrite(clockPin, LOW);
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, HIGH);
digitalWrite(clockPin, LOW);

// shift out the command (the 3 MSB are address and must be 000, the last 5 bits are the command)
shiftOut(dataPin, clockPin, MSBFIRST, command);

// verify we get the right ACK
digitalWrite(clockPin, HIGH);
pinMode(dataPin, INPUT);
ack = digitalRead(dataPin);
if (ack != LOW)
Serial.println(“ACK error 0”);
digitalWrite(clockPin, LOW);
ack = digitalRead(dataPin);
if (ack != HIGH)
Serial.println(“ACK error 1”);
}

// wait for the SHTx answer
void waitForResultSHT(int dataPin) {
int ack;

pinMode(dataPin, INPUT);
for(int i=0; i<100; ++i) {
delay(10);
ack = digitalRead(dataPin);
if (ack == LOW)
break;
}
if (ack == HIGH)
Serial.println(“ACK error 2”);
}

// get data from the SHTx sensor
int getData16SHT(int dataPin, int clockPin) {
int val;

// get the MSB (most significant bits)
pinMode(dataPin, INPUT);
pinMode(clockPin, OUTPUT);
val = shiftIn(dataPin, clockPin, 8);
val *= 256; // this is equivalent to val << 8;

// send the required ACK
pinMode(dataPin, OUTPUT);
digitalWrite(dataPin, HIGH);
digitalWrite(dataPin, LOW);
digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW);

// get the LSB (less significant bits)
pinMode(dataPin, INPUT);
val |= shiftIn(dataPin, clockPin, 8);
return val;
}

// skip CRC data from the SHTx sensor
void skipCrcSHT(int dataPin, int clockPin) {
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
digitalWrite(dataPin, HIGH);
digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW);
}
----end of program —

One more comment: The data acquisition is done via sequential mode, that is, 1st sensor,(delay), 2nd, (delay), and 3rd sensor.

Thanks for your interest. :)