【问题标题】:Neopixel arduino fading from colour to colour using a SparkcoreNeopixel arduino 使用 Sparkcore 从颜色渐变到颜色
【发布时间】:2014-10-16 01:09:46
【问题描述】:

这个问题是我在此处提出的另一个问题 answered 的后续问题。

我有以下功能:

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) -> RED (255,0,0) -> GREEN (0,255,0)。

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

我只是想知道我哪里出错了,或者我如何改进我的代码,以便我真正看到实时衰减。

【问题讨论】:

  • 从一种颜色变为另一种颜色需要多少时间?

标签: c++ arduino rgb led


【解决方案1】:

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

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() 会导致将值以串行方式推送到每个像素的例程运行。

【讨论】:

    猜你喜欢
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 2011-03-06
    • 1970-01-01
    相关资源
    最近更新 更多