Page 1 of 1

Not all code run in setup()

Posted: 12 Oct 2016 23:12
by Kronsson
It seems like not all code is run in the setup().
Why?

On a "normal Arduino" the code below will blink the LED slow twice and then continue with fast blinking.
On Z-UNO it starts immediately with fast blinking.



// LED pin number
#define LED_PIN 13

void setup() {
pinMode(LED_PIN, OUTPUT); // set LED pin as output

digitalWrite(LED_PIN, HIGH); // turn LED on
delay(2000); // wait for 1 second
digitalWrite(LED_PIN, LOW); // turn LED off
delay(2000);
digitalWrite(LED_PIN, HIGH); // turn LED on
delay(2000); // wait for 1 second
digitalWrite(LED_PIN, LOW); // turn LED off
}

void loop() {
digitalWrite(LED_PIN, HIGH); // turn LED on
delay(100); // wait for 1 second
digitalWrite(LED_PIN, LOW); // turn LED off
delay(300); // wait for 1 second
}

Re: Not all code run in setup()

Posted: 13 Oct 2016 10:11
by PoltoS
Your observation is correct. delay() do not work in setup() due to some structures not initialized at this time. We will update the manual to mention this.

Re: Not all code run in setup()

Posted: 13 Oct 2016 13:28
by Kronsson
Ok. Thanks for that clarification.
Does it happen to be the same for Serial.print()?
If so, could it be fixed? It is really handy to see on the serial monitor that the sketch started by having a print in the setup.

Re: Not all code run in setup()

Posted: 14 Oct 2016 08:48
by A.Harrenberg
Hi PoltoS,
PoltoS wrote:Your observation is correct. delay() do not work in setup() due to some structures not initialized at this time. We will update the manual to mention this.
will it stay that way or will it be changed later so that delay() can be used in setup()?

I have currently some delay() in my sketch that I put there to allow the sensors and an OLED to do there init before entering the loop().

I had noticed that sometimes the first values aren't correct, but had not investigated further. This explains now why, but I want to know if it is worth putting some extra delay in loop() or wait for some changes in Z-Uno to allow delay() in setup().

Regards,
Andreas.

Re: Not all code run in setup()

Posted: 15 Oct 2016 11:23
by PoltoS
We will probably add this in setup too as it might be required to init user connected HW. But keep in mind that it will delay inisalization of Z-Wave stack (it is done after setup).

Re: Not all code run in setup()

Posted: 15 Oct 2016 11:45
by A.Harrenberg
Hi PoltoS,
thanks for the infos. Will the Z-Wave stack be initialized "between" setup() and loop(), so the question is, can we safely assume that the Z-Wave stack is operational at the first entry of loop()?

For the delays I think that I will create a "user-init" function inside loop() that is only called once and put all my initial delays for HW-Init there. I am not sure about moving e.g. Serial.begin(), bmp.begin() also to this init-function but I would try to only leave the e.g. pinmode definitions in the setup().

Do you see any problems by doing this?
Regards,
Andreas.

Re: Not all code run in setup()

Posted: 15 Oct 2016 12:47
by PoltoS
Yes, Z-Wave stack and full Z-Uno code is initialized before first enter into loop(). Function setup() should be used only to init variables and set up h/w pins mode.

Serial.begin() and bmp.begin() are ok to be in setup()