IF / ELSE in void setup() ??

Hi,
I am measuring temperature. Every time the measurement has been done Arduino goes to Deep Sleep Mode. For that reason all the code I have is in VOID SETUP() and the LOOP() is empty. I would like to check if the measured temperature falls into certain range (-30 deg C and +30 deg C). For that reason can I use IF-ELSE in VOID SETUP() ??

void setup() {
  Serial.begin(9600);
  sensors.begin();
  sensors.getAddress(sensorDeviceAddress, 0);
  sensors.setResolution(sensorDeviceAddress, SENSOR_RESOLUTION);

// from here is copied from loop part
sensors.requestTemperatures();
temp = sensors.getTempCByIndex(0);

if (-30 <= temp && temp <= 30) {
     temp_ok = temp;
   }
else {
return; /// WILL THIS WORK IN VOID SETUP??
}

Serial.print(temp_ok);

// Arduino goes to SLEEPMODE and when wakes up VOID SETUP starts again

} // end void setup

If the sensor is giving obviously wrong readings like -127 then it's possible that it is also sometimes giving slightly wrong readings which are not so noticeable.
If you post your code we can help some more.

quilkin:
If the sensor is giving obviously wrong readings like -127...

With a value like that a more likely culprit is a communications error trying to read the sensor.

Your code is not going to deep sleep if temp is OK and will spin an infinite empty loop....so we don't know what you are really talking about...

waking up from deep sleep does not restart the setup() function but would just continue where you left off... so unless you put a loop I need the setup, you will do this only once... and if you put a loop in the setup, then why not consider doing that directly in the loop where it makes more sense by design?

can I use IF-ELSE in VOID SETUP()

To answer your specific question, yes, you can use if/else in the setup() function.

However, without seeing your complete program it is impossible to say whether you are using it correctly in your circumstances.

I reformulate my question - how can I accomplish this:

void setup() {
// different stuff here
// different stuff here
sensors.requestTemperatures();
1:
temp = sensors.getTempCByIndex(0);
if (-30 <= temp && temp <= 30) {
temp_ok = temp;
}
else {
return; // go back to 1:
}

Serial.print(temp_ok);
} // end void setup, program stops

Well OP shared code

if (-30 <= temp && temp <= 30) {
     temp_ok = temp;
} else {
    return; /// WILL THIS WORK IN VOID SETUP??
}

This is a legit construct in C (looks event better when indented properly....) and it will work as designed but will not do what OP thinks this is doing....

Your post #5 shows you don't understand the execution flow of an arduino program

Go read again about what setup() does and what loop() does and how they are called from the generated main() function

The main control of your program looks (essentially, simplifying a bit) like this

int main(void)
{
    init(); // you don't mess with that one
    setup(); // this is your function 
    for (;;) {
        loop(); // this is your function called forever in an infinite loop
        if (serialEventRun) serialEventRun(); // if you coded serialEventRun() function  it's called as well after every loop
    }
    return 0;
}

So when you return from the setup() call you go to the loop() section

Inside void setup() how to restart setup() ?

void setup() {
sensors.requestTemperatures();
temp = sensors.getTempCByIndex(0);

if (-30 <= temp && temp <= 30) {
temp_ok = temp;
}
else {
//RESTART VOID SETUP
}

Serial.print(temp_ok);
}


Maybe this will work?

void(* resetFunc) (void) = 0;
resetFunc();

Unless you code a loop into the setup, You can't (or unless you reset the arduino there) and there is zero need for that. Rethink your design goal. What you want to do belongs to the loop() section

See answer #3 and you could benefit from reading Nick's power article

Call LOOP() inside SETUP() then do LOOP stuff and exit from LOOP then return to SETUP?

I think you're on the wrong track. If the sensor isn't returning incorrect values sometimes then you should investigate why that is, rather than fixing it the way you are trying.
Maybe you just need to do a small delay before reading the sensor. Or something similar. What if you get -29 when the actual temperature is +20?

what do you mean by delay?

delay(1000);
sensors.requestTemperatures();
tempvalue = sensors.getTempCByIndex(0);

why should it help?

At the moment the temperature is being measured and no anomalies are present. Since I gave my NodeMCU V2 Lua new power by adding a rechargeable battery to it (Adafruit 18650 3.7V 2600mAh, 20 Feb 20:00) there have been two anomalies:

02/22/2017 00:51:39 FST 85
02/22/2017 00:43:34 FST -127

Noone knows the reason why. I made a reset and the values became normal again.

02/22/2017 01:37:19 FST 17.25
02/22/2017 01:22:38 FST 17.62
02/22/2017 01:07:57 FST 18.12
02/22/2017 00:53:11 FST 16.31
02/22/2017 00:52:23 FST 15.81
02/22/2017 00:51:39 FST 85
02/22/2017 00:43:34 FST -127
02/22/2017 00:28:54 FST 14
02/22/2017 00:14:17 FST 14.38
02/21/2017 23:59:40 FST 14.63
02/21/2017 23:45:04 FST 14.94

Might be that there was a bad connection because at the same evening I installed the battery and charging module. The connections are made using jumper wires and clear tape. So maybe the error does not hide in the code but is inside the physical part. I think that it is good to make IF - ELSE statement to SETUP(). When the value is off, then the reset is performed. As I did manually.

-127 usually means a communication error. you need to catch those and not take that blindly as a reading.

Again - see discussion above for using setup() and loop() correctly. What you want to do is read T° form time to time, so this is a repetitive operation. repetitive operations are best achieved in the loop(). if you want to sleep your arduino during the loop() that's totally possible and I pointed you to Nick's article on power saving.

Ok I put the if sentence into loop(). But how to restart loop? Does return; work? Or should I use return 0;

void loop() {
float degreesC;
float temp_ok;
delay(500);
sensors.requestTemperatures();
delay(500);
temp_ok = sensors.getTempCByIndex(0);
if (-30 <= temp_ok && temp_ok <= 30) {
degreesC = temp_ok;
}
else {
return; //restart loop, take new measurement
}
Serial.print(degreesC);
}

the loop() loops by design - have you looked at the main() code I listed above?

for (;;) {
        loop(); // this is your function called forever in an infinite loop
        if (serialEventRun) serialEventRun(); // if you coded serialEventRun() function  it's called as well after every loop
    }

you don't need to restart it, you don't need explicit return or whatever.

You possibly would benefit from reading the basics or tutorials like this one

But how to restart loop?

The answer is in the name of the function.