#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
#define BOT1 32 // ↑ — pull-up externo na PAT
#define BOT2 34 // ↓ — pull-up externo na PAT
#define BOT3 39 // ← — pull-up externo na PAT (não suporta INPUT_PULLUP)
#define BOT4 35 // → — INPUT_PULLUP
#define LEDR 19
#define LEDG 18
#define LEDB 5
#define TLED 26
#define TSEG1 27
#define TSEG2 14
enum Estado { MENU1, MENU2_A, MENU2_B, APAGADO };
Estado estadoAtual = MENU1;
Estado estadoAntes = MENU1;
int corAtual = 0;
unsigned long antRGB = 0;
void setRGB(int r, int g, int b) {
analogWrite(LEDR, r);
analogWrite(LEDG, g);
analogWrite(LEDB, b);
}
void aplicaCor() {
if (estadoAtual == MENU1) {
switch (corAtual) {
case 0: setRGB( 255, 0, 0); break; // verde
case 1: setRGB( 0, 255, 0); break; // azul
case 2: setRGB(0, 0, 255); break; // vermelho
case 3: setRGB(128, 105, 0); break; // amarelo
}
} else if (estadoAtual == MENU2_A || estadoAtual == MENU2_B) {
switch (corAtual) {
case 0: setRGB(255, 80, 0); break; // laranja
case 1: setRGB(150, 0, 128); break; // roxo
case 2: setRGB(250, 0, 80); break; // magenta
case 3: setRGB( 0, 80, 250); break; // ciano
}
}
}
void atualizaRGB() {
if (estadoAtual == APAGADO) {
setRGB(255, 255, 255);
return;
}
int totalCores = 4;
unsigned long intervalo = (estadoAtual == MENU1) ? 700 : 400;
if (millis() - antRGB >= intervalo) {
antRGB = millis();
corAtual = (corAtual + 1) % totalCores;
aplicaCor();
}
}
void atualizaOLED() {
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
switch (estadoAtual) {
case MENU1:
oled.setCursor(0, 0); oled.println("UTFPR Curitiba");
oled.setCursor(0, 16); oled.println("Lucas Horn 21");
break;
case MENU2_A:
oled.setCursor(0, 0); oled.println("Eng. Mecatronica");
oled.setCursor(0, 16); oled.println("Lucas Horn 21");
break;
case MENU2_B:
oled.setCursor(0, 0); oled.println("Eng. Mecatronica");
oled.setCursor(0, 16); oled.println("Sist.Microcontrol");
break;
case APAGADO:
oled.setCursor(0, 28); oled.println("Menu Apagado");
break;
}
oled.display();
}
bool bot1Ant = HIGH, bot2Ant = HIGH, bot3Ant = HIGH, bot4Ant = HIGH;
void verificaBotoes() {
bool bot1 = digitalRead(BOT1);
bool bot2 = digitalRead(BOT2);
bool bot3 = digitalRead(BOT3);
bool bot4 = digitalRead(BOT4);
// ↑ — Menu 1
if (bot1 == LOW && bot1Ant == HIGH) {
estadoAtual = MENU1;
corAtual = 0; antRGB = millis();
aplicaCor(); atualizaOLED();
}
// ↓ — avança no Menu 2 (1º clique: linha 1 | 2º clique: linha 1 + linha 2)
if (bot2 == LOW && bot2Ant == HIGH) {
if (estadoAtual == MENU1) estadoAtual = MENU2_A;
else if (estadoAtual == MENU2_A) estadoAtual = MENU2_B;
corAtual = 0; antRGB = millis();
aplicaCor(); atualizaOLED();
}
// ← — apaga tudo
if (bot3 == LOW && bot3Ant == HIGH) {
if (estadoAtual != APAGADO) estadoAntes = estadoAtual;
estadoAtual = APAGADO;
setRGB(255, 255, 255);
atualizaOLED();
}
// → — comuta entre MENU1 e MENU2_A sempre
if (bot4 == LOW && bot4Ant == HIGH) {
estadoAtual = (estadoAtual == MENU1) ? MENU2_A : MENU1;
corAtual = 0; antRGB = millis();
aplicaCor(); atualizaOLED();
}
bot1Ant = bot1; bot2Ant = bot2;
bot3Ant = bot3; bot4Ant = bot4;
}
void setup() {
Serial.begin(115200);
pinMode(TLED, OUTPUT); digitalWrite(TLED,HIGH );
pinMode(TSEG1, OUTPUT); digitalWrite(TSEG1, LOW);
pinMode(TSEG2, OUTPUT); digitalWrite(TSEG2, LOW);
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
pinMode(BOT1, INPUT);
pinMode(BOT2, INPUT);
pinMode(BOT3, INPUT);
pinMode(BOT4, INPUT); // GPIO 32 suporta pull-up
Wire.begin();
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED nao encontrado!");
while (true);
}
atualizaOLED();
corAtual = 0;
antRGB = millis();
aplicaCor();
}
void loop() {
verificaBotoes();
atualizaRGB();
delay(10);
}D1
D2
S1
S2
S3
S4
S6
S5
⮕
⮕
⮕
⮕
Ⓐ
Ⓑ