Not all code run in setup()

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
Kronsson
Posts: 3
Joined: 12 Oct 2016 23:02

Not all code run in setup()

Post 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
}

User avatar
PoltoS
Posts: 5626
Joined: 26 Jan 2011 19:36

Re: Not all code run in setup()

Post 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.

Kronsson
Posts: 3
Joined: 12 Oct 2016 23:02

Re: Not all code run in setup()

Post 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.

A.Harrenberg
Posts: 201
Joined: 05 Sep 2016 22:27

Re: Not all code run in setup()

Post 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.
fhem.de - ZWave development support

User avatar
PoltoS
Posts: 5626
Joined: 26 Jan 2011 19:36

Re: Not all code run in setup()

Post 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).

A.Harrenberg
Posts: 201
Joined: 05 Sep 2016 22:27

Re: Not all code run in setup()

Post 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.
fhem.de - ZWave development support

User avatar
PoltoS
Posts: 5626
Joined: 26 Jan 2011 19:36

Re: Not all code run in setup()

Post 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()

Post Reply