/*
   https://www.instructables.com/id/Action-Camera-RGB-Ring-Light/
   with 2 potentiometersto vary color and brightness array lists hex colours to use

   using the following to mix colours
   https://www.w3schools.com/colors/colors_mixer.asp

   Using attiny167 digispark pro
   Set for DigisparkPro 16mhz default
*/

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            1

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      24

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

/*
  long colour[] = { 0x191F61, 0x231D61, 0x2F1D61, 0x3D1D62, 0x4B1F62, 0x592062, 0x682462, 0x6F2562, 0x762862, 0x7E2A62,
                  0x802B50, 0x832E3E, 0x86312E, 0x893620, 0x8C3B15, 0x904019, 0x96441C, 0x9B491E, 0xA04D21, 0xA55124,
                  0xA85424, 0xAA5726, 0xAE5927, 0xB15C29, 0xB45F2A, 0xBD6C2E, 0xC77932, 0xD08636, 0xDA933B, 0xE5A13F };

*/
/*// rainbow colours black(off) to white
 long colour[] = { 0x000000, 0xff0000, 0xff9900, 0xfffb00, 0xb2ff00, 0x1aff00, 0x00ff99,
                  0x00ffff, 0x008cff, 0xfff68f, 0x001eff, 0x7300ff, 0xf00ff, 0xff007b,
                  0xffffff, 0xffffff
                };*/
/*
// 6 rainbow colours                
long colour[] = {0xfd0707, 0xff8a12, 0xdddb03, 0x129114, 0x0d0ddf, 0x8402ad};
*/                
//White to Yellow
/*                
long colour[] = {0xffffff, 0xfcfcf2, 0xfafae6, 0xf7f7d9, 0xf5f5cc, 0xf2f2bf, 0xf0f0b2, 0xededa6,
                 0xebeb99, 0xe8e88c, 0xe6e680, 0xe3e373, 0xe0e066, 0xdede59, 0xdbdb4d, 0xd9d940,
                 0xd6d633, 0xd4d426, 0xd1d119, 0xcfcf0d, 0xcccc00
                };
*/
/*                
// white to pale yellow (warm white)very subtle
long colour[] = {0xffffff, 0xfffff7, 0xfffff0, 0xffffe8, 0xffffe0, 0xffffd9, 0xffffd1, 0xffffc9, 0xffffc2, 0xffffba, 0xffffb2, 0xffffab, 0xffffa3, 0xffff9c, 0xffff94, 0xffff8c, 0xffff85, 0xffff7d, 0xffff75, 0xffff6e, 0xffff66};
*/

// 6 rainbow colours and white to pale yellow (warm white)very subtle 27 colours
/*
long colour[] = {0xfd0707, 0xff8a12, 0xdddb03, 0x129114, 0x0d0ddf, 
                 0x8402ad, 0xffffff, 0xfffff7, 0xfffff0, 0xffffe8, 
                 0xffffe0, 0xffffd9, 0xffffd1, 0xffffc9, 0xffffc2, 
                 0xffffba, 0xffffb2, 0xffffab, 0xffffa3, 0xffff9c, 
                 0xffff94, 0xffff8c, 0xffff85, 0xffff7d, 0xffff75, 
                 0xffff6e, 0xffff66};
*/

// 6 rainbow colours and white to pale yellow (warm white)very subtle 17 colours

long colour[] = {0xfd0707, 0xff8a12, 0xdddb03,0x129114, 0x0d0ddf, 
                 0x8402ad, 0xffffff, 0xfffff0,0xffffe0, 0xffffd1,  
                 0xffffc2, 0xffffb2, 0xffffa3,0xffff94, 0xffff85, 
                 0xffff75, 0xffff66};

/*
//blue to orange
long colour[] = {0x0000ff, 0x0d03f2, 0x1a05e6, 0x2608d9, 0x330acc, 0x400dbf, 0x4c0fb2, 0x5912a6, 0x661499, 0x73178c, 0x801a80, 0x8c1c73, 0x991f66, 0xa62159, 0xb2244d, 0xbf2640, 0xcc2933, 0xd92b26, 0xe62e19, 0xf2300d, 0xff3300};
*/
int colourNumber = 17; // number of colours in array
int delayval = 100; // delay for half a second
int brightPin  =   A11;  //analog pin used for potentiometer brightness
int colourPin = A12; // analog pin used for potentiometer colour

void setup() {

  Serial.begin(74880); // Wemos likes this rate

  Serial.println(__FILE__); // this prints the file name



  pinMode(brightPin, INPUT);
  pinMode(colourPin, INPUT);

  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {


  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
  int brightnessValue = analogRead(brightPin);
  int brightnessVal = map(brightnessValue, 0, 1023, 150, 0); // max is 255 but gets quite hot at full brightness
  int colourValue = analogRead(colourPin);
  int colourVal = map(colourValue, 0, 1023, 0, (colourNumber -1)); // change last value to 1 less than number of colours in the array

  pixels.setBrightness(brightnessVal);

  for (int i = 0; i < NUMPIXELS; i++) {

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    //pixels.setPixelColor(i, pixels.Color(255, 255, 255)); // Moderately bright green color.
    //  pixels.setPixelColor(i, pixels.Color(colourVal*brightnessVal/255, brightnessVal, brightnessVal)); // Moderately bright green color.

    // pixels.setPixelColor(i, 0x00cc00 ); //

    //  pixels.setPixelColor(i, colour[colourVal] ); //changes colours of individual leds

    pixels.fill(colour[colourVal] ); // changes colour/brightness of all leds

    //    pixels.fill(0xffffff);

    pixels.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).

  }
}
