Go Down

Topic: Problem coding color scale for matching game (Read 576 times) previous topic - next topic

Vehemency

Hey, i'm a complete beginner learning programming for the last week or so for my interactive assignment, but i've come at a dead end. The idea of the game has two controllers, the first player chooses a color using multiple inputs, locks the color with a button and the second player has to match that color using their own set of inputs and then each player swaps. I have no problems coding the turns, but I can't wrap my head around the color formula.

The scale itself is a simple 1-100 and a color on the color wheel corresponds to this scale. Basically i'm trying to get the scale and colorwheel to loop infinitely (eg. red=0 and 100), the output is determined by a specific value that is controlled by two (or more) inputs simultaneously.
For instance, a pressure sensor would increase the value by its input and a potentiometer would reduce the value by its input but at a slower rate and if the value passes 100 it starts back at 0 and vice versa. Of course each input value would be mapped to a smaller scale so it wouldn't change too quick. After all that, the color that corresponds to that value on the colorwheel would be output to the RGBLED.

I don't really understand how to write my own color forumla so I've been trying to integrate these two for days with no avail

code off sparkfun's sik guide 10
Code: [Select]
const int RED_LED_PIN = 9;    // Red LED Pin
const int GREEN_LED_PIN = 10; // Green LED Pin
const int BLUE_LED_PIN = 11;  // Blue LED Pin

const int SENSOR_PIN = 0;      // Analog input pin
int redValue, greenValue, blueValue;

void setup()
{
}

void loop()
{
  int sensorValue;
  sensorValue = analogRead(0);
  setRGB(sensorValue);
}

void setRGB(int RGBposition)
{
  int mapRGB1, mapRGB2, constrained1, constrained2;
  // http://sfecdn.s3.amazonaws.com/education/SIK/SchematicImages/Misc/RGB_function.jpg

  mapRGB1 = map(RGBposition, 0, 341, 255, 0);
  constrained1 = constrain(mapRGB1, 0, 255);

  mapRGB2 = map(RGBposition, 682, 1023, 0, 255);
  constrained2 = constrain(mapRGB2, 0, 255);

  redValue = constrained1 + constrained2;
  greenValue = constrain(map(RGBposition, 0, 341, 0, 255), 0, 255)
             - constrain(map(RGBposition, 341, 682, 0,255), 0, 255);
             
  blueValue = constrain(map(RGBposition, 341, 682, 0, 255), 0, 255)
            - constrain(map(RGBposition, 682, 1023, 0, 255), 0, 255);
  analogWrite(RED_LED_PIN, redValue);
  analogWrite(GREEN_LED_PIN, greenValue);
  analogWrite(BLUE_LED_PIN, blueValue);
}


code 2: off http://www.skylark-software.com/2011/01/arduino-notebook-rgb-led-and-color_21.html
Code: [Select]
int ledPins[] = { 11, 9, 10 };
int inputPin = 0;

void setup()
{
  pinMode(inputPin, INPUT);
  for(int i = 0; i < 3; i++)
    pinMode(ledPins[i], OUTPUT);
}

void loop()
{
  int inputValue = analogRead(inputPin);
  int hueValue = map(inputValue, 0, 1024, 0, 360);

  setHueValue(hueValue);
}

void setHueValue(int hueValue)
{
  setColor(ledPins, hsvToRgb(hueValue, 1, 1));
}

void setColor(int* led, const byte* color)
{
  for(int i = 0; i < 3; i++)
    analogWrite(led[i], 255-color[i]);
}

byte rgb[3];

byte* hsvToRgb(int h, double s, double v)
{
        // Make sure our arguments stay in-range
        h = max(0, min(360, h));
        s = max(0, min(1.0, s));
        v = max(0, min(1.0, v));
        if(s == 0)
        {
                // Achromatic (grey)
                rgb[0] = rgb[1] = rgb[2] = round(v * 255);
                return rgb;
        }
        double hs = h / 60.0; // sector 0 to 5
        int i = floor(hs);
        double f = hs - i; // factorial part of h
        double p = v * (1 - s);
        double q = v * (1 - s * f);
        double t = v * (1 - s * (1 - f));
        double r, g, b;
        switch(i)
        {
                case 0:
                        r = v;
                        g = t;
                        b = p;
                        break;
                case 1:
                        r = q;
                        g = v;
                        b = p;
                        break;
                case 2:
                        r = p;
                        g = v;
                        b = t;
                        break;
                case 3:
                        r = p;
                        g = q;
                        b = v;
                        break;
                case 4:
                        r = t;
                        g = p;
                        b = v;
                        break;
                default: // case 5:
                        r = v;
                        g = p;
                        b = q;
        }
        rgb[0] = round(r * 255.0);
        rgb[1] = round(g * 255.0);
        rgb[2] = round(b * 255.0);
        return rgb;
}


I tried to modify them to get a simple formula like; currentcolorvalue +(inputvalue1+inputvalue2) = newcolor. But I realise that these codes aren't made that way, the variables seem to be able to be called inside each function only, plus the scale still doesn't loop, so now I am completely lost how to approach this
/using adurino uno

Any help would be appreciated, thanks

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy