Kan iemand mij uitleggen hoe (of nog beter: waarom*
) onderstaande code precies werkt?
Boven setup() doe ik:
const uint8_t mySegment[] = {0,1,2,3};
In de main-loop roep ik dit aan als:
breatheUpdate(mySegment, 5, 1, 0);
Wat ik dus echt niet snap is hoe dit kan werken zonder dat er "ergens" (extern) buiten de functie zélf een variabele wordt gebruikt om de huidige "status" in op te slaan. Iets dat ik zelf wel altijd gebruik om vervolgens met behulp van millis() iets te doen.
Iemand die dit kan uitleggen?
*ik heb deze code ergens online gevonden en hij werkt perfect --> 4 LED's van een NeoPixel LED-strip faden hiermee netjes aan/uit
[edit]
Ik vraag dit overigens niet alleen uit interesse/nieuwsgierigheid, ik zou het ook graag weten om een probleempje op te lossen:
De aanroep in de main-loop werkt opzich heel goed, maar als ik tussendoor iets anders heb gedaan (servotje laten bewegen) dan knipperen de LED's eerst even heel snel (dus niet meer "breathend").
Dat vind ik niet mooi en zou ik graag willen oplossen. Dus daarom was ik opzoek naar die "externe" variabele om het "breathen" opnieuw te laten beginnen
De 2e vraag is dan eigenlijk ook: hoe kan het dat de LED's ineens heel snel gaan knipperen nadat de microprocessor even iets anders heeft gedaan? Het lijkt er net op alsof "er iets in te halen is" ofzo
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| void breatheUpdate(const uint8_t * segment, const uint32_t increment, const uint8_t step, const uint8_t lowLimit)
{
static uint32_t lastTimeChange = 0;
static uint8_t direction = 1;
static uint8_t value = lowLimit;
if(millis() - lastTimeChange > increment)
{
value += (direction * step);
if (value <= lowLimit || value >= 255)
{
direction = direction * -1;
}
for(uint8_t i = 0; i < sizeof(segment); i++)
{
strip.setPixelColor(segment[i], strip.Color(0, 0, value));
}
strip.show();
lastTimeChange += increment;
}
} |
Boven setup() doe ik:
const uint8_t mySegment[] = {0,1,2,3};
In de main-loop roep ik dit aan als:
breatheUpdate(mySegment, 5, 1, 0);
Wat ik dus echt niet snap is hoe dit kan werken zonder dat er "ergens" (extern) buiten de functie zélf een variabele wordt gebruikt om de huidige "status" in op te slaan. Iets dat ik zelf wel altijd gebruik om vervolgens met behulp van millis() iets te doen.
Iemand die dit kan uitleggen?
*ik heb deze code ergens online gevonden en hij werkt perfect --> 4 LED's van een NeoPixel LED-strip faden hiermee netjes aan/uit
[edit]
Ik vraag dit overigens niet alleen uit interesse/nieuwsgierigheid, ik zou het ook graag weten om een probleempje op te lossen:
De aanroep in de main-loop werkt opzich heel goed, maar als ik tussendoor iets anders heb gedaan (servotje laten bewegen) dan knipperen de LED's eerst even heel snel (dus niet meer "breathend").
Dat vind ik niet mooi en zou ik graag willen oplossen. Dus daarom was ik opzoek naar die "externe" variabele om het "breathen" opnieuw te laten beginnen
De 2e vraag is dan eigenlijk ook: hoe kan het dat de LED's ineens heel snel gaan knipperen nadat de microprocessor even iets anders heeft gedaan? Het lijkt er net op alsof "er iets in te halen is" ofzo
[ Voor 21% gewijzigd door Atmoz op 28-10-2021 10:04 ]










:strip_exif()/f/image/OVlaBX7KHdcLoncILtcyJLmt.jpg?f=fotoalbum_large)

