/* Code developed by Choong Ng */

const int analogInPin=A0; //analog pin to which the potentiometer is attached
const int analogOutPin=6; //analog output pin to which the LED is attached

const int redPin=6; //analog output pin to which the LED is attached
const int greenPin=5; //analog output pin to which the LED is attached
const int bluePin=9; //analog output pin to which the LED is attached

int sensorValue=0; //value read from the potentiometer
int outputValue=0; //value output to the analog out pin

void setup(){
  Serial.begin(115200);
}

int limitedDistance(int a, int b, int limit) {
  return min(abs(a-b), limit);
}

int channelVal(int sensorVal, int hueCenter) {
  return (340 - limitedDistance(sensorVal, hueCenter, 340))/2;
}

void loop(){
  //read the analog value
  sensorValue=analogRead(analogInPin);
  //map it to the range of the analog out
  outputValue=map(sensorValue,0,1023,0,200);
  //change the analog out value
  analogWrite(redPin, max(channelVal(sensorValue, 0), channelVal(sensorValue, 1023)));
  analogWrite(bluePin, channelVal(sensorValue, 341));
  analogWrite(greenPin, channelVal(sensorValue, 683));
  //Serial.println(outputValue);
  delay(10);  
}
