首页 > 解决方案 > 模拟输入改变 RGB 颜色

问题描述

我正在为我的模拟转换器设置一个旋转角度传感器。有一个数字显示器可以显示我们在范围内的位置。我想做的是用模拟范围创建一个颜色范围,但无法弄清楚如何使颜色随着模拟范围而变化。

// This #include statement was automatically added by the Particle IDE.
#include <Grove_ChainableLED.h>

// This #include statement was automatically added by the Particle IDE.
#include <Grove_4Digit_Display.h>

#define NUM_LEDS 1 
ChainableLED leds(D2, D3, NUM_LEDS);

#define POT1 A0
#define LED1 D2
#define CLK D4
#define DIO D5

TM1637 tm1637(CLK,DIO);

int analogValue = 0;
float hue = 0.0;

void setup() 
{
    pinMode(LED1 , OUTPUT);
    pinMode(POT1 , INPUT);
    tm1637.set();
    tm1637.init();
    Serial.begin(9600);
}

void loop()
{
    analogValue = analogRead(POT1);
    hue = analogValue;
    analogWrite(LED1, map(analogValue, 0, 4095, 0, 255));
    tm1637.display(0, (analogValue / 1000));
    tm1637.display(1, (analogValue % 1000 /100));
    tm1637.display(2, (analogValue % 100 /10));
    tm1637.display(3, (analogValue % 10));
    leds.setColorHSB(0, map(hue, 0.0, 4095.0, 0.0, 1.0), 1.0, 0.2);
    delay(200);

}

标签: c++arduino-c++argon

解决方案


我能够重新调整我对光线改变颜色的看法。总的来说,我能够让它发挥作用。数字显示随着旋钮的转动而工作,一旦达到2000,颜色就会改变。

// This #include statement was automatically added by the Particle IDE.
#include <Grove_ChainableLED.h>

// This #include statement was automatically added by the Particle IDE.
#include <TM1637Display.h>

// This #include statement was automatically added by the Particle IDE.
#include <Grove_4Digit_Display.h>



#define POT1 A0
#define CLK D4
#define DIO D5

#define NUM_LEDS 1

TM1637 tm1637(CLK,DIO);
ChainableLED leds(D2, D3, NUM_LEDS);

int analogValue;
int hue = 0.0;


void setup() 
{

    pinMode(POT1 , INPUT);
    tm1637.set();
    tm1637.init();
    leds.init();
    Particle.variable("Displayed_Number", analogValue);
    leds.setColorRGB(0, 0, 0, 255);
}

void loop()
{


    //Reading Input From Knob Turning
    analogValue = analogRead(POT1);
    hue = analogValue;
    tm1637.display(0, (analogValue / 1000));
    tm1637.display(1, (analogValue % 1000 /100));
    tm1637.display(2, (analogValue % 100 /10));
    tm1637.display(3, (analogValue % 10));
    delay(200);


    if (analogValue > 2000) {
        //Turn Light On When Above Number
        leds.setColorRGB(0, 255, 0, 0);
    }

    if (analogValue <= 2000) {
        //Light Is Off If Below Number
        leds.setColorRGB(0, 0, 255, 0);
    }

}

推荐阅读