0

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

}
Ashham93
  • 33
  • 5
  • 3
    Right. Just one thing though - you've forgotten to mention what the problem is. – enhzflep Feb 29 '20 at 23:29
  • Just updated that, sorry for missing the most important part haha! I have the analog range showing on my digital reader, but I am not sure how to set up the range of color's to match the analog range and to change when going through that analog range. – Ashham93 Feb 29 '20 at 23:48
  • I suppose you'd just map your read values to the range [0..360] then use that as the angle for the HSV or HSB colour systems. If your range is 0..4095, simply divide by 11.375 - Though obviously, I'm just guessing wildly here. It would help if your question included the rang of measured values, since the answers will vary if you have a min of 0 versus a min of non-zero. (With a zero for the min value, no need to use map, you can just divide) Also, the expected range of input for the hue would be helpful(necessary). I.e I want to map 0-4095 to 0-360 or 47-4058 to 0-255. :) – enhzflep Mar 01 '20 at 00:10
  • @enhzflep that is one direction I was trying to go with, but I am unsure how to set up that range with the color coordination. So when in the first set of range have it as color blue, then in the next step being purple, etc. – Ashham93 Mar 03 '20 at 20:45
  • Well, since rgb(0,0,255) comes out to be a hue of 240°, you could just add 240° to your value, before modding by 360 (val%360) and finally, inputting into the colour-conversion function.. (I'm assuming it needs a 0-360 input for hue here) – enhzflep Mar 04 '20 at 00:46

1 Answers1

0

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

}
Ashham93
  • 33
  • 5