1

私の Arduino プロジェクトには、72 個の LEDを備えたNeopixel RGB ストリップがあります。

どの LED の色も正常に変更できます (現時点では、テスト目的で最初の 0 のみを設定しています)。したがって、ここでの配線は問題ではなく、コーディングの問題であることがわかります。

私がやりたいのは、色を選択してから別の色を選択し、最初の色を次の色にフェードさせることです (iPhone アプリケーションを使用するときに LIFX 電球が動作するのと同じように)。

これは私が現時点で持っているものです:

何が起こっているのかを示すために、すべての変数の出力をログに記録しています。どこが間違っているのか、または私が求めていることを行うためのより簡単な方法があるかどうかについては、100% 確信が持てません (私は提案を受け付けています)。

この関数は、commandコンマで区切られた文字列であるというパラメーターを受け取ります。

例: 255, 0, 0(赤) または0, 255, 0(緑)。

/*******************************************************************************
 * Function Name  : tinkerSetColour
 * Description    : Sets the strip with the appropriate colour
 * Input          : Pin and value
 * Output         : None.
 * Return         : 1 on success and a negative number on failure
 *******************************************************************************/
int Rstart = 0, Gstart = 0, Bstart = 0;
int Rnew = 0, Gnew = 0, Bnew = 0;

int tinkerSetColour(String command)
{
    sprintf(rgbString, "Rstart %i, Gstart %i, Bstart %i", Rstart, Gstart, Bstart);
    Spark.publish("rgb", rgbString);

    sprintf(rgbString, "Rnew %i, Gnew %i, Bnew %i", Rnew, Gnew, Bnew);
    Spark.publish("rgb", rgbString);

    // Clear strip.
    strip.show();

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

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

    int Rend = red, Gend = grn, Bend = blu;

    sprintf(rgbString, "Rend %i, Gend %i, Bend %i", Rend, Gend, Bend);
    Spark.publish("rgb", rgbString);

    // Larger values of 'n' will give a smoother/slower transition.
    int n = 200;
    for (int i = 0; i < n; i++)
    {
        Rnew = Rstart + (Rend - Rstart) * i / n;
        Gnew = Gstart + (Gend - Gstart) * i / n;
        Bnew = Bstart + (Bend - Bstart) * i / n;

        // Set pixel color here.
        strip.setPixelColor(0, strip.Color(Rnew, Gnew, Bnew));
    }

    sprintf(rgbString, "Rnew %i, Gnew %i, Bnew %i", Rnew, Gnew, Bnew);
    Spark.publish("rgb", rgbString);

    Rstart = red, Gstart = grn, Bstart = blu;

    sprintf(rgbString, "Rstart %i, Gstart %i, Bstart %i", Rstart, Gstart, Bstart);
    Spark.publish("rgb", rgbString);

    return 1;
}

問題は、色が色あせていないことです。

これのいずれかが混乱している場合はお詫び申し上げます。必要に応じて、さらに情報を提供できます。

最初にREDを選択した出力は次のとおりです。

Rstart 0, Gstart 0, Bstart 0
Rnew 0, Gnew 0, Bnew 0
Rend 255, Gend 0, Bend 0
Rnew 253, Gnew 0, Bnew 0

GREENを直接選択した出力は次のとおりです。

Rstart 255, Gstart 0, Bstart 0
Rnew 253, Gnew 0, Bnew 0
Rend 0, Gend 255, Bend 0
Rnew 2, Gnew 253, Bnew 0

その後、BLUEを選択した出力

Rstart 0, Gstart 255, Bstart 0
Rnew 2, Gnew 253, Bnew 0
Rend 0, Gend 23, Bend 255
Rnew 0, Gnew 25, Bnew 253
4

2 に答える 2