#include <Wire.h> // libreria para bus I2C
#include <Adafruit_GFX.h> // libreria para pantallas graficas
#include <Adafruit_SSD1306.h> // libreria para controlador SSD1306
#include <LiquidCrystal_I2C.h>
#define ANCHO 128 // reemplaza ocurrencia de ANCHO por 128
#define ALTO 64 // reemplaza ocurrencia de ALTO por 64
#define OLED_RESET 4
const byte ENCODER_CLK = 2;
const byte ENCODER_DT = 3;
const byte ENCODER_BOTON = 4;
int estado_actual_CLK;
int estado_anterior_CLK = 1;
int contador = 0;
int rotaciones = 0;
int pos = 1;
int lectura_anterior = 1;
int n = 1;
byte RANGO_MINIMO_ENCODER = 0; // Valor mínimo del encoder
byte RANGO_MAXIMO_ENCODER = 6; // Valor máximo del encoder
byte vuelta = 0;
int x, minX; // para la presentacion de pantalla tft
unsigned long previousMillis1 = 0;
String titulo = "tren rapido, destino bilbao";
char mimensaje[] =" aqui andamos con otro tren";
Adafruit_SSD1306 oled(ANCHO, ALTO, &Wire, OLED_RESET); // crea objeto
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Wire.begin();
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.clearDisplay();
oled.setTextWrap(false);
oled.setTextSize(1);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("PROYECTO RAUL");
previousMillis1 = 0;
pinMode(ENCODER_CLK, INPUT); // pin del enoder
pinMode(ENCODER_DT, INPUT); // pin del encoder
pinMode(ENCODER_BOTON, INPUT_PULLUP); // boton del encoder
x = oled.width();
//minX = -12 * strlen(message); // 12 = 6 pixels/character * text size 2
minX = -12 * titulo.length(); // 12 = 6 pixels/character * text size 2
}
byte mar = 1;
byte controlamos = 1;
void loop() {
if (millis() - previousMillis1 > 5000) {
mar = mar +1;
previousMillis1 = millis();
if (mar>2) mar=1;
controlamos = 1;
}
switch(mar){
case 1:
cuadro();
salidas();
break;
case 2:
cuadro();
llegadas();
break;
}
lectura_encoder();
lecturaboton();
}
void cuadro() {
if (controlamos == 1) {
oled.clearDisplay();
oled.drawRect(00, 00, 128, 63, WHITE); // dibuja rectangulo
oled.setCursor(40, 10);
oled.drawFastHLine(01, 24, 127, WHITE);
oled.setTextSize(1); // establece tamano de texto en 2
oled.setTextColor(WHITE); // establece color al unico disponible (pantalla monocromo
oled.display();
}
}
void salidas() {
if (controlamos == 1) {
oled.setTextColor(WHITE);
oled.setCursor(40, 2 );
oled.print("SALIDAS");
oled.setCursor(2, 14);
oled.print("DESTINO HH:MM VIA");
int n= 27;
oled.setCursor(02, n);
oled.print("TARANCON 12:00 1");
n=n+8;
oled.setCursor(02, n);
oled.print("VALENCIA 02:00 2");
n=n+8;
oled.setCursor(02, n);
oled.print("BARCELONA 02:00 2");
n=n+8;
oled.setCursor(02, n);
oled.print("AVILA 02:00 3");
oled.display();
}
controlamos = 2;
}
void llegadas () {
if (controlamos == 1) {
oled.setTextColor(WHITE);
oled.setCursor(40, 2 );
oled.print("LLEGADAS");
oled.setCursor(2, 14);
oled.print("ORIGEN VIA");
int n= 27;
oled.setCursor(02, n);
oled.print("GUADALAJARA 1");
n=n+8;
oled.setCursor(02, n);
oled.print("BURGOS 2");
n=n+8;
oled.setCursor(02, n);
oled.print("SEVILLA 2");
n=n+8;
oled.setCursor(02, n);
oled.print("SORIA 1");
oled.display();
}
controlamos = 2;
}
void lectura_encoder() {
estado_actual_CLK = digitalRead(ENCODER_CLK);
if (estado_actual_CLK != estado_anterior_CLK && estado_actual_CLK == 1) {
if (digitalRead(ENCODER_DT) == estado_actual_CLK) {
contador--;
if (contador < 0) contador = RANGO_MAXIMO_ENCODER;
} else {
contador++;
if (contador > RANGO_MAXIMO_ENCODER) contador = 0;
}
rotaciones = contador;
if (pos != rotaciones) //
{
pos = rotaciones;
lcd_aviso();
}
}
estado_anterior_CLK = estado_actual_CLK;
}
void lecturaboton() {
bool estado_btn = digitalRead(ENCODER_BOTON);
if (lectura_anterior == 0 && estado_btn == 1) {
run_option(rotaciones);
delay(99);
}
lectura_anterior = estado_btn;
}
void run_option(int valor_pasado) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("EJECUNTANDO");
delay(1000);
lcd_aviso();
movimiento(titulo);
}
void lcd_aviso() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MOVEMOS");
lcd.setCursor(3, 1);
lcd.print(rotaciones);
}
void movimiento(String aviso){
//while (n < 10) {
unsigned long start = millis()+8000UL; // cargo a start con el valor +60mil milisegundos
while (millis() < start) {
oled.clearDisplay();
oled.setTextSize(2);
oled.setCursor(x, 10);
oled.print( aviso);
oled.display();
n++;
x = x - 8; // Scroll speed (make more positive to slow down the scroll)
if (x < minX) x = oled.width();
delay(55);
}
}