2

In the normal case the setup() function will be run once automatically and and then the loop() function will run many times.

I want to initialize several modules in the setup() function and then in the loop() I want to read values from them periodically. But if the period between usage is long I want to power off the modules. After defined time the modules will be powered on again and need to be initialized. Can I use the already existing setup() function inside the loop() to initialize the modules or I have to write another similar function? Is there any other better way to power off/on sensors and modules during the execution of code.

  • Sorry if I have not defined the question the right way, maybe somebody can help me edit it so that it can be properly formed – vladiz Dec 28 '14 at 14:29
12

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.

  • Thank you very much for all people that corrected my English. English is not my native language and it is really difficult (impossible) to get better without correction. – Dave Dec 28 '14 at 16:19
  • What you recommend is a good habit for beginners. But for calling it a "very bad design" you should have some stronger arguments. I agree, it is not a good design. – Ariser Dec 28 '14 at 16:57
  • I updated my answer – Dave Dec 28 '14 at 17:10
  • I agree, given the very good example, it is very bad design :) – Ariser Dec 28 '14 at 17:14
  • 1
    I don't disagree with your conclusion, but starting serial a second time will not cause that undefined behavior. The serial ports are global objects defined in HardwareSerial.h, the user code on arduino never creates new instances. – BrettAM Dec 28 '14 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.