Issue by coding smarter with a "for" loop

Hi,
I am struggeling coding something smarter than I did before:

What I did right now is the following

#include <Sensirion.h>;

/*
 * Query a SHT10 temperature and humidity sensor
 *
 * A simple example that queries the sensor every 3 seconds
 * and communicates the result over a serial connection.
 * Error handling is omitted in this example.
 */

//#include "Sensirion.h"

#define sensiriondataPin  2
#define sensirionclockPin 3

float temperature;
float humidity;
float dewpoint;


int sensor1 = 9;
int sensor2 = 10;
int sensor3 = 11;
int sensor4 = 12;


Sensirion tempSensor = Sensirion(sensiriondataPin, sensirionclockPin);

void setup()
{
  Serial.begin(9600);
pinMode(sensor1, OUTPUT);      
pinMode(sensor2, OUTPUT);      
pinMode(sensor3, OUTPUT);      
pinMode(sensor4, OUTPUT);      



}

void loop()
{
/* All data lines from all sensors are connected to one data line.
    Each sensor will be switched on by switching Sensor VCC */

//For sensor 1
  digitalWrite(sensor1, HIGH); //switch on sensor 1 
   delay(150); //time to sensor wake up
  tempSensor.measure(&temperature, &humidity, &dewpoint);
  Serial.print("Sensor1: ");
  Serial.print("Temperature: ");
  serialPrintFloat(temperature);
  Serial.print(" C, Humidity: ");
  serialPrintFloat(humidity);
  Serial.print(" %, Dewpoint: ");
  serialPrintFloat(dewpoint);
  Serial.println(" C");
  digitalWrite(sensor1, LOW);  //switch off sensor 1  
  
  
  //For sensor 2
  digitalWrite(sensor2, HIGH); //switch on sensor 2 
   delay(150); //time to sensor wake up
  tempSensor.measure(&temperature, &humidity, &dewpoint);
  Serial.print("Sensor2: ");
  Serial.print("Temperature: ");
  serialPrintFloat(temperature);
  Serial.print(" C, Humidity: ");
  serialPrintFloat(humidity);
  Serial.print(" %, Dewpoint: ");
  serialPrintFloat(dewpoint);
  Serial.println(" C");
  digitalWrite(sensor2, LOW);  //switch off sensor 2  
  
  
  //For sensor 3
  digitalWrite(sensor3, HIGH); //switch on sensor 3 
   delay(150); //time to sensor wake up
  tempSensor.measure(&temperature, &humidity, &dewpoint);
  Serial.print("Sensor3: ");
  Serial.print("Temperature: ");
  serialPrintFloat(temperature);
  Serial.print(" C, Humidity: ");
  serialPrintFloat(humidity);
  Serial.print(" %, Dewpoint: ");
  serialPrintFloat(dewpoint);
  Serial.println(" C");
  digitalWrite(sensor3, LOW);  //switch off sensor 3

  
  
  //For sensor 4
  digitalWrite(sensor4, HIGH); //switch on sensor 4 
   delay(150); //time to sensor wake up
  tempSensor.measure(&temperature, &humidity, &dewpoint);
  Serial.print("Sensor3: ");
  Serial.print("Temperature: ");
  serialPrintFloat(temperature);
  Serial.print(" C, Humidity: ");
  serialPrintFloat(humidity);
  Serial.print(" %, Dewpoint: ");
  serialPrintFloat(dewpoint);
  Serial.println(" C");
  digitalWrite(sensor4, LOW);  //switch off sensor 4  
  
}

void serialPrintFloat(float f){
  Serial.print((int)f);
  Serial.print(".");
  int decplace = (f - (int)f) * 100;
  Serial.print(abs(decplace));
}

What I want to do is something like that switching the sensors and setting up the variables:

for (int i=0; i <= 3; i++){
  //For all sensors
  digitalWrite(sensor[i], HIGH); //switch on sensor i 
   delay(150); //time to sensor wake up
  tempSensor.measure(&temperature, &humidity, &dewpoint);
  Serial.print("Sensor"[i])
  Serial.print(": ");
  Serial.print("Temperature: ");
  serialPrintFloat(temperature);
  Serial.print(" C, Humidity: ");
  serialPrintFloat(humidity);
  Serial.print(" %, Dewpoint: ");
  serialPrintFloat(dewpoint);
  Serial.println(" C");
  digitalWrite(sensor[i], LOW);  //switch off sensor i  

   }

There is an error coming up by not declaring the correct variable “sensor”. Declaring int “sensor” another error occurs:

error: invalid types’int[int]’ for array subscript

I do not know how to get that message away? :-[

From the first chunk of code, I presume that you added something like this in the second chunk:

int sensor[] = {9, 10, 11, 12};

If not, that's what you should have.

Serial.print("Sensor"[i])

I'm not sure what you are trying to do here, but I think it needs to be more like this:

Serial.print("Sensor ");
Serial.print(i);

Thanks for the hint. That helps me showing the according values. The main problem is to solve how to switch on the sensors with the variable “i”. I suspect the rest is fine…

 digitalWrite(sensor[i], HIGH); //switch on sensor i

You haven't showed how "sensor[]" is declared.

Groove, yes that was my fault. Here it is:

int sensor;
int sensor1 = 9;
int sensor2 = 10;
int sensor3 = 11;
int sensor4 = 12;

As PaulS suggested, you need to put those values into an array - you can't subscript a scalar variable.

Dear PaulS,
Dear Groove,
got it finally- sorry for the delay…

Here is the code which finally got no failure messages :). I will try this soon on my board…

#include <Sensirion.h>;

/*
 * Query a SHT10 temperature and humidity sensor
 *
 * A simple example that queries the sensor every 3 seconds
 * and communicates the result over a serial connection.
 * Error handling is omitted in this example.
 */

//#include "Sensirion.h"

#define sensiriondataPin  2
#define sensirionclockPin 3

float temperature;
float humidity;
float dewpoint;
int sensor[] = {9, 10, 11, 12};

Sensirion tempSensor = Sensirion(sensiriondataPin, sensirionclockPin);

void setup()
{
  Serial.begin(9600);
}

void loop()
{

for (int i=0; i <= 3; i++){
  //For all sensors
  digitalWrite(sensor[i], HIGH); //switch on sensor 2 
   delay(150); //time to sensor wake up
  tempSensor.measure(&temperature, &humidity, &dewpoint);
  Serial.print("Sensor");
  Serial.print(i);
  Serial.print(": ");
  Serial.print("Temperature: ");
  serialPrintFloat(temperature);
  Serial.print(" C, Humidity: ");
  serialPrintFloat(humidity);
  Serial.print(" %, Dewpoint: ");
  serialPrintFloat(dewpoint);
  Serial.println(" C");
  digitalWrite(sensor[i], LOW);  //switch off sensor 2  
   } 
}

void serialPrintFloat(float f){
  Serial.print((int)f);
  Serial.print(".");
  int decplace = (f - (int)f) * 100;
  Serial.print(abs(decplace));
}