#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define clk 2
#define dt 3
#define btn 4
#define led 10
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
Adafruit_SSD1306 display (SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Definicion de Variables y Pines
String opciones[] = {"LECTURA", "CONFIGURACION", "INFORMACION"};
int max_opciones = sizeof(opciones)/sizeof(opciones[0]);
int state_clk_old;
int state_btn_old;
int count = 0;
const int buttonPin1 = 12; // Pin del primer botón
const int buttonPin2 = 14; // Pin del segundo botón
const int buttonSalir = 26; // Pin del botón de salida.
const int ledPin3 = 5;// Pin para el tercer LED
bool Regresar = false;
// 'Logo NOVIGADO', 1,28x64px
const unsigned char epd_bitmap_Logo_NOVIGADO [] 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, 0xfc, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xe0, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0x80, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xfe, 0x00, 0x00, 0x00, 0x03, 0xfd, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xfc, 0x00, 0x00, 0x00, 0x03, 0xf9, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf8, 0x00, 0xff, 0xff, 0x03, 0xf3, 0xe7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf8, 0x0f, 0xff, 0xff, 0x83, 0xf3, 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf0, 0x1f, 0xff, 0xff, 0x83, 0xf3, 0xf9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf0, 0x3e, 0x7f, 0xff, 0x83, 0xf3, 0xe1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xe0, 0x78, 0x3f, 0xff, 0x83, 0xe3, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xe0, 0xf8, 0x3f, 0xff, 0x83, 0xc0, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xe0, 0xf8, 0x1f, 0xff, 0x83, 0xc0, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x0f, 0xff, 0x83, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x07, 0xff, 0x83, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x07, 0xff, 0x83, 0xfe, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x03, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x01, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x00, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x00, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x00, 0x7f, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x20, 0x7f, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x30, 0x3f, 0x83, 0xe0, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x38, 0x1f, 0x83, 0xe0, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x38, 0x0f, 0x83, 0xc0, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x3c, 0x0f, 0x83, 0xc0, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x3e, 0x07, 0x83, 0xe0, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x3e, 0x03, 0x83, 0xff, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x3f, 0x03, 0x83, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x3f, 0x81, 0x83, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x3f, 0x80, 0x83, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x3f, 0xc0, 0x03, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x3f, 0xe0, 0x03, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x3f, 0xf0, 0x03, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x3f, 0xf0, 0x03, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x3f, 0xf8, 0x03, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x3f, 0xfc, 0x03, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x3f, 0xfc, 0x03, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x3f, 0xfe, 0x03, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0xf8, 0x3f, 0xff, 0x03, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xe0, 0xf8, 0x3f, 0xff, 0x83, 0xff, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xe0, 0x78, 0x3f, 0xff, 0x83, 0xff, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xe0, 0x78, 0x3f, 0xff, 0xc7, 0xfe, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf0, 0x3e, 0xff, 0xff, 0xff, 0xfc, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf0, 0x1f, 0xff, 0xff, 0xff, 0xf8, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xf8, 0x07, 0xff, 0xff, 0xff, 0xe0, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xc0, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xf0, 0x00, 0x00, 0x00, 0x0f, 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,
0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc0, 0x00, 0x07, 0xff, 0xe0, 0x00, 0x03, 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,
0xdb, 0x16, 0x31, 0xd8, 0xc3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xca, 0x4a, 0xa7, 0xd9, 0x59, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xc2, 0xe8, 0xa8, 0x9b, 0x1d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xd2, 0xed, 0xac, 0x0b, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xdb, 0x1d, 0xa0, 0x08, 0x43, 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 setup() {
Serial.begin(9600);
Iniciar_Pantalla ();
Logo_Novigado ();
pinMode(clk, INPUT);
pinMode(dt, INPUT);
pinMode(btn, INPUT_PULLUP);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonSalir, INPUT);
pinMode(ledPin3, OUTPUT);
state_clk_old = digitalRead(clk);
state_btn_old = digitalRead(btn);
mostrar_menu();
}
void loop() {
int state_btn = digitalRead(btn);
encoder();
if(state_btn_old == HIGH && state_btn == LOW){
run_option();
}
state_btn_old = state_btn;
}
void Iniciar_Pantalla (){
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)){
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
display.clearDisplay();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // here the 0x3c is the I2C address, check your i2c address if u have multiple devices.
display.clearDisplay();
}
void Logo_Novigado (){
display.drawBitmap(0, 0, epd_bitmap_Logo_NOVIGADO, 128, 64, WHITE);
display.display();
display.setTextSize(1);
display.setTextColor(SSD1306_BLACK);
display.setCursor(65,30);
display.println("Cargando");
display.display();
display.setCursor(65,40);
display.println("Sistema...");
display.display();
delay(3000);
display.clearDisplay();
}
void run_option(){
if(count == 0){
display.clearDisplay();
display.setCursor(20,25);
display.println("Leyendo Carril");
Serial.println("Se ingreso a la opción 1");
Activar_Alarma(buttonPin1, buttonPin2, ledPin3, buttonSalir);
display.display();
display.clearDisplay();
}
if(count == 1){
display.clearDisplay();
display.setCursor(20,25);
display.println("Modificar tiempos");
display.setCursor(20,35);
display.println("entre cortinas");
//Serial.println("Se ingreso a la opción 2");
display.display();
if(digitalRead(buttonSalir) == 1 )
mostrar_menu();
}
if(count == 2){
display.clearDisplay();
display.setCursor(20,25);
display.println("Version:" );
display.setCursor(20,35);
display.println("Hardware: 1.0");
display.setCursor(20,45);
display.println("software: 1.0");
display.display();
//Serial.println("Se ingreso a la opción 3");
if(digitalRead(buttonSalir) == 1 )
mostrar_menu();
}
}
void encoder(){
int state_clk = digitalRead(clk);
int state_dt = digitalRead(dt);
if(state_clk_old == HIGH && state_clk == LOW){
if(state_dt == LOW){
count--;
}else{
count++;
}
if(count < 0) count = max_opciones - 1;
if(count > max_opciones-1) count = 0;
mostrar_menu();
Serial.println("Navegando Menu");
}
delay(5);
state_clk_old = state_clk;
}
void mostrar_menu(){
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.drawRect(47,1,29,11,WHITE);
display.setCursor(50,3);
display.println("MENU ");
display.display();
display.drawRect(8,15,105,20,WHITE);
display.setCursor(10,20);
display.println(opciones[count]);
display.display();
}
void Activar_Alarma(int sensor1, int sensor2, int Mensaje, int botonSalir) {
int tiempoS1 = 0;
int tiempoS2 = 0;
Serial.println("Se ingreso a activar Alarma");
while (digitalRead(buttonSalir) == 0 ){
while (digitalRead(sensor1) == HIGH ){
delay (1);
tiempoS1++;
// Serial.println("Se leyó sensor1");
if (tiempoS1 > 10000){
while( digitalRead(sensor2)== HIGH){
delay(1);
// Serial.println("Se leyó sensor2");
tiempoS2++;
if(tiempoS2 > 5000){
digitalWrite(Mensaje, HIGH);
display.setCursor(20,25);
display.println("Detección de cola");
display.display();
}
else {
digitalWrite(Mensaje, LOW);
}
}
tiempoS2 = 0;
digitalWrite(Mensaje, LOW);
}
else{
digitalWrite(Mensaje, LOW);
//tiempoS1 = 0;
}
}
tiempoS1 = 0;
digitalWrite(Mensaje, LOW);
delay(1);
}
mostrar_menu();
Serial.println("Llamamos al menu");
}