c++ Fade between colors? (Arduino)

前端 未结 3 1979
醉酒成梦
醉酒成梦 2021-01-28 01:11

I currently have this thats fading between 2 set colours:

for(int i=0;i

        
3条回答
  •  死守一世寂寞
    2021-01-28 01:56

    It's impossible to fade to pink beacuse you are starting from red and ending with green.

    To avoid this kind of mistake I suggest you to write an object oriented code.

    If you don't want to write the classes to handle a 3D vectonr you can use the Arduino Tinker Library

    I wrote this example for you:

    #include 
    #include 
    
    Tinker::Vect3d red(255,0,0);
    Tinker::Vect3d green(0,255,0);
    Tinker::SerialLog serialLog;
    
    void setup(){
      Serial.begin(9600);
      serialLog.display("Fade color example");  
      serialLog.endline();
    }
    
    void loop(){
      //fade factor computation
      const uint32_t t   = millis()%10000;
      const float cosArg = t/10000.*3.1415*2;
      const float fade   = abs(cos(cosArg));
    
      //Here's the color computation... as you can see is very easy to do!! :)
      Tinker::Vect3d finalColor(red*fade + green*(1-fade));
    
      //We print the vect3d on the arduino serial port
      Tinker::LOG::displayVect3d(finalColor,&serialLog);
      serialLog.endline();
      delay(500);
    }
    

    Which prints the following output on the serial port

    Fade color example
    V[255;0;0]
    V[242;12;0]
    V[206;48;0]
    V[149;105;0]
    V[78;176;0]
    V[0;254;0]
    V[79;175;0]
    V[150;104;0]
    V[206;48;0]
    V[242;12;0]
    V[254;0;0]
    V[242;12;0]
    V[205;49;0]
    V[148;106;0]
    V[77;177;0]
    V[1;253;0]
    V[80;174;0]
    V[151;103;0]
    

    hope that this helps :)

提交回复
热议问题