#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include "config.h"
// Creo el objeto pantalla
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int buttonState = 0;
int lastButtonState = 0;
int potValue = 0;
int ledBrightness = 0;
int buttonPressCount = 0;
void setup() {
pinMode(potPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP); // en la placa tengo que usar INPUT_PULLUP
pinMode(blueLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(BAUDRATE);
Serial.println("Iniciando pantalla...");
displayInit();
delay(3000);
}
void loop() {
// Contador On|Off led azul
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPressCount++;
digitalWrite(blueLedPin, !digitalRead(blueLedPin));
Serial.println("Pulsador: " + String(buttonPressCount) + " acciones");
}
delay(100);
}
lastButtonState = buttonState;
// Intensidad led rojo
potValue = analogRead(potPin);
ledBrightness = map(potValue, 0, 4095, 0, 255);
analogWrite(redLedPin, ledBrightness);
// On|Off led verde
if (potValue > 2048) {
digitalWrite(relayPin, HIGH);
digitalWrite(greenLedPin, HIGH);
} else {
digitalWrite(relayPin, LOW);
digitalWrite(greenLedPin, LOW);
}
// Salida por display y consola
static unsigned long previousMillis = millis();
displayUpdateAndShow(100);
if(millis() - previousMillis > 100) {
previousMillis = millis();
Serial.print("Led A: ");
Serial.print(digitalRead(blueLedPin) ? "Encendido" : "Apagado");
Serial.print(" Led R: ");
Serial.print(map(potValue, 0, 4095, 0, 100));
Serial.print("% de potencia");
Serial.print(" Led V: ");
Serial.println(digitalRead(greenLedPin) ? "Encendido" : "Apagado");
}
}
const unsigned char epd_bitmap_UTN [] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf1, 0xe1, 0xe3, 0xfe, 0x1f, 0xfe, 0x10, 0x00, 0x01, 0x87, 0xff, 0x87, 0xff, 0xff, 0xff, 0xff,
0xf1, 0xe1, 0xe3, 0xfe, 0x1f, 0xfe, 0x10, 0x00, 0x01, 0x83, 0xff, 0x87, 0xff, 0xff, 0xff, 0xff,
0xf0, 0xe1, 0xc3, 0xfe, 0x1f, 0xfe, 0x10, 0x00, 0x01, 0x83, 0xff, 0x87, 0xff, 0xff, 0xff, 0xff,
0xf0, 0xe1, 0x83, 0xfe, 0x1f, 0xfe, 0x1f, 0xe0, 0xff, 0x81, 0xff, 0x87, 0xff, 0xff, 0xff, 0xff,
0xf0, 0x61, 0x87, 0xfe, 0x1f, 0xfe, 0x1f, 0xf0, 0xff, 0x80, 0xff, 0x87, 0xff, 0xff, 0xff, 0xff,
0xf8, 0x00, 0x07, 0xfe, 0x1f, 0xfe, 0x1f, 0xf0, 0xff, 0x80, 0x7f, 0x87, 0xff, 0xff, 0xff, 0xff,
0xfc, 0x00, 0x0f, 0xfe, 0x1f, 0xfe, 0x1f, 0xf0, 0xff, 0x80, 0x3f, 0x87, 0xff, 0xff, 0xff, 0xff,
0xfe, 0x00, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xf0, 0xff, 0x80, 0x3f, 0x87, 0xff, 0xff, 0xff, 0xff,
0xff, 0x00, 0x7f, 0xfe, 0x1f, 0xfe, 0x1f, 0xf0, 0xff, 0x84, 0x1f, 0x87, 0xff, 0xff, 0xff, 0xff,
0xf0, 0x00, 0x03, 0xfe, 0x1f, 0xfe, 0x1f, 0xf0, 0xff, 0x86, 0x0f, 0x87, 0xff, 0xff, 0xff, 0xff,
0xe0, 0x00, 0x03, 0xfe, 0x1f, 0xfe, 0x1f, 0xf0, 0xff, 0x87, 0x07, 0x87, 0xff, 0xff, 0xff, 0xff,
0xe0, 0x00, 0x03, 0xfe, 0x1f, 0xfe, 0x1f, 0xf0, 0xff, 0x87, 0x83, 0x87, 0xff, 0xff, 0xff, 0xff,
0xe0, 0x00, 0x03, 0xfe, 0x1f, 0xfe, 0x1f, 0xf0, 0xff, 0x87, 0x81, 0x87, 0xff, 0xff, 0xff, 0xff,
0xff, 0x80, 0x7f, 0xfe, 0x1f, 0xfe, 0x1f, 0xf0, 0xff, 0x87, 0xc1, 0x87, 0xff, 0xff, 0xff, 0xff,
0xff, 0x00, 0x3f, 0xfe, 0x0f, 0xfe, 0x1f, 0xf0, 0xff, 0x87, 0xe0, 0x87, 0xff, 0xff, 0xff, 0xff,
0xfe, 0x00, 0x1f, 0xff, 0x0f, 0xfc, 0x1f, 0xf0, 0xff, 0x87, 0xf0, 0x07, 0xff, 0xff, 0xff, 0xff,
0xf8, 0x00, 0x0f, 0xff, 0x0f, 0xfc, 0x3f, 0xf0, 0xff, 0x87, 0xf8, 0x07, 0xff, 0xff, 0xff, 0xff,
0xf8, 0x00, 0x07, 0xff, 0x07, 0xf8, 0x3f, 0xf0, 0xff, 0x87, 0xfc, 0x07, 0xff, 0xff, 0xff, 0xff,
0xf0, 0x61, 0x83, 0xff, 0x83, 0xf0, 0x7f, 0xf0, 0xff, 0x87, 0xfc, 0x07, 0xff, 0xff, 0xff, 0xff,
0xf0, 0xe1, 0xc3, 0xff, 0x80, 0x00, 0x7f, 0xf0, 0xff, 0x87, 0xfe, 0x07, 0xff, 0xff, 0xff, 0xff,
0xf0, 0xe1, 0xc3, 0xff, 0xc0, 0x00, 0xff, 0xf0, 0xff, 0x87, 0xff, 0x07, 0xff, 0xff, 0xff, 0xff,
0xe1, 0xe1, 0xe3, 0xff, 0xf0, 0x03, 0xff, 0xf0, 0xff, 0x87, 0xff, 0x87, 0xff, 0xff, 0xff, 0xff,
0xf1, 0xe3, 0xe3, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
void displayInit(){
display.begin(0x3C, true);
display.clearDisplay();
display.setTextColor(SH110X_WHITE);
display.drawBitmap(0,0,epd_bitmap_UTN,128,32,SH110X_BLACK,SH110X_WHITE);
display.display();
}
void displayUpdateAndShow(unsigned long interval){
static unsigned long previousMillis = 0;
if(millis() - previousMillis > interval)
{
previousMillis = millis();
display.clearDisplay();
display.setCursor(2,0);
display.println("Datos de Compilacion");
display.setCursor(0,15);
display.println("Pulsador: "+ String(buttonPressCount) + " acciones");
display.setCursor(0,24);
display.print("R: ");
display.print(map(potValue, 0, 4095, 0, 100));
display.println("%");
display.setCursor(54,24);
display.print("G: ");
display.println(digitalRead(greenLedPin) ? "E" : "A");
display.setCursor(103,24);
display.print("B: ");
display.println(digitalRead(blueLedPin) ? "E" : "A");
display.display();
}
}