As the answers to this question and this one state, setup()
and loop()
are identical in the way they execute code (apart from the obvious looping function of loop()
).
However.
I get different results with following two pieces of code:
Code run once, in setup()
/*
* light_ws2812 library test
*/
#include <WS2812.h>
WS2812 LED(1); // 1 LED
cRGB value;
void setup() {
LED.setOutput(13); // Digital Pin 13
value.b = 0; value.g = 0; value.r = 255; // RGB Value -> Yellow?
LED.set_crgb_at(0, value); // Set value at LED found at index 0
LED.sync(); // Sends the value to the LED
}
void loop() {
}
Code run once, in loop()
/*
* light_ws2812 library test
*/
#include <WS2812.h>
WS2812 LED(1); // 1 LED
cRGB value;
bool hasBeenSet = false;
void setup() {
LED.setOutput(13); // Digital Pin 13
}
void loop() {
if (hasBeenSet == false){
value.b = 0; value.g = 0; value.r = 255; // RGB Value -> Red
LED.set_crgb_at(0, value); // Set value at LED found at index 0
LED.sync(); // Sends the value to the LED
hasBeenSet = true;
}
}
The difference:
The code run in setup()
makes the LED flash once with the desired color.
The code run in loop()
makes the LED stay lit with the desired color.
With the data being transmitted on pin 13, I can tell (because of the LED on there) in both cases data is being sent only once. With the loop()
version I can even disconnect the data line after the color has been set, and it will stay lit.
I can only conclude the same code is executed differently in both cases.
Is there some sort of (soft) power reset after setup()
? That could be a potential cause, as the color is stored in volatile memory on the WS2812B (if I'm not mistaken).
Link to the library used: http://github.com/cpldcpu/light_ws2812