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.
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?
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
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
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
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?
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:
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.
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.