我有问题。我正在使用Arduino控制WS2812B,但是呼吸效果有问题。我在看起来像这样的代码中创建了一个类:
String breathDirection = "Down";
class Nanohex
{
private:
CRGB primary;
uint16_t mode;
uint8_t hueRandomness;
uint8_t brightness;
uint8_t selectedShape;
public:
Nanohex() : mode(1),
primary(CRGB(0, 0, 0)),
hueRandomness(50),
brightness(0)
{
FastLED.addLeds<WS2812B, LED_PIN, BRG>(leds, TOTAL_LEDS);
}
void mode_breath()
{
if (brightness != 0)
{
fill_solid(leds, TOTAL_LEDS, primary);
float currentBrightness = FastLED.getBrightness();
if (currentBrightness <= 0)
{
breathDirection = "Up";
}
else if (currentBrightness >= brightness)
{
breathDirection = "Down";
}
float brightnessCorrection = brightness / 200;
if (breathDirection == "Down")
{
currentBrightness = currentBrightness - brightnessCorrection;
FastLED.setBrightness(currentBrightness);
}
else if (breathDirection == "Up")
{
currentBrightness = currentBrightness + brightnessCorrection;
FastLED.setBrightness(currentBrightness);
}
}
}
/* MORE CODE BELOW, BUT NOT RELEVANT */
现在呼吸效果仅在brightness
变量设置为200时起作用。如果我将其设置为200以下的其他值,则currentBrightness会一遍又一遍地返回相同的值,而brightnessDirection
返回Down
。
这里出了什么问题?
对于低于200的值,此表达式
float brightnessCorrection = brightness / 200;
由于integer division的行为,总是被评估为0。
尝试改成类似的东西>>
float brightnessCorrection = static_cast<float>(brightness) / 200;