c++ - 平滑一个总是更新的值?

标签 c++ arduino smoothing

你将如何平滑一个总是更新的值,代码:

int nLEDs = 25; // Number of LED's 
int sensitivity = 1;

int incomingByte = 0; 
int r = 0;
int g = 0;
int b = 0;
int a = 0;

void loop() {

        if (Serial.available() > 0) {
          incomingByte = Serial.parseInt();
          a = (255 / 100) * (incomingByte * sensitivity);
        }

       // some code 
}

void fade(){
  for(int i=0;i<nLEDs;i++){
   r = a;
   g = 0;
   b = 0;

   FTLEDColour col = { r , g , b };
 }
}

void ColourFade(){
  for(int i=0;i<nLEDs;i++){
   r = a;
   g = 255 - a;
   b = 0;

   FTLEDColour col = { r , g , b };
  }
}

关于淡入淡出方法,您将如何平滑值的变化而不是仅仅跳跃?如果可能的话。

最佳答案

取而代之的是:

value = NewValue

做这样的事情:

value = (Value * 7 + NewValue + 7) / 8

这将导致 value 为旧值的 7/8 和新值的 1/8,导致它逐渐“漂移”到新值,而不是一下子跳到那里。

关于c++ - 平滑一个总是更新的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20458110/

相关文章:

c++ - 复制构造函数和移动构造函数具有相同的运行时间,并且未调用移动构造函数

python - get_includes 找不到标准库头文件

c++ - Arduino 仅在按下按钮时继续 for-loop

c++ - 如何将 int 从 int[] 转换为 const char*

css - 输入中框阴影的平滑动画

r - R smooth.spline(): smoothing spline is not smooth but overfitting my data

c++ - 继承类中的 g++ typedef 模板

c++ - 为什么实例化后什么也没有发生? (C++ 内存分配器实现)

python - 用 Python 模拟操纵杆

machine-learning - 朴素贝叶斯分类中的未知词