#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>

#ifdef __AVR__
#include <avr/power.h>
#endif

#define PIN 8
#define NUMPIXELS 6

SoftwareSerial mySerial(0, 1); // RX, TX from point of view of chip
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 25; // delay for half a second

int fadeAmount = 1;

int R = 0;
int G = 255;
int B = 255;
int Rval = 0;
int Bval = 0;
int brightness = 0;
bool fade = false;

int analogPin = 3; // potentiometer wiper (middle terminal) connected to analog pin 3 -- Physical pin 3
// outside leads to ground and +5V
long int val = 0; // variable to store the value read

void setup()
{
#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.
mySerial.begin(9600); // setup serial
}

void loop() {

val = 0 ;

for (int count = 0; count < 255; ++count) {
val = analogRead(analogPin) + val;
}

Rval = map(val,147600,219000,0,255);
Bval = map(val,147600,219000,0,255);

if (Rval < 5){
fade = true;
}
else{
fade = false;
}

mySerial.print("val = ");
mySerial.println(val); // debug value
// mySerial.print("fade = ");
//mySerial.println(fade);

// read the input pin (filtering, smooth off noise)

for (int i = 0; i < NUMPIXELS; i++) {

brightness += fadeAmount;
B = 255;
//G = G + fadeAmount;
//B = B + fadeAmount;

// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(Rval, G, B)); // Moderately bright green color.
pixels.show();


if (brightness >= 100) {
fadeAmount = -fadeAmount;
brightness = 100;
}

if (brightness <= 0) {
fadeAmount = -fadeAmount;
brightness = 0;
}

pixels.setBrightness(brightness);


if (fade) fadeAmount = -1;

//mySerial.print("brightness = ");
//mySerial.println(brightness);

delay(delayval); // Delay for a period of time (in milliseconds).

}

}