It could be possible, since it is going simply to call the same function BUT it is really a very bad design*.
Usually the main purpose for a microcontroller is just sitting there and run a procedure or main function repeatedly until the end of the world. And during one loop just to check if some conditions are met.
You should never stop or influence your loop()
function and never mess up the default design of the arduino.
In you case (but it depends on your code and your application), you just want to run the loop()
once all sensors have been already started (in the setup()
function).
In your run, you check periodically the sensor and start a timer (better if interrupted). If an amount of time has expired and the sensor should be turned off, then the timer will call another function, which has the only target to switch sensors off.
The loop()
runs again and again and if the original condition is met, then you can start your sensors again.
To save up time and code typing, I suggest you write only ONE function for switching on the sensors and call this function in setup()
and in your loop()
, something like this:
void startSensors();
void stopSensors();
void setup() {
startSensors(); // I start all sensors here
}
void loop() {
if( condition?!?! )
{
...
stopSensors();
...
} else {
...
startSensors(); // I call the same function here
...
}
}
*: for istance you define in your setup()
the connection through serial port. This means that your object "serial port" is going to be recreated each time you call setup()
whit undefeined behavior in case it is outputting datas or has same data in the FIFO Buffer. Simply...setup()
has that name because needs to setup things once. Everything that must be done more than once, then must be called from run()
too.