#include "U8glib.h"
//https://github.com/olikraus/u8glib/wiki/userreference
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST);
const unsigned char IMAGEM_TURBO [] PROGMEM =
{
0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xfc, 0x20, 0x00, 0x00, 0x00, 0x00,
0x01, 0x80, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x20, 0x7e, 0xdb, 0xef, 0xbe, 0x10, 0x00, 0x00, 0x20, 0x18, 0xdb, 0x6d, 0xb6,
0x10, 0x00, 0x00, 0x20, 0x18, 0xdb, 0x6f, 0x36, 0x20, 0x3f, 0xfc, 0x20, 0x18, 0xdb, 0xcd, 0xb6,
0x20, 0x41, 0x03, 0xc0, 0x18, 0xdb, 0x6d, 0xb6, 0x40, 0x80, 0x81, 0x00, 0x18, 0xfb, 0x6f, 0xbe,
0x41, 0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00,
0x41, 0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00,
0x41, 0x00, 0x41, 0x00, 0x7d, 0xf7, 0xdf, 0x7e, 0x40, 0x80, 0x81, 0x00, 0x6d, 0xb6, 0xd8, 0x18,
0x20, 0x41, 0x02, 0x00, 0x79, 0xb6, 0xdf, 0x18, 0x20, 0x3e, 0x02, 0x00, 0x6d, 0xb6, 0xc3, 0x18,
0x10, 0x00, 0x04, 0x00, 0x6d, 0xb6, 0xdb, 0x18, 0x10, 0x00, 0x04, 0x00, 0x7d, 0xf7, 0xdf, 0x18,
0x08, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x80, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const unsigned char IMG_TURBO_ANIMACAO_1 [] PROGMEM =
{
0x01, 0x00, 0x09, 0x20, 0x05, 0x40, 0x03, 0x80, 0x1f, 0xf0, 0x03, 0x80, 0x05, 0x40, 0x09, 0x20,
0x01, 0x00
};
const unsigned char IMG_TURBO_ANIMACAO_2 [] PROGMEM =
{
0x00, 0x80, 0x04, 0x80, 0x02, 0xa0, 0x1f, 0xc0, 0x03, 0x80, 0x07, 0xf0, 0x0a, 0x80, 0x02, 0x40,
0x02, 0x00
};
const unsigned char IMG_TURBO_ANIMACAO_3 [] PROGMEM =
{
0x02, 0x00, 0x02, 0x40, 0x0a, 0x80, 0x07, 0xf0, 0x03, 0x80, 0x1f, 0xc0, 0x02, 0xa0, 0x04, 0x80,
0x00, 0x80
};
const int PIN_PRESSAO = A0;
const int PIN_VOLTAGEM = A2;
const int PIN_MODO = A3;
const float tabelaVoltagens[] = {1.1, 1.23, 1.47, 1.7, 1.93, 2.17, 2.4, 2.63, 2.87, 3.1, 3.33};
const float tabelaPressoes[] = { 0, 0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.7, 1.9, 2.1};
const int SENSOR_VALOR_MINIMO = 0;
const int SENSOR_VALOR_MAXIMO = 2.1;
const int INICIO_RED_LINE = 98;
const float VOLTAGEM_FATOR = 1220.0;
const int VOLTAGEM_VALOR_MINIMO = 0;
const int VOLTAGEM_VALOR_MAXIMO = 15;
const int PONTOS_POR_MEDIA = 10;
const int FIM_BARRA_PROGRESSO = 121;
const unsigned char * TURBO_ANIMACOES[3] =
{
IMG_TURBO_ANIMACAO_1,
IMG_TURBO_ANIMACAO_2,
IMG_TURBO_ANIMACAO_3
};
//Variável para armazenar o tempo de início
unsigned long startTime;
bool exibirIntroducao = true;
bool exibirVoltagem = false;
float voltagem = 0.0;
float pressao = 0.0;
int turboAnimacaoIndice = 0;
int barraEstado = INICIO_RED_LINE;
void setup()
{
//Descomentar para debugar
//Serial.begin(9600);
//Inverter tela
u8g.setRot180();
//set the color to white
u8g.setColorIndex(1);
pinMode(PIN_PRESSAO, INPUT);
pinMode(PIN_VOLTAGEM, INPUT);
pinMode(PIN_MODO, INPUT);
//Registra o tempo de início
startTime = millis();
}
void loop()
{
if(exibirIntroducao)
{
ExibirIntroducao();
}
else
{
//Botão Pressionado
if (analogRead(PIN_MODO) < 100) {
exibirVoltagem = !exibirVoltagem;
delay(500);
}
if(exibirVoltagem)
{
ExibirVoltagem();
}
else
{
ExibirPressao();
}
}
}
void ExibirIntroducao()
{
unsigned long elapsedTime = 0;
u8g.firstPage();
while (elapsedTime < 60000) {
ExibirVoltagem();
elapsedTime = millis() - startTime;
}
exibirIntroducao = false;
}
void ObterVoltagem()
{
voltagem = 0.0;
int i;
//Obter uma leitura com média móvel para aumentar a precisão
for (i = 0; i < PONTOS_POR_MEDIA; i++)
{
voltagem += analogRead(PIN_VOLTAGEM);
}
voltagem = (voltagem / PONTOS_POR_MEDIA) * (5.0 / VOLTAGEM_FATOR);
voltagem = voltagem * 5.0;
//Serial.println(voltagem);
}
void ObterPressao()
{
float voltagemPressao = 0.0;
int i;
//Obter uma leitura com média móvel para aumentar a precisão
for (i = 0; i < PONTOS_POR_MEDIA; i++)
{
voltagemPressao += analogRead(PIN_PRESSAO);
}
voltagemPressao = (voltagemPressao / PONTOS_POR_MEDIA) * (5.0 / VOLTAGEM_FATOR);
//Serial.println(voltagemPressao);
pressao = CalcularPressao(voltagemPressao);
}
void ExibirVoltagem()
{
ObterVoltagem();
u8g.firstPage();
do
{
//Definindo a fonte para a medição
u8g.setFont(u8g_font_fub35n);
int posicao = 35;
if (voltagem >= 9.9) posicao = 7;
u8g.drawStr(posicao, 38, String(voltagem, 1).c_str());
u8g.setFont(u8g_font_profont22);
u8g.drawStr(115, 37, "V");
DesenharBateria();
DesenharBarraProgressoVoltagem();
} while ( u8g.nextPage() );
delay(500);
}
void ExibirPressao()
{
ObterPressao();
u8g.firstPage();
do
{
AnimarTurbo();
u8g.drawBitmapP(101, 0, 64/8, 24, IMAGEM_TURBO);
//draw the animated inside part of the icon
u8g.drawBitmapP(106, 8, 16/8, 9, TURBO_ANIMACOES[turboAnimacaoIndice]);
//Definindo a fonte para a medição
u8g.setFont(u8g_font_fub49n);
u8g.drawStr(0, 49, String(pressao, 1).c_str());
u8g.setFont(u8g_font_profont15);
u8g.drawStr(105, 50, "BAR");
DesenharBarraProgressoPressao();
} while ( u8g.nextPage() );
delay(100);
}
void DesenharBarraProgressoPressao()
{
DesenharBarraProgressoRetangulo();
int valor = pressao * 10;
if (valor <= 15)
{
u8g.drawBox(4, 53, map(valor, SENSOR_VALOR_MINIMO, 15, 0, FIM_BARRA_PROGRESSO), 9);
}
//Exibe animação na barra quando atinge pressão máxima
else
{
u8g.drawBox(4, 53, barraEstado, 9);
barraEstado += 1;
if(barraEstado >= FIM_BARRA_PROGRESSO)
{
barraEstado = INICIO_RED_LINE;
}
}
DesenharLinhasBarraProgresso();
}
void DesenharBarraProgressoVoltagem()
{
DesenharBarraProgressoRetangulo();
int valor = voltagem * 10;
if (valor < 80)
{
valor = 80;
}
u8g.drawBox(4, 53, map(valor, 80, VOLTAGEM_VALOR_MAXIMO * 10, 0, FIM_BARRA_PROGRESSO), 9);
u8g.drawVLine(0, 46, 6);
u8g.drawVLine(70, 46, 6);
u8g.drawVLine(127, 46,65);
}
void DesenharBateria()
{
u8g.drawVLine(114, 0, 2);
u8g.drawVLine(115, 0, 2);
u8g.drawVLine(116, 0, 2);
u8g.drawVLine(123, 0, 2);
u8g.drawVLine(124, 0, 2);
u8g.drawVLine(125, 0, 2);
u8g.drawFrame(111, 2, 17, 14);
u8g.setFont(u8g_font_profont15);
u8g.drawStr(113, 10, "-");
u8g.drawStr(120, 10, "+");
}
void DesenharBarraProgressoRetangulo()
{
u8g.drawFrame(0, 51, 128, 13);
}
void DesenharLinhasBarraProgresso()
{
int linhaPosicao = INICIO_RED_LINE;
for (int i = 0; i <= 5; i++) {
u8g.drawVLine(linhaPosicao, 55, 6);
linhaPosicao += 5;
}
}
void AnimarTurbo()
{
//increase the turboAnimacaoIndice for the inner part of the icon
if (pressao >= 0.1) {turboAnimacaoIndice = turboAnimacaoIndice + 1;}
//there are total of 3 frames for the animation
if (turboAnimacaoIndice > 2) {turboAnimacaoIndice = 0;}
}
float CalcularPressao(float voltagem) {
int tableSize = sizeof(tabelaVoltagens) / sizeof(tabelaPressoes[0]);
// Verifica se o valor de A está fora do intervalo da tabela
if (voltagem <= tabelaVoltagens[0]) {
return SENSOR_VALOR_MINIMO;
}
if (voltagem >= tabelaVoltagens[tableSize - 1]) {
return SENSOR_VALOR_MAXIMO;
}
// Encontra os dois valores de A mais próximos ao valor de destino
int i = 0;
while (voltagem > tabelaVoltagens[i + 1]) {
i++;
}
// Realiza a interpolação linear
float alpha = (voltagem - tabelaVoltagens[i]) / (tabelaVoltagens[i + 1] - tabelaVoltagens[i]);
float interpolatedB = tabelaPressoes[i] + alpha * (tabelaPressoes[i + 1] - tabelaPressoes[i]);
return interpolatedB;
}