# include <Adafruit_NeoPixel.h>
# include <ezButton.h>

# define UNIT     79    // 1024 / (2 * PORCH + CONTROL)

# define PORCH    4
# define CONTROL  5
# define SPAN (PORCH + CONTROL)

const int HOT = (PORCH + CONTROL) * UNIT;
const int COLD = PORCH * UNIT;  

int fuzz = UNIT / 2;

# define xLED 2
# define POT  A0

// pot fuzz
const int pf341 = COLD; // + UNIT / 2;
const int pf683 = HOT; // - UNIT / 2;
  





# define N_REAL  ((PORCH + CONTROL) << 1) // 18 // until features
# define PIN  8

// Global variables for lights and CAN communication
Adafruit_NeoPixel led(N_REAL, PIN, NEO_GRB + NEO_KHZ800);

# define THREE  3
ezButton button[THREE] = (5, 6, 7);

void setup() {
  Serial.begin(115200); Serial.println("first things first");

;//
Serial.print(HOT); Serial.print(" <- HOT   ");
Serial.print(COLD); Serial.print(" <- COLD ");
//Serial.print(pf341); Serial.print(" ");
//Serial.print(pf683); Serial.print(" ");
Serial.println("");
;

  pinMode(xLED, OUTPUT);
  digitalWrite(xLED, HIGH);

  for (byte ii = 0; ii < THREE; ii++)
    button[ii].setDebounceTime(20);

  led.begin();
  led.setPixelColor(1,  0xff0000);
  led.setPixelColor(13, 0x00ff00);
  led.show();
  delay(777);
}

bool isOn;

void loop() {
//  fuzz = 0;           // fuzz FUZZ or no fuzz

  static unsigned long lastTime;
  unsigned long now = millis();

  if (now - lastTime < 33) return;
  lastTime = now;

  int lampX;

  int pot = analogRead(POT);
  pot = constrain(pot, 0, 1019);    // maths sue me

  if (pot > (HOT - fuzz)) isOn = false;
  if (pot < (COLD + fuzz)) isOn = true;

  static bool pOn;
  if (pOn != isOn) {
    Serial.print(pot);
    Serial.println(isOn ? " turned me on" : " turned me off");
    pOn = isOn;
  }

  if (isOn) {
    digitalWrite(xLED, HIGH);
  }
  else {
    digitalWrite(xLED, LOW);
  }

// select which bar to place and draw one pixel 
// isOn not. bar part 10..19 represents a porch + control from 0 to UNIT * PORCH 
// isOn. bar part 0..10 is a control and a porch
// sry. this was harder than it should have been

  led.clear();
  if (!isOn) {
    byte p1 = (pot - COLD) / UNIT;
    if (pot >= COLD) 
//      if (p1 < SPAN) led.setPixelColor((SPAN - 1 - p1) + SPAN, 0xc00040);
      if (p1 < SPAN) led.setPixelColor(p1 + SPAN, 0x2000d0);
  }
  else {
    byte p0 = pot / UNIT;
//    if (p0 < SPAN) led.setPixelColor(SPAN - 1 - p0, 0xc02040);
    if (p0 < SPAN) led.setPixelColor(p0, 0xff0000);
  }





//  led.setPixelColor(lampX, 0xc00040);


  led.show();
}


/*
  if (!isOn) {
    digitalWrite(xLED, HIGH);

// isOn not. bar part 10..19 represents a porch + control from 0 to UNIT * PORCH 

lampX = pot / UNIT + 10;

//    pot = constrain(pot, p f341, 1023);
//    lampX = map(pot, p f341, 1023, 10, 20);
  }
  else {

// isOn. bar part 0..10 is a control and a porch
    digitalWrite(xLED, LOW);

lampX = (pot - PORCH * UNIT) / UNIT;
//    pot = constrain(pot, 0, p f683);
//    lampX = map(pot, 0, p f683, 0, 10);
  }

  led.setPixelColor(lampX, 0xc00040);
  led.show();
}
*/

/*
Serial.print("pot = "); Serial.print(pot);
Serial.print("       pot - COLD = "); Serial.print(pot - COLD);

Serial.print("       p0 = "); Serial.print(p0); 
Serial.print("       p1 = "); Serial.print(p1); 

Serial.println("");
*/

/*
static unsigned long progTime;
static int fpot;
boolean dir = true;

if (now - progTime > 200) {
  progTime = now;
  if (dir) {
Serial.print(fpot); Serial.println(" fpot");
    fpot += 60;
    if (fpot >= 1024) {
      fpot = 1023;
       dir = false;
    }
  }
  else {
Serial.print(fpot); Serial.println(" fpot");
    fpot -= 60;
    if (fpot < 0) {
      fpot = 2;
       dir = true;
    }     
  }
}

    int pot = fpot;
*/
0. . . . . . . . . . . . . . . . . . . . . . . . . 8
9. . . . . . . . . . . . . . . . . . . . . . . . .17
v0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v12
HEAT