简体   繁体   中英

Analog Input To Change RGB Color

I am setting up a rotary angle sensor for my analog changer. There is a digital display to show where we are within the range. What I am trying to do is create a color range with the analog range but are unable to figure out how to make the colors change with the analog range.

// 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);

}

I was able to redirect how I was looking into having the light change color. Overall I was able to make it work. The digital display worked with the knob being turned, then once it reaches 2000 the color changes.

// 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);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM