//26/06/2024 22:30
#include <Arduino.h>
#include <Button.h>
#include <ACI_10K_an.h>
Button button2(7);  // create ezButton object that attach to pin 7;
Button button5(8);  // create ezButton object that attach to pin 8;
byte LedState1 = LOW;
byte LedState2 = LOW;
Button button1(A2); // Connect your button between pin A2 and GND
Button button3(A5); // Connect your button between pin A5 and GND
Button button4(A3); // Connect your button between pin A3 and GND
// parametrização
int led, led1, led2;
int autVent = 0;
int celsius;
int autVeloc = 0;
int VentLador = 0;
int ioniZando = 0;
int ioniZa = 0;
int bomBeamento = 0;
int climaTiz = 0;
long alterVeloc = !0;
int idNiv = 0;
int modoVent = 0 ;
int nivelTanque = 0;
int velVent = 0;
int autLador = 0;
int modVel = 0;
int ionIzar = 0;
int pinNiv = 2 ; // pino nivelTanque
volatile int repiQue;
volatile int brightness = !0;    // how bright the LED is
volatile int fadeAmount = random (144, -144);   // how many points to fade the LED by
void velocidade();
void autovelocidade();
void bombeamento(void);
void oscilador(void);
void ionizador(void);
void serial(void);

void setup(void) {
  // Inicia a comunicação serial (monitor serial)
  while (!Serial) { }; // for Leos
	Serial.begin(9600);

  pinMode(A0, INPUT_PULLUP); // reserva
  pinMode(A1, INPUT_PULLUP);
  pinMode(A2, INPUT_PULLUP);
  pinMode(A3, INPUT_PULLUP);
  pinMode(A4, INPUT_PULLUP);
  pinMode(A5, INPUT_PULLUP);
  pinMode(1, OUTPUT); // pastilha Peltier
  pinMode(2, INPUT_PULLUP); //Nível do tanque
  pinMode(3, OUTPUT); // pino resistencia de aquecimento
  pinMode(4, OUTPUT); // pino alarme temperatura
  pinMode(5, OUTPUT); // pino led VentLador alto
  pinMode(6, OUTPUT); //pino led VentLador medio
  pinMode(7, INPUT_PULLUP); // pino VentLador
  pinMode(8, INPUT_PULLUP); // pino modoVent auto/temp
  pinMode(9, OUTPUT); //pino led VentLador baixo
  pinMode(10, OUTPUT); // pino led bomBeamento
  pinMode(11, OUTPUT); // pino oscilar
  pinMode(12, OUTPUT); //pino ioniZando
  pinMode(13, OUTPUT); // pino led nivelTanque
  //Give the random a random seed from the noise from the ADC of A0
  randomSeed(analogRead(A0));//parametro infinito
  analogReference(DEFAULT);
 
  /*
    A0
    A1 Temperatura
    A2 climatizar
    A3 pino oscilar
    A4 
    A5 pino ionizar
  */
  button1.begin();
  button2.begin(); // set debounce time to 50 milliseconds
  button3.begin();
  button4.begin();
  button5.begin(); // set debounce time to 50 milliseconds
}
void loop() {
  velocidade();
  autovelocidade();
  bombeamento();
  oscilador();
  ionizador();
  serial();
  noInterrupts();
  cli();
  // [ Critical Section ]
  interrupts();
  sei();
  // The Rest of The Code
  delay(random(500));
   ADMUX |= (0 & 0b0000);//É definido o pino A0 como INPUT.
  // Give some time to open serial monitor after programming
  delay(50);
}
void autovelocidade(void) {

  //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

  delayMicroseconds(500);
  celsius = (an10k.getTemp(analogRead(A1)));
  if (button5.pressed())
    (autVeloc = autVeloc + 1);
  if (autVeloc == 1) {
    autVeloc = 1;
    //digitalWrite(3, LOW);
    switch (celsius) {
      case -24 ... 18: {
          digitalWrite(1, LOW);
          digitalWrite(4, !digitalRead(4));
          digitalWrite(3, HIGH);
          delayMicroseconds(50);
          (alterVeloc = 1);
          break;
        }
      case 20 ... 24: {
          digitalWrite(4, !digitalRead(4));
          digitalWrite(3, !digitalRead(3));
          delayMicroseconds(50);
          digitalWrite(1, LOW);
          (alterVeloc = 2);
          break;
        }
      case 26 ... 40: {
          digitalWrite(4, !digitalRead(4));
          digitalWrite(1, !digitalRead(1));
          delayMicroseconds(50);
          digitalWrite(3, LOW);
          (alterVeloc = 3);
          break;
        }
      default: {
          digitalWrite(3, LOW);
          digitalWrite(1, LOW);
          digitalWrite(1, HIGH);
        }
    }
  }
  if (autVeloc >= 2) {
    digitalWrite(4, LOW);
    digitalWrite(3, LOW);
    (autVeloc = 0);
  }
}
void fader9() {
  // set the fader of pin 9:

  brightness = brightness + fadeAmount;
  // reverse the direction of the fading at the ends of the fade:
  if (random (brightness <= -255) || random(brightness >= 255) && random (brightness ^ 0)) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
  analogWrite(9, brightness);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
}
void fader6() {
  // set the fader of pin 6:
  analogWrite(6, brightness);
  brightness = brightness + fadeAmount;
  // reverse the direction of the fading at the ends of the fade:
  if (random (brightness <= -255) || random(brightness >= 255) && random (brightness ^ 0)) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
  digitalWrite(5, LOW);
  digitalWrite(9, LOW);
}
void fader5() {
  // set the fader of pin 5:
  analogWrite(5, brightness);
  brightness = brightness + fadeAmount;
  // reverse the direction of the fading at the ends of the fade:
  if (random (brightness <= -255) || random(brightness >= 255) && random (brightness ^ 0)) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
  digitalWrite(6, LOW);
  digitalWrite(9, LOW);
}
void velocidade(void) {

  // 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(9, HIGH);
  }
  if (alterVeloc == 2) {
    (alterVeloc = 2);
    digitalWrite(6, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(9, LOW);
  }
  if (alterVeloc == 3) {
    (alterVeloc = 3);
    digitalWrite(9, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
  }
  if (alterVeloc == 4) {
    (alterVeloc = 4);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(9, LOW);
  }
  if (alterVeloc == 5) {
    (alterVeloc = 5);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    fader9();
  }
  if (alterVeloc == 6) {
    (alterVeloc = 6);
    digitalWrite(5, LOW);
    digitalWrite(9, LOW);
    fader6();
  }
  if (alterVeloc == 7 ) {
    (alterVeloc = 7);
    digitalWrite(9, LOW);
    digitalWrite(6, LOW);
    fader5();
  }
  if ((alterVeloc >= 8))
  {
    (alterVeloc = 1);

  }
}
void bombeamento(void) {
  pinMode(2, INPUT);
  // inicio da parametrização do bombeamento
  // le o estado sensornivel: ligado (LOW) ou desligado (HIGH)
  nivelTanque = digitalRead(pinNiv);
 // noInterrupts();
  switch (nivelTanque) { // escolha caso o nível
    case 1 ... HIGH: { // quando nível está alto
        digitalWrite(13, HIGH); // liga o LED de nível baixo
        for (repiQue = 1; repiQue <= (-500); repiQue = repiQue + 1) {
          delay(repiQue);
        }
        digitalWrite(10, LOW); // desliga a bomba
        break;
      }
    default: { // quando o nível estiver baixo
        analogWrite(13, LOW); // desliga o LED de nível baixo
        if (button1.toggled()) { // inicio da parametrização do climatizador
          if (button1.read() == Button::PRESSED)
            digitalWrite(10, !digitalRead(10));
        }
      }
  }
}
void oscilador(void) {
  if (button4.toggled()) { // inicio da parametrização do oscilador
    if (button4.read() == Button::PRESSED)
      digitalWrite(11, !digitalRead(11));
  }
}
void ionizador(void) {
  if (button3.toggled()) {// inicio da parametrização do ionizador
    if (button3.read() == Button::PRESSED)
      digitalWrite(12, !digitalRead(12));
  }
}
void serial(void) {
  /*
    Serial.println();
    Serial.print("temp: ");
    Serial.println((int) celsius);
    Serial.flush();
    Serial.println();
    Serial.print("Nível: ");
    Serial.print(nivelTanque);
    Serial.flush();
    // Store the current time (start the clock)
    unsigned long millisWithFlushStart = millis();
    // Transmit the same psuedo-random text as before
    Serial.println(F("How long will it take to transmit the following:"));
    Serial.println(F("abcdefghijklmnopqrstuvwxyz"));
    Serial.println(F("done"));
    // This time, let TX buffer flush before "stopping" the clock
    Serial.flush();
    // Store the current time (stop the clock)
    unsigned long millisWithFlushStop = millis();

    // Print results for flushed calls
    Serial.print(F("WITH flush, Serial.println()s return control in: "));
    Serial.print( millisWithFlushStop - millisWithFlushStart);
    Serial.println(F(" milliseconds."));
    Serial.print("Fade: ");
    Serial.println(brightness);
    Serial.print(F("Interrupt count: "));
    noInterrupts();
    /*Serial.println(a);
    Serial.println(e);
    Serial.println(i);
    Serial.println(o);
    Serial.println(u);
    Serial.println(A);
    interrupts();
    delay(random(500));*/
}
$abcdeabcde151015202530354045505560fghijfghij