首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Sparkcore从彩色到彩色的新阿杜诺褪色

使用Sparkcore从彩色到彩色的新阿杜诺褪色
EN

Stack Overflow用户
提问于 2014-10-16 01:09:46
回答 1查看 3.4K关注 0票数 0

这个问题是我在这里问的另一个问题answered的后续问题。

我的职能如下:

代码语言:javascript
复制
MotiColor startColor;
MotiColor endColor;

void setup()
{
    // Begin strip.
    strip.begin();

    // Initialize all pixels to 'off'.
    strip.show();

    Serial1.begin(9600);

    startColor = MotiColor(0, 0, 0);
    endColor = MotiColor(0, 0, 0);
}

void loop () {
}

int tinkerSetColour(String command)
{
    strip.show();

    int commaIndex = command.indexOf(',');
    int secondCommaIndex = command.indexOf(',', commaIndex+1);
    int lastCommaIndex = command.lastIndexOf(',');

    String red = command.substring(0, commaIndex);
    String grn = command.substring(commaIndex+1, secondCommaIndex);
    String blu = command.substring(lastCommaIndex+1);

    startColor = MotiColor(red.toInt(), grn.toInt(), blu.toInt());

    int16_t redDiff = endColor.getR() - startColor.getR();
    int16_t greenDiff = endColor.getG() - startColor.getG();
    int16_t blueDiff = endColor.getB() - startColor.getB();

    int16_t _delay = 500;
    int16_t duration = 3500;
    int16_t steps = duration / _delay;

    int16_t redValue, greenValue, blueValue;

    for (int16_t i = steps; i >= 0; i--) {
        redValue = (int16_t)startColor.getR() + (redDiff * i / steps);
        greenValue = (int16_t)startColor.getG() + (greenDiff * i / steps);
        blueValue = (int16_t)startColor.getB() + (blueDiff * i / steps);

        sprintf(rgbString, "%i,%i,%i", redValue, greenValue, blueValue);
        Spark.publish("rgb", rgbString);

        for (uint16_t i = 0; i < strip.numPixels(); i++) {
            strip.setPixelColor(i, strip.Color(redValue, greenValue, blueValue));
        }

        delay(_delay);
    }

    delay(_delay);

    for (uint16_t i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(endColor.getR(), endColor.getG(), endColor.getB()));
    }

    delay(_delay);

    endColor = MotiColor(startColor.getR(), startColor.getG(), startColor.getB());

    return 1;
}

我正确地看到了公布的结果:

这来自于OFF (0,0,0) ->红色(250,0,0) ->绿(0,255,0)。

当我通过Spark.publish()事件将结果发布回网络控制台时,它工作得很好,但是实际的Neopixel并不像预期的那样从颜色褪色到颜色。它们只是从一种颜色变化到另一种颜色,而不是它们之间很好地褪色。

我只是想知道我哪里出了问题,或者如何改进我的代码,这样我才能实时地看到衰落。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-17 13:07:16

必须在for循环中调用strip.show(),如下所示:

代码语言:javascript
复制
for (int16_t i = steps; i >= 0; i--) {
    redValue = (int16_t)startColor.getR() + (redDiff * i / steps);
    greenValue = (int16_t)startColor.getG() + (greenDiff * i / steps);
    blueValue = (int16_t)startColor.getB() + (blueDiff * i / steps);

    sprintf(rgbString, "%i,%i,%i", redValue, greenValue, blueValue);
    Spark.publish("rgb", rgbString);

    for (uint16_t i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(redValue, greenValue, blueValue));
    }



    // !!! Without this, you'll only see the result the next time you call
    // tinkerSetColor() !!!
    strip.show();



    delay(_delay);
}

要了解发生了什么,您可以查看NeoPixel库源代码。您将看到strip.setPixelColor()只将RGB值存储在内存中(将其视为绘图缓冲区,以便您可以立即更新整个条条,如果您查看控制器芯片是如何工作的,这是有意义的)。调用strip.show()将导致将值推送到串行中的每个像素的例程运行。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26394668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档