#include <U8g2lib.h>
#include <Arduino.h>
#include <SPI.h>
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, SCL, SDA, U8X8_PIN_NONE);
// JoyStick PINS
#define Vertical A0
#define Horizontal A1
#define Seleccion 2
int posicion = 1;
bool retorno = LOW;
void setup() {
u8g2.begin();
Serial.begin(9600);
pinMode(Vertical, INPUT);
pinMode(Horizontal, INPUT);
pinMode(Seleccion, INPUT_PULLUP);
/*
u8g2.clearBuffer();
u8g2.setFontMode(1);
u8g2.setBitmapMode(1);
for (int i = 1; i < 87; i = i + 2) {
u8g2.drawFrame(17, 26, 93, 24);
u8g2.drawBox(20, 29, i, 18);
u8g2.setDrawColor(2);
u8g2.setFont(u8g2_font_6x10_tr);
u8g2.drawStr(40, 41, "cargando");
u8g2.sendBuffer();
u8g2.clearBuffer();
}
u8g2.clear();
*/
}
void loop() {
int cursorV = map(posicion, 0, 2, 16, 46);
//Serial.println(posicion);
u8g2.clearBuffer();
u8g2.setFontMode(1);
u8g2.setBitmapMode(1);
u8g2.setFont(u8g2_font_helvB08_tr);
u8g2.drawStr(12, 27, "Opcion 1");
u8g2.drawStr(12, 42, "Opcion 2");
u8g2.drawStr(12, 57, "Opcion 3");
u8g2.setDrawColor(2);
u8g2.drawRBox(7, cursorV, 87, 16, 4); // seleccion de Opciones MENU
//u8g2.drawRBox(7, 16, 87, 16, 4); // Opcion 1
//u8g2.drawRBox(7, 31, 87, 16, 4); // Opcion 2
//u8g2.drawRBox(7, 46, 87, 16, 4); // Opcion 3
u8g2.setDrawColor(1);
u8g2.setFont(u8g2_font_5x8_tr);
u8g2.drawStr(11, 11, "Selecciona una opcion");
u8g2.sendBuffer();
// ========= controles JoyStic ===================
int Vert = map(analogRead(Vertical), 0, 1023, 0, 2);
int Horz = map(analogRead(Horizontal), 0, 1023, 0, 2);
bool Sel = digitalRead(Seleccion) == LOW;
if (Vert > 1 && retorno == LOW) {
posicion --;
retorno = HIGH;
}
else if (Vert < 1 && retorno == LOW ) {
posicion ++;
retorno = HIGH;
}
else if (Vert == 1) {
retorno = LOW;
}
if (posicion > 2) {
posicion = 0;
} else if (posicion < 0) {
posicion = 2;
}
while (posicion == 0 && Sel == HIGH && retorno == LOW) { // abrir menu Opcion 1
opcion1();
}
while (posicion == 1 && Sel == HIGH && retorno == LOW) { // abrir menu Opcion 2
opcion2();
}
while (posicion == 2 && Sel == HIGH && retorno == LOW) { // abrir menu Opcion 3
opcion3();
}
}
void opcion1() {
int Horz = map(analogRead(Horizontal), 0, 1023, 0, 2);
u8g2.clearBuffer();
u8g2.setFontMode(1);
u8g2.setBitmapMode(1);
u8g2.setDrawColor(1);
u8g2.setFont(u8g2_font_5x8_tr);
u8g2.drawStr(11, 11, "Opcion 1");
u8g2.sendBuffer();
if (Horz == 2) {// volver a tras
retorno = HIGH;
}
}
void opcion2() {
int Horz = map(analogRead(Horizontal), 0, 1023, 0, 2);
u8g2.clearBuffer();
u8g2.setFontMode(1);
u8g2.setBitmapMode(1);
u8g2.setDrawColor(1);
u8g2.setFont(u8g2_font_5x8_tr);
u8g2.drawStr(11, 11, "Opcion 2");
u8g2.sendBuffer();
if (Horz == 2) { // volver atras
retorno = HIGH;
}
}
void opcion3() {
int Horz = map(analogRead(Horizontal), 0, 1023, 0, 2);
u8g2.clearBuffer();
u8g2.setFontMode(1);
u8g2.setBitmapMode(1);
u8g2.setDrawColor(1);
u8g2.setFont(u8g2_font_5x8_tr);
u8g2.drawStr(11, 11, "Opcion 3");
u8g2.sendBuffer();
if (Horz == 2) { // volver atras
retorno = HIGH;
}
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA
joystick1:VCC
joystick1:VERT
joystick1:HORZ
joystick1:SEL
joystick1:GND