// Include semaphore supoport
#include <Arduino.h>
#include <LiquidCrystal.h>
#include <Button.h>
#include <ACI_10K_an.h>
#include <FadeLed.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Button button2(17);  // create ezButton object that attach to pin 7;
Button button5(18);  // create ezButton object that attach to pin 8;
byte LedState1 = LOW;
byte LedState2 = LOW;
Button button1(22); // Connect your button between pin A0 and GND
Button button3(28); // Connect your button between pin A5 and GND
Button button4(26); // Connect your button between pin A3 and GND
// parametrização
int autVent = 0;
float celsius;
int autVeloc = 0;
int VentLador = 0;
int ioniZando = 0;
int ioniZa = 0;
int bomBeamento = 0;
int climaTiz = 0;
int alterVeloc = !0;
int idNiv = 0;
int modoVent = 0 ;
int nivelTanque;
int velVent = 0;
int autLador = 0;
int modVel = 0;
int ionIzar = 0;
void velocidade();
void autovelocidade();
void bombeamento();
void oscilador();
void ionizador();
void fader0();
void fader9();
void fader6();
void fader5();
//make two FadeLed objects for pin 16 (leds[0]) and pin 21 (leds[1])
FadeLed leds[4] = {19, 6, 5, 0};

//it doesn't need to be an array, you can also make separate named objects
//still all updated with FadeLed::update()

