//bibliteca display
#include <LiquidCrystal.h>
byte temp1[8] = { B00100, B00100, B00100, B00100, B00100, B01110, B01110, B01110 };
byte sadFace1[8] = { B00000, B00000, B01010, B00000, B01110, B10001, B00000, B00000 };
byte happyFace1[8] = { B00000, B00000, B01010, B00000, B10001, B01110, B00000, B00000 };
byte midFace1[8] = { B00000, B00000, B01010, B00000, B00000, B01110, B00000, B00000 };
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
//biblioteca DHT
#include <DHT.h>
#define DHTPIN 13
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
//pinos led e buzzer
int redPin = 12;
int yellowPin = 11;
int greenPin = 10;
int buzzer = 9;
float temp;
float ph;
void setup() {
//iniciando os dispositivos
Serial.begin(9600);
lcd.begin(16, 2);
dht.begin();
//randomizador para o valor do ph
randomSeed(analogRead(0));
//configuracao da pinagem dos LEDs e Buzzer
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode (buzzer, OUTPUT);
//animacao de inicio
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(5,0);
lcd.print("Global");
lcd.setCursor(5,1);
lcd.print("Solution");
delay(5000);
lcd.clear();
//criacao dos Chars customizados para o lcd
lcd.createChar(0, temp1);
lcd.createChar(1, sadFace1);
lcd.createChar(2, happyFace1);
lcd.createChar(3, midFace1);
}
int gerarValorAleatorio() {
// Gera um número aleatório entre 1 e 15
return random(1, 15);
}
void loop() {
float mediaTemp = 0;
float mediaPh = 0;
//funcao para media dos valores de temp e ph
for(int i = 0; i < 10; i++) {
temp = dht.readTemperature();
ph = gerarValorAleatorio();
mediaTemp += temp;
mediaPh += ph;
delay(500);
}
temp = mediaTemp / 10;
ph = mediaPh / 10;
//desligando os LEDs e Buzzer
digitalWrite(greenPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(redPin, LOW);
noTone(buzzer);
//enviando informacoes para o terminal
Serial.print("temp: ");
Serial.print(temp);
Serial.println(" C");
Serial.print("ph: ");
Serial.println(ph);
delay(500);
//estrutura condicional para os LEDs e buzzer
if (7 <= ph && ph <= 9 && temp <= 26) {
digitalWrite(greenPin, HIGH);
digitalWrite(yellowPin, LOW);
digitalWrite(redPin, LOW);
delay(300);
}
if (ph >= 10 || temp >= 27) {
digitalWrite(redPin, HIGH);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
tone(buzzer,200,3000);
delay(3000);
noTone(buzzer);
delay(1000);
}
if (ph <= 6) {
digitalWrite(yellowPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(redPin, LOW);
delay(300);
tone(buzzer,800,3000);
delay(3000);
noTone(buzzer);
delay(1000);
}
//colocando os chars no display
lcd.noDisplay();
delay(500);
lcd.display();
lcd.setCursor(5, 0);
lcd.write(byte(0));
lcd.setCursor(11, 0);
lcd.write("ph");
delay(2000);
//estrutura condicional para o estado do ph e temp
if (temp <= 26) {
lcd.setCursor(5, 1);
lcd.write(byte(2));
} else if (temp >= 27) {
lcd.setCursor(5, 1);
lcd.write(byte(1));
}
if (ph <= 6) {
lcd.setCursor(11, 1);
lcd.write(byte(3));
} else if (7 <= ph && ph <= 9) {
lcd.setCursor(11, 1);
lcd.write(byte(2));
} else if (ph >= 10) {
lcd.setCursor(11, 1);
lcd.write(byte(1));
}
}