While and Multiple void Functions

Good Day, Folks. I trust all are well? I apologise about the title - I am not sure how to phrase it correctly.

So I have been tinkering again and seem to have come to a bit of an impasse... although I think that I should mention is that the principal problem is that I keep wanting to make things better and more efficient and more accurate and more feature rich - so much is possible that it seems a crime not to take it as far as one can, right?

Anyhow, what I am trying to do is this:

I have a temperature sensor and a humidity sensor. These are used to ensure that incubated samples stay within their limits at various stages of their growth. There are four stages, all of which have different requirements as far as temperature and humidity is concerned. The UNO reads all the values and depending on the required environment, it switches heaters, exhausts, humidifiers on or off to stabilize the temperature. Some of you very kind people helped me correct and improve that code!

Now I want to add a button for each stage of incubation. So when the "callus" stage is over I can flip the switch off and switch on the "maintenance" stage for instance. I plan on using good old on/off buttons, not the ones you have to hold for it to ==HIGH.

So I have this in mind on the code side of things so far, I know the syntax is all wrong and incomplete but this is just to illustrate my intended plan:

#included stuff here...

//Buttons on these pins.
const int Stage1Button = 3; 
const int Stage2Button = 4;
const int Stage3Button = 5;
const int Stage4Button = 6;

void setup() {
pinMode(Stage1Button, INPUT);
pinMode(Stage2Button, INPUT);
pinMode(Stage4Button, INPUT);
pinMode(Stage5Button, INPUT);

//also included here is the required info for the sensors and relays
}

void loop(){
//Stage 1 Program Button Settings
while (digitalRead(Stage1Button) == HIGH && digitalRead(Stage2Button) == LOW && digitalRead(Stage3Button) == LOW && digitalRead(Stage4Button) == LOW) {
Stage1();
}

//Stage 2 Program Button Settings
while (digitalRead(Stage1Button) == LOW && digitalRead(Stage2Button) == HIGH && digitalRead(Stage3Button) == LOW && digitalRead(Stage4Button) == LOW) {
Stage2();
}

//Stage 3 Program Button Settings
while (digitalRead(Stage1Button) == LOW && digitalRead(Stage2Button) == LOW && digitalRead(Stage3Button) == HIGH && digitalRead(Stage4Button) == LOW) {
Stage3();
}

//Stage 4 Program Button Settings
while (digitalRead(Stage1Button) == LOW && digitalRead(Stage2Button) == LOW && digitalRead(Stage3Button) == LOW && digitalRead(Stage4Button) == HIGH) {
Stage4();
}
//Also in the void loop() is the code for the sensors - their variables and the basic sensor setup.
}

Void Stage1() {
// this is where the min - max temperatures and humidity go for stage 1. 
// relays will switch on based on the sensor readings.
}

Void Stage2() {
// the environmental requirements here are different to Stage 1 and so the relays will switch on at different times here.
}

Void Stage3() {
 // same thing as above. 
}

Void Stage4() {
//Also the same issue - relays activate based on a new min - max temp and humidity range.
}

I hope I've been clear enough for people to get the idea behind my question... My main worry with the way I plan on implementing this is that when stage 2 is switched on, none of the code within the void loop() will run, and that means no sensor data.... a way around this would be to redo the sensor setup in every void Stagex() but that seems extremely inefficient, doesn't it? Or am I wrong?

Sadly I am only going to pick up this hardware next week so I cannot physically test it until then. Any advice on this would be great folks!

My main worry with the way I plan on implementing this is that when stage 2 is switched on, none of the code within the void loop() will run, and that means no sensor data.

Then don’t implement it that way!

The while statements in loop() are unnecessary. Use if statements, instead. That way, loop() executes over and over. On each pass, check what to do.

Sounds good then! If it is! I'll put up a revision for reference - is this an accepted standard by the way; to repost ones corrections?

On a related note, if my sensor setup were different for each stage (to take into account something like a temperature offset at different stages, due to anaerobic activity in the medium, for instance) would that be a case to use while statements in loop()?

They aren't, but I was just wondering about the possible uses for having it setup in this way. Perhaps if different sensor sensitivities were required for different applications?

On an unrelated note, PaulS, some of your replies are hilarious! I was looking for an answer to this question and someone posted a question saying something like, "I'm using a sensor and a potentiometer but it won't work, why?", no code attached and your reply was a one-liner similar to, error on line 35.

If you want to post updated code, please do that using a new post and [u]do not[/u] go back and replace the code in previous posts.

If you want the behaviour of your sketch to change then define a variable to hold the current mode, and use simple if/else or switch/case statements to execute the logic associated with the current mode. Robin2 has posted some neat examples of finite state machines which use this approach.