#include <Arduino.h>
#include <DHT.h>
/* Integrantes do grupo:
Antonio de Luca Ribeiro Silva RM 560169
Enzo Ribeiro Vilela de Azevedo RM 560250
Lucas Almeida de Siqueira RM 560914
Paulo Sérgio França Barbosa RM 559914
*/
// Mapeamento e Configuração
// INPUT (Esquerda (0) -> Direita (n))
#define BT_0 7
#define BT_1 9
#define BT_2 11
#define BT_3 13
// OUTPUT
#define LED_BLU 3
#define LED_RED 4
#define LED_YEL 5
#define LED_GRE 6
// Sensores
#define DHTPin 2
#define DHTType DHT22
DHT dht(DHTPin, DHTType);
// Variáveis
bool stBT3, flag3;
bool stBT2, flag2;
bool stBT1, flag1;
bool stBT0, flag0;
void setup()
{
Serial.begin(9600);
dht.begin();
// INPUT
pinMode(BT_0, INPUT_PULLUP);
pinMode(BT_1, INPUT_PULLUP);
pinMode(BT_2, INPUT_PULLUP);
pinMode(BT_3, INPUT_PULLUP);
// OUTPUT
pinMode(LED_BLU, OUTPUT);
pinMode(LED_RED, OUTPUT);
pinMode(LED_YEL, OUTPUT);
pinMode(LED_GRE, OUTPUT);
}
void loop()
{
// loopExercise1();
// loopExercise2();
// loopExercise3();
loopExercise4();
// loopExercise5();
}
// Exercício 1
int countExercise1 = 0;
void loopExercise1()
{
bt0ActionsExercise1();
delay(10);
}
void bt0ActionsExercise1()
{
stBT0 = digitalRead(BT_0);
// Borda de descida
if (stBT0 == LOW && flag0 == LOW)
flag0 = HIGH;
// Borda de subida
if (stBT0 == HIGH && flag0 == HIGH)
{
countExercise1++;
if (countExercise1 >= 5)
{
digitalWrite(LED_BLU, HIGH);
Serial.println("Meta atingida!");
while (true)
;
}
blinkBlueLedLoopExercise1(countExercise1);
flag0 = LOW;
}
}
void blinkBlueLedLoopExercise1(int times)
{
for (int i = 0; i < times; i++)
{
digitalWrite(LED_BLU, HIGH);
delay(200);
digitalWrite(LED_BLU, LOW);
delay(200);
}
}
// Exercício 2
int tickExercise2;
bool endGameExercise2;
int scoreboard1Exercise2, scoreboard2Exercise2;
bool ledBlueStExercise2, ledRedStExercise2;
int period1Exercise2, period2Exercise2;
void loopExercise2()
{
bt2ActionsExercise2();
if (!endGameExercise2)
{
bt0ActionsExercise2();
bt1ActionsExercise2();
checkWinnerExercise2();
tickExercise2++;
blinkExercise2();
}
delay(10);
}
void bt0ActionsExercise2()
{
stBT0 = digitalRead(BT_0);
// Borda de descida
if (stBT0 == LOW && flag0 == LOW)
flag0 = HIGH;
// Borda de subida
if (stBT0 == HIGH && flag0 == HIGH)
{
scoreboard1Exercise2++;
flag0 = LOW;
}
}
void bt1ActionsExercise2()
{
stBT1 = digitalRead(BT_1);
// Borda de descida
if (stBT1 == LOW && flag1 == LOW)
flag1 = HIGH;
// Borda de subida
if (stBT1 == HIGH && flag1 == HIGH)
{
scoreboard2Exercise2++;
flag1 = LOW;
}
}
void bt2ActionsExercise2()
{
stBT2 = digitalRead(BT_2);
// Borda de descida
if (stBT2 == LOW && flag2 == LOW)
flag2 = HIGH;
// Borda de subida
if (stBT2 == HIGH && flag2 == HIGH)
{
scoreboard1Exercise2 = 0;
ledBlueStExercise2 = LOW;
digitalWrite(LED_BLU, ledBlueStExercise2);
scoreboard2Exercise2 = 0;
ledRedStExercise2 = LOW;
digitalWrite(LED_RED, ledRedStExercise2);
endGameExercise2 = false;
flag2 = LOW;
}
}
void blinkExercise2()
{
period1Exercise2 = (scoreboard1Exercise2 > scoreboard2Exercise2) ? 20 : 40;
period2Exercise2 = (scoreboard2Exercise2 > scoreboard1Exercise2) ? 20 : 40;
if (tickExercise2 % period1Exercise2 == 0)
{
ledBlueStExercise2 = !ledBlueStExercise2;
digitalWrite(LED_BLU, ledBlueStExercise2);
}
if (tickExercise2 % period2Exercise2 == 0)
{
ledRedStExercise2 = !ledRedStExercise2;
digitalWrite(LED_RED, ledRedStExercise2);
}
}
void checkWinnerExercise2()
{
if (scoreboard1Exercise2 >= 10)
{
digitalWrite(LED_BLU, HIGH);
digitalWrite(LED_RED, LOW);
Serial.println("Vencedor: Jogador 1!");
endGameExercise2 = true;
}
if (scoreboard2Exercise2 >= 10)
{
digitalWrite(LED_BLU, LOW);
digitalWrite(LED_RED, HIGH);
Serial.println("Vencedor: Jogador 2!");
endGameExercise2 = true;
}
}
// Exercício 3
int scoreboard1Exercise3 = 0;
int scoreboard2Exercise3 = 0;
void loopExercise3()
{
bt0ActionsExercise3();
bt1ActionsExercise3();
bt2ActionsExercise3();
bt3ActionsExercise3();
delay(10);
}
void blinkExercise3(int port, int delayTime)
{
digitalWrite(port, HIGH);
delay(delayTime);
digitalWrite(port, LOW);
delay(delayTime);
}
void bt0ActionsExercise3()
{
stBT0 = digitalRead(BT_0);
// Detecção de borda de descida (HIGH ---> LOW)
if (stBT0 == LOW && flag0 == LOW)
{
Serial.println("O botão foi pressionado!!!");
flag0 = HIGH;
}
// Detecção de borda de subida (LOW ---> HIGH)
if (stBT0 == HIGH && flag0 == HIGH)
{
Serial.println("O botão foi desativado!!! + 1 Ponto Placar 1");
flag0 = LOW;
for (int i = 0; i <= scoreboard1Exercise3; i++)
{
blinkExercise3(LED_BLU, 500);
}
scoreboard1Exercise3++;
}
}
void bt1ActionsExercise3()
{
stBT1 = digitalRead(BT_1);
if (stBT1 == LOW && flag1 == LOW)
{
Serial.println("O botão foi pressionado!!!");
flag1 = HIGH;
}
if (stBT1 == HIGH && flag1 == HIGH)
{
Serial.println("O botão foi desativado!!! + 1 Ponto Placar 2");
flag1 = LOW;
for (int i = 0; i <= scoreboard2Exercise3; i++)
{
blinkExercise3(LED_RED, 500);
}
scoreboard2Exercise3++;
}
}
void bt2ActionsExercise3()
{
stBT2 = digitalRead(BT_2);
if (stBT2 == LOW && flag2 == LOW)
{
Serial.println("O botão foi pressionado!!!");
flag2 = HIGH;
}
if (stBT2 == HIGH && flag2 == HIGH)
{
Serial.println("O botão foi desativado!!! Conteúdo Apagado");
flag2 = LOW;
scoreboard1Exercise3 = 0;
scoreboard2Exercise3 = 0;
}
}
void bt3ActionsExercise3()
{
stBT3 = digitalRead(BT_3);
if (stBT3 == LOW && flag3 == LOW)
{
Serial.println("O botão foi pressionado!!!");
flag3 = HIGH;
}
if (stBT3 == HIGH && flag3 == HIGH)
{
Serial.println("O botão foi desativado!!! Analise de Pontuação");
flag3 = LOW;
digitalWrite(LED_YEL, HIGH);
if (scoreboard1Exercise3 > scoreboard2Exercise3)
{
lightLedBlueExercise3();
}
else if (scoreboard1Exercise3 < scoreboard2Exercise3)
{
lightLedRedExercise3();
}
else
{
blinkBothLedExercise3();
}
digitalWrite(LED_YEL, LOW);
}
}
void lightLedBlueExercise3()
{
digitalWrite(LED_BLU, HIGH);
delay(2000);
digitalWrite(LED_BLU, LOW);
}
void lightLedRedExercise3()
{
digitalWrite(LED_RED, HIGH);
delay(2000);
digitalWrite(LED_RED, LOW);
}
void blinkBothLedExercise3()
{
digitalWrite(LED_BLU, HIGH);
digitalWrite(LED_RED, HIGH);
delay(500);
digitalWrite(LED_BLU, LOW);
digitalWrite(LED_RED, LOW);
delay(500);
digitalWrite(LED_BLU, HIGH);
digitalWrite(LED_RED, HIGH);
delay(500);
digitalWrite(LED_BLU, LOW);
digitalWrite(LED_RED, LOW);
delay(500);
digitalWrite(LED_BLU, HIGH);
digitalWrite(LED_RED, HIGH);
delay(500);
digitalWrite(LED_BLU, LOW);
digitalWrite(LED_RED, LOW);
}
// Exercício 4
int stageExercise4 = 0;
void loopExercise4()
{
// Cada botão só é "escutado" (tem efeito) no estágio em que realmente importa:
// - bt0, bt1, bt2 são processados apenas nos seus respectivos cases
// - bt3 é o botão de reset, pode ser pressionado em qualquer momento do loop
switch (stageExercise4)
{
case 0:
lightOneLedExercise4(0); // Apaga todos os LEDs
bt0ActionsExercise4();
break;
case 1:
lightOneLedExercise4(LED_BLU);
bt1ActionsExercise4();
break;
case 2:
lightOneLedExercise4(LED_RED);
bt2ActionsExercise4();
break;
case 3:
lightOneLedExercise4(LED_YEL);
break;
case 4:
lightOneLedExercise4(LED_GRE);
bt0ActionsExercise4();
break;
}
bt3ActionsExercise4();
delay(10);
}
void lightOneLedExercise4(int port)
{
digitalWrite(LED_BLU, LED_BLU == port);
digitalWrite(LED_RED, LED_RED == port);
digitalWrite(LED_YEL, LED_YEL == port);
digitalWrite(LED_GRE, LED_GRE == port);
}
void bt0ActionsExercise4()
{
stBT0 = digitalRead(BT_0);
// Borda de descida
if (stBT0 == LOW && flag0 == LOW)
flag0 = HIGH;
// Borda de subida
if (stBT0 == HIGH && flag0 == HIGH)
{
if (stageExercise4 == 0)
stageExercise4++;
if (stageExercise4 == 4)
stageExercise4 = 1;
flag0 = LOW;
}
}
void bt1ActionsExercise4()
{
stBT1 = digitalRead(BT_1);
// Borda de descida
if (stBT1 == LOW && flag1 == LOW)
flag1 = HIGH;
// Borda de subida
if (stBT1 == HIGH && flag1 == HIGH)
{
if (stageExercise4 == 1)
stageExercise4++;
flag1 = LOW;
}
}
void bt2ActionsExercise4()
{
stBT2 = digitalRead(BT_2);
// Borda de descida
if (stBT2 == LOW && flag2 == LOW)
flag2 = HIGH;
// Borda de subida
if (stBT2 == HIGH && flag2 == HIGH)
{
if (stageExercise4 == 2)
stageExercise4++;
flag2 = LOW;
}
}
void bt3ActionsExercise4()
{
stBT3 = digitalRead(BT_3);
// Borda de descida
if (stBT3 == LOW && flag3 == LOW)
flag3 = HIGH;
// Borda de subida
if (stBT3 == HIGH && flag3 == HIGH)
{
if (stageExercise4 == 3)
stageExercise4++;
else
stageExercise4 = 0;
flag3 = LOW;
}
}
// Exercício 5
int tickExercise5;
int modeExercise5; // 0 - Ajuste de Temperatura | 1 - Ajuste de Umidade
String actualModeTextExercise5;
String setPointPreviewExercise5;
int potValueExercise5, flagPotValueExercise5;
float maxTempExercise5, tempExercise5, flagTempExercise5;
float minHumidExercise5, humidExercise5, flagHumidExercise5;
void loopExercise5()
{
potValueExercise5 = analogRead(A0);
tempExercise5 = dht.readTemperature();
humidExercise5 = dht.readHumidity();
bt0ActionsExercise5();
bt1ActionsExercise5();
monitorsValuesExercise5();
// Exibe as informações no monitor serial somente quando:
// 1. O contador (tickExercise5 | 1 tick = 10ms) atinge um múltiplo de 50 (controle
// de frequência | a cada 500ms);
// 2. Alguma das variáveis monitoradas (temperatura, umidade ou potenciômetro)
// apresenta valor diferente do último registrado.
// Dessa forma, evita-se impressão desnecessária e reduz-se o ruído no monitor.
if (tickExercise5 % 50 == 0 && (tempExercise5 != flagTempExercise5 ||
humidExercise5 != flagHumidExercise5 ||
potValueExercise5 != flagPotValueExercise5))
{
flagTempExercise5 = tempExercise5;
flagHumidExercise5 = humidExercise5;
flagPotValueExercise5 = potValueExercise5;
displayInfosExercise5();
}
tickExercise5++;
delay(10);
}
void bt0ActionsExercise5()
{
stBT0 = digitalRead(BT_0);
// Borda de descida
if (stBT0 == LOW && flag0 == LOW)
flag0 = HIGH;
// Borda de subida
if (stBT0 == HIGH && flag0 == HIGH)
{
if (modeExercise5 == 0)
{
modeExercise5 = 1;
Serial.println("Modo alterado para AJUSTE DE UMIDADE");
}
else
{
modeExercise5 = 0;
Serial.println("Modo alterado para AJUSTE DE TEMPERATURA");
}
displayInfosExercise5();
flag0 = LOW;
}
}
void bt1ActionsExercise5()
{
stBT1 = digitalRead(BT_1);
// Borda de descida
if (stBT1 == LOW && flag1 == LOW)
flag1 = HIGH;
// Borda de subida
if (stBT1 == HIGH && flag1 == HIGH)
{
if (modeExercise5 == 0)
{
maxTempExercise5 = map(potValueExercise5, 0, 1023, -40, 80);
Serial.println("Ajuste de Temperatura Maxima realizado com sucesso!");
}
if (modeExercise5 == 1)
{
minHumidExercise5 = map(potValueExercise5, 0, 1023, 0, 100);
Serial.println("Ajuste de Umidade Minima realizado com sucesso!");
}
displayInfosExercise5();
flag1 = LOW;
}
}
void monitorsValuesExercise5()
{
if (tempExercise5 > maxTempExercise5)
{
digitalWrite(LED_RED, HIGH);
if (tickExercise5 % 100 == 0)
Serial.println("ALARME! TEMPERATURA ALTA!");
}
else
{
digitalWrite(LED_RED, LOW);
}
if (humidExercise5 < minHumidExercise5)
{
digitalWrite(LED_BLU, HIGH);
if (tickExercise5 % 100 == 0)
Serial.println("ALARME! UMIDADE BAIXA!");
}
else
{
digitalWrite(LED_BLU, LOW);
}
}
void displayInfosExercise5()
{
actualModeTextExercise5 = modeExercise5 == 0 ? "AJUSTE DE TEMPERATURA" : "AJUSTE DE UMIDADE";
setPointPreviewExercise5 = modeExercise5 == 0
? String(map(potValueExercise5, 0, 1023, -40, 80)) + " °C"
: String(map(potValueExercise5, 0, 1023, 0, 100)) + "%";
Serial.println("===================================");
Serial.println("MODO: " + actualModeTextExercise5);
Serial.println("Set Point Temperatura: " + String(maxTempExercise5) + " °C");
Serial.println("Set Point Umidade: " + String(minHumidExercise5) + " %");
Serial.println("Temperatura Atual: " + String(tempExercise5) + " °C");
Serial.println("Umidade Atual: " + String(humidExercise5) + " %");
Serial.println("Valor do Potenciômetro: " + String(potValueExercise5) + " (" + setPointPreviewExercise5 + ")");
Serial.println("===================================");
}