If you have a lot of "sets" of variables - like, five different things each with their own "temp", "humidity", and "time", then you can define a struct to tie all those things together:
struct Thermo {
float temp;
float humidity;
uint32_t time;
};
Thermo upstairs;
Thermo downstairs;
Thermo garage;
Thermo outside;
This allows you to use upstairs.temp
as a variable.
Even better, you can pass these things in functions by reference.
void foo() {
if(buttonAIsPressed()) {
display(upstairs);
}
else {
display(downstairs);
}
}
void display(Thermo& t) {
// code to display a Thermo
}
If you have lots of these things, then you can put them in an array and reference them by number.
Thermo thermo[5]; // five Thermo structs
void quux() {
display(thermo[2]);
}
This allows you to loop through all of them
void displayAll() {
for(i=0; i< 5; i++) {
display(thermo[i]);
delay(500);
}
}
You can put functions inside a struct. When a function is inside a struct, it's called a method.
struct Thermo {
byte temp_pin;
byte humidity_pin;
float temp;
float humidity;
uint32_t time;
void getReading() {
temp = analogRead(temp_pin);
humidity = analogRead(humidity_pin);
time = millis();
}
};
void updateAll() {
for(i=0; i< 5; i++) {
thermo[i].getReading();
}
}
The variables in a struct are all set to zero at the start, but you can give them an initializer. You can even code up a more complicated kind of initializer called a constructor. Just remember that in arduinos, constructors are invoked before any hardware setup, so they mustn't try to do any talking to pins. In Arduino code, your structs will need their own setup()
struct Thermo {
const byte temp_pin;
const byte humidity_pin;
const byte led_pin;
float temp;
float humidity;
uint32_t time;
Thermo(byte attach_temp, byte attach_humidity, byte attach_led)
: temp_pin(attach_temp), humidity_pin(attach_humidity), led_pin(attach_led)
{
}
void setup() {
pinMode(led_pin, OUTPUT);
}
void getReading() {
temp = analogRead(temp_pin);
humidity = analogRead(humidity_pin);
time = millis();
}
};
Thermo thermo[5] = {
Thermo(A0, A1, 3), // garage
Thermo(A2, A3, 4), // upstairs
Thermo(A5, A5, 5), // downstairs
Thermo(A6, A7, 6), // kitchen
Thermo(A8, A9, 7) // basement
};
void setup() {
for(i=0; i< 5; i++) {
thermo[i].setup();
}
}
For more info, google "C++ tutorial" and learn some stuff about the C++ language. You can also read the page in my sig, but that might be a little more info than you need at this point
.