:strip_exif()/f/image/26v9U2ja4o6GFipzcYNnst9o.jpg?f=fotoalbum_large)
:strip_exif()/f/image/9lhMUtC9Y2DlQojDxxpxbpwT.jpg?f=fotoalbum_large)
:strip_exif()/f/image/p7WzIPXHBaUvokM9HZqr7Otb.jpg?f=fotoalbum_large)
:no_upscale():strip_icc():strip_exif()/f/image/FoTjaGX8bCQIkv0WN7VWKIxj.jpg?f=user_large)
:strip_exif()/f/image/UuYs6HTretvY5etpbflsvL1h.jpg?f=fotoalbum_large)
:strip_exif()/f/image/vk5S48hZAMTm6cbjbBC2P35o.jpg?f=fotoalbum_large)
:strip_exif()/f/image/vpYwjMygMF8M33kIoSiNdkzb.jpg?f=fotoalbum_large)
:no_upscale():strip_icc():strip_exif()/f/image/xkEqe2nSTM9ZrHHbBsrXKgI8.jpg?f=user_large)
:strip_exif()/f/image/V0TAtneJjpS67c48TZLDzd4z.jpg?f=fotoalbum_large)
:strip_exif()/f/image/EDggPdH5Y9Xz3uNNDzd5XqS9.jpg?f=fotoalbum_large)
:strip_exif()/f/image/UoW9mmnPh6PkHdLiplFsBGoM.jpg?f=fotoalbum_large)
/f/image/dwjsYFfB5khSdJy21m5GmGJl.webp?f=fotoalbum_large)
:strip_exif()/f/image/UvZotiHG32Q2OwlhRub4Veds.jpg?f=fotoalbum_large)
:strip_exif()/f/image/4vxZLEOHzjg2anhSmk8bdG2y.png?f=user_large)
:strip_exif()/f/image/S475Qo5exmvUxu51SKbTYo9s.jpg?f=fotoalbum_large)
:no_upscale():strip_icc():strip_exif()/f/image/aV3olzrUfdYZpv9529AHFwbW.jpg?f=user_large)
:strip_exif()/f/image/O0vR4NIUQWOdwxHBP9UsowKk.jpg?f=fotoalbum_large)
:strip_exif()/f/image/PMalY72fazrOo9OZ8JNxeE9v.jpg?f=fotoalbum_large)
:no_upscale():strip_icc():strip_exif()/f/image/02abpUXQV9JcRtINk51VmboK.jpg?f=user_large)
:strip_exif()/f/image/ibEYgkPTxncZgDvAkhhS1lcB.jpg?f=fotoalbum_large)
:strip_exif()/f/image/OU9U69prLiWCqbL4rmr97mcU.jpg?f=fotoalbum_large)
:strip_exif()/f/image/53q67ZBHBJPXDhfuwrQ5fnvC.jpg?f=fotoalbum_large)
:strip_exif()/f/image/KA0rvEuiFrhYplQEZQAzP9pq.jpg?f=fotoalbum_large)
:no_upscale():strip_icc():strip_exif()/f/image/kIMVGl3WU8l31TphEZR9iSWW.jpg?f=user_large)
:no_upscale():strip_icc():strip_exif()/f/image/tJmxdg3CgAokYU842NgH8elt.jpg?f=user_large)
:strip_exif()/f/image/kp1KeI1trvlbpayGXxHfZ4Ky.jpg?f=fotoalbum_large)
:strip_exif()/f/image/mxbt4tNOj0HZ3BrbCJSx5cfa.jpg?f=fotoalbum_large)
:strip_exif()/f/image/WjCEiYWZKxDvjvABlJrpY8lJ.jpg?f=fotoalbum_large)
:strip_exif()/f/image/8pcNvtkjD53c5kajjWjlap4Z.jpg?f=fotoalbum_large)
:strip_exif()/f/image/WtTTfU6ecXAfJ9ruiB0iTEoI.jpg?f=fotoalbum_large)
:no_upscale():strip_icc():strip_exif()/f/image/TFapQFKi54qhTksttZEqwM2K.jpg?f=user_large)
:strip_exif()/f/image/dAOBzRp5mP4NPb32UGwKqJv3.jpg?f=fotoalbum_large)
:no_upscale():strip_exif()/f/image/qnN4y3W0Xpofuna8Lu1LPRID.webp?f=user_large)
/f/image/MKNDTAqQ5M4IPZGQYMgab500.png?f=fotoalbum_large)
:strip_exif()/f/image/eBFWmtHI0SSIOyTzRfJv5WkI.jpg?f=fotoalbum_large)
:strip_exif()/f/image/9RVmtH16LXjqVg7eLD1MTgjR.jpg?f=fotoalbum_large)
:no_upscale():strip_icc():strip_exif()/f/image/cRHCR6KAWTnjjiEy6TumMtDA.jpg?f=user_large)
:no_upscale():strip_icc():strip_exif()/f/image/gFnB4npNu2JhMgONMSVytdpX.jpg?f=user_large)
:strip_exif()/f/image/gpFmSRZFgVNZlzQsTxCxH3Sx.jpg?f=fotoalbum_large)
:strip_exif()/f/image/ql4IydQOcDoe9pyJlO63h20U.jpg?f=fotoalbum_large)
:strip_exif()/f/image/w47sf3ktG7ir3ShN6WUYS3Tl.jpg?f=fotoalbum_large)
:strip_exif()/f/image/yeteI8zmWB5yvSIjDj5ZAE2s.jpg?f=fotoalbum_large)
:strip_exif()/f/image/C4hM11QGxdFzJdw1SwMOXB76.jpg?f=fotoalbum_large)
:strip_exif()/f/image/q1ELIOUzRAyDdYPCef44cTRU.jpg?f=fotoalbum_large)
:no_upscale():strip_icc():strip_exif()/f/image/tFE3UDFjgT4qwDUqYmcAVLbU.jpg?f=user_large)
:no_upscale():strip_icc():strip_exif()/f/image/ic4R6dsv7WxNmJ8Axa7MDiwW.jpg?f=user_large)
:no_upscale():strip_icc():strip_exif()/f/image/9qsiCY8ofHKWgUsNJrt5psxI.jpg?f=user_large)
:strip_exif()/f/image/KzNpQ0v15ZPIk47mwuL2RLCk.jpg?f=fotoalbum_large)
/f/image/kf8CM8CjlnyXWNraCJHU0Nme.png?f=fotoalbum_large)
:strip_exif()/f/image/aoZGHPQcYWl1nYMuFnNLFrsy.jpg?f=fotoalbum_large)
:strip_exif()/f/image/B35SlE2Kxdz5vuYFAbsCKGfH.jpg?f=fotoalbum_large)
:strip_exif()/f/image/dSnsClbMDnb1qXKDk7HAQMM3.jpg?f=fotoalbum_large)
:strip_exif()/f/image/mpStzRvwJZs8NQKfVaD0gHyE.jpg?f=fotoalbum_large)
:strip_exif()/f/image/iWmkET7z2wCNSc4Zyx9MvQkB.jpg?f=fotoalbum_large)
:strip_exif()/f/image/eIu59ej9vyOb7SVceXgg7wX9.jpg?f=fotoalbum_large)
:no_upscale():strip_icc():strip_exif()/f/image/AEE5e2pNhjXVtQdpJDhNJDGn.jpg?f=user_large)
:strip_exif()/f/image/mcz5BmiIJ90JkwpLGu8ceNUc.jpg?f=fotoalbum_large)
:strip_exif()/f/image/34D9kwPCi2ZNpxTpkroGzMEJ.jpg?f=fotoalbum_large)
:strip_exif()/f/image/YFYCbMc6F01YBMobpCU31kTQ.jpg?f=fotoalbum_large)
:strip_exif()/f/image/4Mklf1iCgJ2ZmGoUfv53ORFb.jpg?f=fotoalbum_large)
:strip_exif()/f/image/Veppvmp3YhMDH3nUvrr1OrYs.jpg?f=fotoalbum_large)
:strip_exif()/f/image/uv8VG7D3G17USwux4VF066qd.jpg?f=fotoalbum_large)
:no_upscale():strip_icc():strip_exif()/f/image/iUK9AXC9h4gKfynK18uBH1LX.jpg?f=user_large)
:strip_exif()/f/image/xzlFgul3WX3rKIKONM0B1dT7.jpg?f=fotoalbum_large)
:strip_exif()/f/image/t5GLNSLyoyQrkZF9n3dfMQaV.jpg?f=fotoalbum_large)
:strip_exif()/f/image/fZ71CgKT4DRicGCaT9GiJH90.jpg?f=fotoalbum_large)
:strip_exif()/f/image/8CFESMIW1ZyP24Yb9CgAFBTg.jpg?f=fotoalbum_large)
:strip_exif()/f/image/Xk4hIYTVYLgdEc8fUlTz8Hz3.jpg?f=fotoalbum_large)
:no_upscale():strip_icc():strip_exif()/f/image/sBS3C2lzFWCXFy3SBo6L7wwd.jpg?f=user_large)
:strip_exif()/f/image/jLRQEI5vWcKrZocKWmVOJ8vs.jpg?f=fotoalbum_large)
:strip_exif()/f/image/cTqysmFyvE3YrtuWWsNSkqa1.jpg?f=fotoalbum_large)
:strip_exif()/f/image/USrWmHGivnpQ1VroqTWLst3P.jpg?f=fotoalbum_large)
:strip_exif()/f/image/MMXLT1mJ61dvyODfBMMGF6M8.jpg?f=fotoalbum_large)
:strip_exif()/f/image/lwUvvboCcA4R4IHvY007Zpqw.jpg?f=fotoalbum_large)