Well, the code have more than one problem by shure...
my proggramming skills are, let's say, basics..
But the fact is that this code is already working on the device, i reopen it to solve some issues like when the device measure more than 220mm start to "discount" instead of count up, (overload issue, or maybe i should set the variables to zero in the middle of the measuring process, i don't know)
But here is the full code (Sorry for the mess)
#include<ButtonV2.h>
#include <Arduino.h>
#include <U8g2lib.h>
#define ENCODER_DO_NOT_USE_INTERRUPTS
#include <Encoder.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#define numOfButtons 2
ButtonV2 Button1;
const byte ButtonPin = 13;
int laserPin = 15;
/*//////////////////////////////////////////////////timer con millis para pintar
long startMillis = 0;
unsigned long currentMillis;
const unsigned long period = 1000; //the value is a number of milliseconds
*/
////////////////////////////////////////timer para Velocidad
long VelStart = 0;
unsigned long VelPeriod = 2000;
unsigned int V = 1; //estes son dentro de Velocidad()
unsigned long TimeVel = 0;
unsigned long VelSalida = 0;
unsigned long VelCurrent = 0;
//////////////////////////////////////////////timer para la rueda
long startMillisWhell = 0;
unsigned long MilliscurrentWhell;
const unsigned long periodWhell = 200;
//constructor line
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16, /* clock=*/ 5, /* data=*/ 4);
String TextToDraw = "Txanquete";
///timer para Txanquete
long MillisTxanNow ;
long MillisTxanStart = 0;
//////////////////////////////////////////////// iniciando el encoder
Encoder myEnc(14, 12);
long counter = 0;
long newPosition;
long oldPosition = -999;
int WhellButton = 13;
//encoder para direccion giro
int direccion = 0;
//posicion del menu
int Menupos = 0;
/////////////////Byte para Navegar
char navega = 'S';
int i, j, k = 0;
////////////////medidas
const int pase = 8246679;
long Medida = 0;
///Variables para iniciar a la segunda
unsigned int Laser = 1;
unsigned int Vol = 1;
unsigned int Vel = 1;
///////////////////////////////////////////Array menu
String MenuArray[4][4][2] = {
{
{ "Medir", "Velocidad" }, //medir 0-0-0 menu0-0-1
{ "0", "Medir"}, //Medir 0-1-1
{ "0", "Resetear" }, //Restar 0-2-1
{ "0", "Ver Medida" } //Ver Medida 0-3-1
},
{
{ "Laser", "on"}, //Laser 1-0-0 on 1-0-1
{ "0", "off"}, //off 1-1-1
{ "0", "Blink"}, //Blink 1-2-1
{ "TempPrint", "0" } //TempPrint 1-3-0
},
{
{ "Medidas", "Num 1" }, //Medidas 2-0-0; Num 1 2-0-1
{ "0", "Num 2",}, // Num 2 2-1-1
{ "0", "Num 3" }, // Num 3 2-2-1
{ "0", "Num 4" } // Num 4 2-3-1
},
{
{ "Calculos" , "Vol Cubo"}, //Calculos 3-0-0 Volumen 3-0-1
{ "0", "Volumen Tubo"}, //Volumen Tubo 3-1-1
{ "0", "Radio perimetro"}, //Triangulo Lado 3-2-1
{ "0", "Txanquete"} //Radio Perimetro 3-3-1
},
};
void setup()
{
//////////////////////////////////////////////pinMode(LEDpin, OUTPUT);
Serial.begin(115200);
//pinMode(LEDpin, OUTPUT);
pinMode(ButtonPin, INPUT_PULLUP);
Button1.SetStateAndTime(LOW);
///////////////////////////////////////////////millis rueda laser
pinMode (laserPin, OUTPUT);
//setup display
u8g2.begin();
}
void loop()
{
//ESCUCHA BOTON RUEDA
Boton();
//ESCUCHA DIRECCION RUEDA asi que pase el tiempo period
MilliscurrentWhell = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (MilliscurrentWhell - startMillisWhell >= periodWhell) //test whether the period has elapsed
{
direccion_whell();
startMillisWhell = MilliscurrentWhell; //IMPORTANT to save the start time of the current LED state.
}
////pinta optimizado, entra solo cuando cambia, para pintar cambiar coordenadas ijk,o su valor
if (MenuArray[i][j][k] != TextToDraw) {
Serial.println ("Pinta Dentro");
Serial.println (TextToDraw);
Serial.println (MenuArray[i][j][k]);
TextToDraw = MenuArray[i][j][k];
pinta();
}
}
////////////////////////FUNCION BOTONES Envia directo a funcion navega segun el tipo de pulsacion
void Boton () {
byte type = Button1.CheckButton(ButtonPin); // current time and length of time to press the button as many times as you can ie. 1.5 seconds
switch (type)
{
case WAITING:
break;
case PRESSED:
navega = 'D';
Navega();
Serial.println("pressed 1 time");
break;
case DOUBLE_PRESSED:
navega = 'F';
Navega();
Serial.println("pressed 2 times");
break;
case MULTI_PRESSED:
Serial.println("pressed 3 times");
i = j = k = 0;
break;
case HELD:
//Serial.println("Button uno HELD");
break;
}
}
//////////////////////DIRECCION DE LA RUEDA PARA MENU
int direccion_whell() {
long newPosition = myEnc.read();
if (newPosition == oldPosition) { //si no cambia la rueda direccion a 0
direccion = 0;
}
if (newPosition != oldPosition) {
if ((newPosition + 2) > oldPosition) {
navega = 'S';
Navega();
Serial.println("Rueda arriba");
}
if ((newPosition - 2) < oldPosition) {
navega = 'I';
Navega();
Serial.println("Rueda abajo");
}
oldPosition = newPosition;
}
}
////////////////////////////FUNCIONES DE NAVEGACION
void Navega() {
Serial.print("Dentro de navega; Navega Value: ");
Serial.println(navega);
Serial.print ("Valor i,j,k: " );
Serial.print ( i);
Serial.print (" , ");
Serial.print (j);
Serial.print (" , ");
Serial.println (k);
switch (navega) {
case 'S'://Superior
if (j == 0 && k == 0) {
i = i + 1;
if (i > 3) {
i = 0;
}
}
if (k == 1) {
j = j + 1;
if (j > 3) {
j = 0;
}
}
break;
case 'I'://Inferior
if (j == 0 && k == 0) {
i = i - 1;
if (i < 0) {
i = 3;
}
}
if (k == 1) {
j = j - 1;
if (j < 0) {
j = 3;
}
}
break;
case 'D'://Dentro
if (j == 0 && k == 0) {
k = 1;
}
if (k == 1) {
Serial.println("Desde pulsaciones hacia Ejecuta");
Ejecuta();
//Ejecuta posicion i,j,k actual
}
break;
case 'F'://Fuera
if (j == 0 && k == 0) {
TextToDraw = "No Hay Superior";
pinta();
}
if (k == 1) {
k = 0;
j = 0;
}
break;
}
}