void setup() {
  // Inicia a comunicação serial (monitor serial)
  //Wire.begin();
  //Serial1.begin(115200);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Circular");
  delay(1000);
  lcd.clear();
  pinMode(0, 0); // reserva
  pinMode(21, INPUT_PULLUP);
  pinMode(22, INPUT_PULLUP);
  pinMode(26, INPUT_PULLUP);
  pinMode(27, INPUT_PULLUP);
  pinMode(28, INPUT_PULLUP);
  pinMode(1, OUTPUT); // pastilha Peltier
  pinMode(2, INPUT_PULLUP); //INTERRUPÇÃO no pin2 paa Velocidade
  pinMode(3, OUTPUT); // pino resistencia de aquecimento
  pinMode(4, OUTPUT); // pino auto
  pinMode(5, OUTPUT); // pino led VentLador alto
  pinMode(6, OUTPUT); //pino led VentLador medio
  pinMode(17, INPUT_PULLUP); // pino VentLador
  pinMode(18, INPUT_PULLUP); // pino modoVent auto/temp
  pinMode(19, OUTPUT); //pino led VentLador baixo
  pinMode(20, OUTPUT); // pino led bomBeamento
  pinMode(14, OUTPUT); // pino oscilar
  pinMode(15, OUTPUT); //pino ioniZando
  pinMode(13, OUTPUT); // pino led nivelTanque
  /*
    0
    21 Nível do tanque
    22 climatizar
    26 pino oscilar
    27 Temperatura
    28 pino ionizar
  */
  button1.begin();
  button2.begin(); // set debounce time to 50 milliseconds
  button3.begin();
  button4.begin();
  button5.begin(); // set debounce time to 50 milliseconds
  //set the interval (ms) between led updates
  //it's the same for all FadeLed objects
  //call BEFORE setTime() otherwise the time calculation is wrong
  FadeLed::setInterval(random(200));

  //for led[1] no gamma correction is used
  //leds[1].noGammaTable();

  //both LEDs will take 5 seconds to fade from 0 to max.
  leds[0].setTime(5000, true);
  leds[1].setTime(5000, true);
  leds[2].setTime(5000, true);

  //We fade it to full brightness
  //so will take 5 seconds
  leds[0].off();
  leds[1].off();
  leds[2].off();
}
void loop() {
  velocidade();
  autovelocidade();
  bombeamento();
  oscilador();
  ionizador();
  FadeLed::update();
}
void autovelocidade() {

  //Temperatura
  Aci_10K an10k; //start an instance of the library
  //Aci_10K an10k(3.3,12);//support for 3.3 volt board and/or 12bit analog read resolution Fahrenheit
  lcd.setCursor(0, 0);
  lcd.print("temp: ");
  lcd.print((float) celsius);

  celsius = (an10k.getTemp(analogRead(27)));
  if (button5.pressed())
    (autVeloc = autVeloc + 1);
  if (autVeloc == 1) {
    autVeloc = 1;
    lcd.setCursor(0, 1);
    lcd.print("temp/auto");
    switch ((int)celsius) {
      case -24 ... 18: {
          digitalWrite(1, LOW);
          digitalWrite(4, !digitalRead(4));
          digitalWrite(3, HIGH);
          delayMicroseconds(50);
          (alterVeloc = 1);
          lcd.setCursor(0, 1);
          lcd.print("temp/auto: baixa");
          break;
        }
      case 20 ... 24: {
          digitalWrite(4, !digitalRead(4));
          digitalWrite(3, !digitalRead(3));
          delayMicroseconds(50);
          digitalWrite(1, LOW);
          (alterVeloc = 2);
          lcd.setCursor(0, 1);
          lcd.print("temp/auto: media");
          break;
        }
      case 26 ... 40: {
          digitalWrite(4, !digitalRead(4));
          digitalWrite(1, !digitalRead(1));
          delayMicroseconds(50);
          digitalWrite(3, LOW);
          (alterVeloc = 3);
          lcd.setCursor(0, 1);
          lcd.print("temp/auto:  alta");
          break;
        }
      default: {
          digitalWrite(3, LOW);
          digitalWrite(1, LOW);
          digitalWrite(1, HIGH);
          lcd.setCursor(0, 1);
          lcd.print("                 ");
        }
    }
  }
  if (autVeloc >= 2) {
    digitalWrite(4, LOW);
    digitalWrite(3, LOW);
    (autVeloc = 0);
    lcd.setCursor(0, 1);
    lcd.print("                 ");
  }
}
void fader0() {
  FadeLed::update();
  leds[0].off();
  leds[1].off();
  leds[2].off();
  digitalWrite(19, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  alterVeloc = 1;
}

void fader9() {
  // set the fader of pin 16 ou 21:
  digitalWrite(19, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  leds[1].off();
  leds[2].off();
  // main function call to update all fadeLeds
  //must be called often to make it work
  FadeLed::update();

  //we let both LEDs fade up and down all the time
  //to see if it's done fading we can check .done()
  //Because they fade simultaneously in the same time only led[0] is checked
  if (leds[0].done()) {
    //.get() will return the current brightness
    //so this will tell us if we are not at 0
    if (leds[0].get()) {
      //then we are done fading up, let's fade down again
      leds[0].off();
    }
    //or we are at 0
    else {
      //then we are done fading down, let's fade up again
      leds[0].on();
    }
  }
}
void fader6() {
  // set the fader of pin 6:
  digitalWrite(19, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  leds[0].off();
  leds[2].off();
  // main function call to update all fadeLeds
  //must be called often to make it work
  FadeLed::update();

  //we let both LEDs fade up and down all the time
  //to see if it's done fading we can check .done()
  //Because they fade simultaneously in the same time only led[0] is checked
  if (leds[1].done()) {
    //.get() will return the current brightness
    //so this will tell us if we are not at 0
    if (leds[1].get()) {
      //then we are done fading up, let's fade down again
      leds[1].off();
    }
    //or we are at 0
    else {
      //then we are done fading down, let's fade up again
      leds[1].on();
    }
  }
}
void fader5() {
  // set the fader of pin 5:
  digitalWrite(19, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  leds[0].off();
  leds[1].off();
  // main function call to update all fadeLeds
  //must be called often to make it work
  FadeLed::update();

  //we let both LEDs fade up and down all the time
  //to see if it's done fading we can check .done()
  //Because they fade simultaneously in the same time only led[0] is checked
  if (leds[2].done()) {
    //.get() will return the current brightness
    //so this will tell us if we are not at 0
    if (leds[2].get()) {
      //then we are done fading up, let's fade down again
      leds[2].off();
    }
    //or we are at 0
    else {
      //then we are done fading down, let's fade up again
      leds[2].on();
    }
  }
}
void velocidade() {

  // instanciando um botão só
  // inicio da parametrização do VentLadordor
  if (button2.pressed())
    (alterVeloc = alterVeloc + 1);
  if (alterVeloc == 1) {
    (alterVeloc = 1);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(19, HIGH);
  }
  if (alterVeloc == 2) {
    (alterVeloc = 2);
    digitalWrite(6, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(19, LOW);
  }
  if (alterVeloc == 3) {
    (alterVeloc = 3);
    digitalWrite(19, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
  }
  if (alterVeloc == 4) {
    (alterVeloc = 4);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(19, LOW);
  }
  if (alterVeloc == 5) {
    (alterVeloc = 5);
    digitalWrite(19, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    fader9();
  }
  if (alterVeloc == 6) {
    (alterVeloc = 6);
    leds[0].off();
    digitalWrite(19, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    fader6();
  }
  if (alterVeloc == 7 ) {
    (alterVeloc = 7);
    leds[1].off();
    digitalWrite(19, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    fader5();
  }
  if (alterVeloc == 8)
  {
    (alterVeloc = 8);
    FadeLed::update();
    leds[0].off();
    leds[1].off();
    leds[2].off();
    digitalWrite(19, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
  }
  if (alterVeloc >= 9)
  {
     FadeLed::update();
    fader0();
  }
}
void bombeamento() {
  pinMode(2, INPUT);
  // inicio da parametrização do bombeamento
  // le o estado sensornivel: ligado (LOW) ou desligado (HIGH)
  nivelTanque = digitalRead(2);
  // noInterrupts();
  switch (nivelTanque) { // escolha caso o nível
    case HIGH: { // quando nível está alto
        digitalWrite(13, LOW); // liga o LED de nível baixo
        if (button1.toggled()) { // inicio da parametrização do climatizador
          if (button1.read() == Button::PRESSED)
            digitalWrite(20, !digitalRead(20));
        }
        break;
      }
    case LOW: {
        digitalWrite(13, HIGH); // desliga o LED de nível baixo
        delay(5000);
        digitalWrite(20, LOW); // desliga a bomba

        break;
      }
  }
}
void oscilador() {
  if (button4.toggled()) { // inicio da parametrização do oscilador
    if (button4.read() == Button::PRESSED)
      digitalWrite(14, !digitalRead(14));
  }
}
void ionizador() {
  if (button3.toggled()) {// inicio da parametrização do ionizador
    if (button3.read() == Button::PRESSED)
      digitalWrite(15, !digitalRead(15));
  }
}
$abcdeabcde151015202530354045505560fghijfghij
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT