// LCD1602 and Pi Pico!
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
#include <Adafruit_NeoPixel.h>
#define PIN 16  // input pin Neopixel is attached to
#define NUMPIXELS      16 // number of neopixels in strip
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval1 = 300; // timing delay in milliseconds
int delayval2 = 1500;
int redColor = 0;
int greenColor = 0;
int blueColor = 0;
int LedPmax = 0;
int LedFmax = 0;
int BrewStart = 1; // pump allowance
int P9b = 0; //pump pressure exceeded 9bar
int Fullshot = 0; // Output flow exceeded the requested amount
int BrewDone = 0; //the brew is done. -> turn off pump, open 3way
void setup() {
  lcd.begin(16, 2);
  lcd.print("Hello 3N+1!");
  Serial.begin(115200);
  pixels.begin();
  /*lcd.setCursor(0, 1);
    lcd.print("> Pi Pico <");
  */
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
}
void loop() {
  pixels.clear();
  int Psens = analogRead(A0);
  int Flowsens = analogRead(A1);
  int LedP = Psens / 1023.0 * NUMPIXELS;
  int LedF = Flowsens / 1023.0 * NUMPIXELS;
  if (LedPmax < LedP) {
    LedPmax = LedP;
  }
  if (LedFmax < LedF) {
    LedFmax = LedF;
  }
  Serial.println(Psens);
  Serial.println(Flowsens);
  delay(50);
  // flip the brew pressure value
  if (LedPmax == NUMPIXELS) {
    P9b = 1;
  }
  // Brew pressure build up showcase
  if (LedP != 0&& P9b != 1)
  { pixels.fill(0xff0000, NUMPIXELS - LedPmax, LedPmax);
    pixels.show();
  }
  else {
    pixels.clear();
    pixels.show();
  }
  // Brew amount showcase
  if (LedF != 0 &&  P9b == 1) {
    pixels.fill(0x00ff00, 0, LedF);
    pixels.show();
  }
  if (LedFmax == NUMPIXELS) {
    Fullshot = 1;
  }
// End title - Need to make independent
  if (BrewStart == 1 && Fullshot == 1) {
    for (int i = 0; i < 10; i++) {
      // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
      pixels.setBrightness(1);
      pixels.show();
      delay(250);
      pixels.setBrightness(255);
      pixels.show();
      delay(250);
    }
  }
  lcd.clear();
lcd.setCursor(0, 0);
  lcd.print(P9b);
  lcd.setCursor(2, 0);
  lcd.print(Fullshot);
 lcd.setCursor(4, 0);
  lcd.print(BrewDone);
  lcd.setCursor(0, 1);
  lcd.print(LedP);
  lcd.setCursor(6, 1);
  lcd.print(Psens);
  //here starts the LEDRING
  /*
    for (int i = NUMPIXELS - 1; i > -1; i--) {
      // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
      pixels.setPixelColor(i, pixels.Color(255, 0, 0));
      // This sends the updated pixel color to the hardware.
      pixels.show();
      // Delay for a period of time (in milliseconds).
      delay(delayval1 / NUMPIXELS); //From pressure sensor
    }
    //delay(delayval2/NUMPIXELS);
    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(0, 255, 0));
      // This sends the updated pixel color to the hardware.
      pixels.show();
      // Delay for a period of time (in milliseconds).
      delay(delayval2 / NUMPIXELS); //from flow comparator
    }
    for (int i = 0; i < 10; i++) {
      // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
      pixels.setBrightness(1);
      pixels.show();
    delay(250);
      pixels.setBrightness(255);
      pixels.show();
    delay(250);
    }
  */
  //strip.fill(color, first, count)
}
/* void N3_1() { int nmax=0;
  for (int i = 1; i < 10000; i++) {
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("i=");
   lcd.print(i);
   int n = 0;
       lcd.setCursor(5, 1);
   lcd.print("nmax= ");
   lcd.print(nmax);
   //delay(1000);
   for (int j = i; j != 1;) {
     lcd.clear();
     lcd.setCursor(0, 0);
     lcd.print("i=");
     lcd.print(i);
     if (j % 2 == 0) {
       lcd.setCursor(0, 1);
       lcd.print("j= j/2 ");
       j = j / 2;
       lcd.setCursor(6, 0);
       lcd.print("j= ");
       lcd.print(j);
     }
     else {
       lcd.setCursor(0, 01);
       lcd.print("j= 3*j+1 ");
       j = 3 * j + 1;
       lcd.setCursor(6, 0);
       lcd.print("j= ");
       lcd.print(j);
     };
     n++;
     if (nmax < n) {
       nmax = n;
     };
     lcd.setCursor(10, 1);
     lcd.print(n);
    //delay(100);
    };
  };
  } */