String Ruta_Guinda = "Juan_Covarrubias_2,Calle_Cedron_1000,Pradera_Florida_33,Calle_Cedron_324,Calle_Cedron_64,Calle_Nanche_499,Las_Palmas_302,Las_Palmas_78_A,Malaquita_144,Avenida_La_Joya_417,Calle_Margarita_Maza_De_Juarez_51,Avenida_Fuentes_De_Morelia_311,Circuito_Periferico_Independencia_8719";
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Ticker.h>
Ticker desplazarTextoLCD;
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool state1;
bool state2;
int receivedData;
int textLength;
bool ms250_ok;
int posicion;
String token;
int contadorParadas = 13;
unsigned long previousMillis = 0;
#define DELAY_BTN 500 // 500ms
int i = 0;
int btnPress = 0;
long lastTime = 0;
bool LCDrefresh;
#define btn1 14
// #define btn2 26
// #define btn3 33
// #define btn4 32
void ISR(){
if (!digitalRead(btn1))
{
btnPress = 1;
}
// else if (!digitalRead(btn2)) btnPress = 2;
// else if (!digitalRead(btn3)) btnPress = 3;
// else if (!digitalRead(btn4)) btnPress = 4;
else btnPress = 0;
}
void setup() {
Serial.begin(9600);
pinMode(33, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.clear();
state1 = true;
state2 = false;
//int textLength;
receivedData = 0;
ms250_ok = false;
posicion = 0;
desplazarTextoLCD.attach_ms(150,desplazarLCDTexto);
LCDrefresh = false;
pinMode(btn1, INPUT_PULLUP); // INPUT_PULLDOWN/INPUT_PULLUP
// pinMode(btn2, INPUT_PULLUP); // INPUT_PULLDOWN/INPUT_PULLUP
// pinMode(btn3, INPUT_PULLUP); // INPUT_PULLDOWN/INPUT_PULLUP
// pinMode(btn4, INPUT_PULLUP); // INPUT_PULLDOWN/INPUT_PULLUP
attachInterrupt(btn1, ISR, FALLING); // LOW/HIGH/FALLING/RISING/CHANGE
// attachInterrupt(btn2, ISR, FALLING); // LOW/HIGH/FALLING/RISING/CHANGE
// attachInterrupt(btn3, ISR, FALLING); // LOW/HIGH/FALLING/RISING/CHANGE
// attachInterrupt(btn4, ISR, FALLING); // LOW/HIGH/FALLING/RISING/CHANGE
}
void loop() {
// if(state1)
// {
// // Obtener el token y aplicar desplazamiento
// String token = getTokenAtIndex(Ruta_Guinda, ',', 5);
// // scrollText(token, 5000); // Repetir cada 5 segundos (5000 milisegundos)
// lcd.clear();
// lcd.print(token);
// delay(150);
// int textLength = token.length();
// }
// while(1)
// {
// serialEvent();
if (btnPress)
{
if (millis()-lastTime > DELAY_BTN)
{
Serial.print("¡Interrupción ");
Serial.print(btnPress);
Serial.println("!");
ButtonPressed();
lastTime = millis();
}
btnPress = 0;
}
//Serial.println(i);
i++;
delay(200);
// ButtonPressed();
// while(posicion < textLength)
// {
// // serialEvent();
// ButtonPressed();
// ms250_ok = false;
// posicion = 0;
// posicion++;
// previousMillis = millis();
// while(ms250_ok == false)
// {
// // serialEvent();
// ButtonPressed();
// if (millis() - previousMillis >= 120)
// {
// lcd.scrollDisplayLeft();
// //delay(250);
// //posicion++;
// ms250_ok = true;
// }
// else
// {
// posicion--;
// }
// }
// }
// for (int posicion = 0; posicion < textLength; posicion++)
// {
// // previousMillis = millis();
// // if (millis() - previousMillis >= 250)
// // {
// lcd.scrollDisplayLeft();
// delay(250);
// //posicion++;
// // }
// // else
// // {
// // posicion--;
// // }
// }
// if (millis() - previousMillis >= strDelay)
// {
// previousMillis = millis();
// i++;
// // strobeRGB(i); // Rojo
// effectSelect(effectVal,i,255,100,0);
// // currentMillis = millis();
// //interval = (i+1)*interval;
// // flash_finished = 1;
// }
// // }
}
String getTokenAtIndex(String data, char delimiter, int index) {
int start = 0;
int end = data.indexOf(delimiter);
int currentIndex = 0;
while (end != -1) {
if (currentIndex == index) {
String token = data.substring(start, end);
// Reemplazar "_" por " "
token.replace("_", " ");
return token;
}
start = end + 1;
end = data.indexOf(delimiter, start);
currentIndex++;
}
// Devolver el último token si el índice es el último
if (currentIndex == index) {
String lastToken = data.substring(start);
// Reemplazar "_" por " "
lastToken.replace("_", " ");
return lastToken;
}
// Devolver una cadena vacía si el índice está fuera de rango
return "";
}
// void scrollText(String text, int repeatDelay) {
// int textLength = text.length();
// int lcdCols = 16; // Número de columnas del LCD
// int scrollSteps = textLength + lcdCols; // Número total de pasos de desplazamiento
// // Realizar el efecto de desplazamiento
// for (int i = textLength - 1; i >= -lcdCols; --i) {
// lcd.clear();
// int endIdx = max(0, i); //endIdx
// int startIdx = min(textLength, i + lcdCols); //startIdx
// String visibleText = text.substring(startIdx, endIdx);
// int spacesToAdd = lcdCols - visibleText.length();
// for (int j = 0; j < spacesToAdd; ++j) {
// visibleText = " " + visibleText;
// }
// lcd.setCursor(0, 0);
// lcd.print(visibleText);
// delay(100); // Ajusta el retardo según sea necesario para el desplazamiento
// }
// // Retardo antes de repetir
// delay(repeatDelay);
// }
void ButtonPressed() {
LCDrefresh = false;
// if (digitalRead(33) == LOW) {
//char c = Serial.read();
// char charDigit = '5';
//int intDigit = c - '0';
//receivedData = intDigit;
// if (receivedData >= 0)
// {
receivedData++;
// Obtener el token y aplicar desplazamiento
token = getTokenAtIndex(Ruta_Guinda, ',', receivedData-1);
// scrollText(token, 5000); // Repetir cada 5 segundos (5000 milisegundos)
lcd.clear();
lcd.print(token);
delay(150);
textLength = token.length();
contadorParadas--;
if(contadorParadas == 0)
{
contadorParadas = 13;
receivedData = 0;
}
LCDrefresh = true;
// }
// }
}
void serialEvent() {
while (Serial.available()) {
char c = Serial.read();
// char charDigit = '5';
int intDigit = c - '0';
receivedData = intDigit;
if (receivedData >= 0)
{
// Obtener el token y aplicar desplazamiento
token = getTokenAtIndex(Ruta_Guinda, ',', receivedData);
// scrollText(token, 5000); // Repetir cada 5 segundos (5000 milisegundos)
lcd.clear();
lcd.print(token);
delay(150);
textLength = token.length();
}
}
}
void desplazarLCDTexto()
{
if(LCDrefresh)
{
lcd.scrollDisplayLeft();
}
// lcd.clear();
// lcd.print(token);
// delay(150);
// textLength = token.length();
}