#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define Ancho 128 // OLED display width, in pixels
#define Alto 64 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 Oled(Ancho, Alto, &Wire, OLED_RESET);
// 'bateria', 50x30px
const unsigned char Bit_bateria [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x80, 0x00, 0xff, 0x80, 0x00, 0x00, 0x40,
0x80, 0x00, 0xc1, 0x80, 0x00, 0x01, 0xff, 0xc0, 0x01, 0xff, 0xc0, 0x00, 0x01, 0x80, 0x60, 0x03,
0x00, 0xc0, 0x00, 0x7f, 0xf0, 0x3f, 0xff, 0xf0, 0x7f, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x30,
0x80, 0x00, 0x03, 0x00, 0x06, 0x00, 0x31, 0xe0, 0x00, 0x0e, 0x01, 0xc6, 0x00, 0x30, 0x00, 0x00,
0x3c, 0x00, 0x06, 0x00, 0x30, 0x00, 0x01, 0xf8, 0x00, 0x06, 0x00, 0x30, 0x00, 0x06, 0x30, 0x00,
0x06, 0x00, 0x30, 0x00, 0x18, 0x3f, 0x00, 0x06, 0x00, 0x30, 0x00, 0x7e, 0x1c, 0x00, 0x06, 0x00,
0x30, 0x00, 0x0e, 0xe0, 0x00, 0x06, 0x00, 0x30, 0x00, 0x1f, 0x80, 0x00, 0x06, 0x00, 0x30, 0x00,
0x3c, 0x00, 0x00, 0x06, 0x00, 0x30, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, 0x30, 0x00, 0x00, 0x00,
0x00, 0x06, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00
};
// 'panelBMP', 29x32px
const unsigned char Bit_panelBMP [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xc0, 0x10, 0x30, 0x60, 0x40,
0x10, 0x30, 0x60, 0x40, 0x10, 0x30, 0x60, 0x40, 0x1f, 0xff, 0xff, 0xc0, 0x10, 0x20, 0x60, 0x60,
0x10, 0x20, 0x60, 0x20, 0x10, 0x20, 0x60, 0x20, 0x10, 0x20, 0x60, 0x20, 0x1f, 0xff, 0xff, 0xe0,
0x3f, 0xff, 0xff, 0xf0, 0x20, 0x20, 0x30, 0x10, 0x20, 0x20, 0x30, 0x10, 0x20, 0x20, 0x30, 0x10,
0x7f, 0xff, 0xff, 0xf8, 0x7f, 0xff, 0xff, 0xf8, 0x7f, 0xff, 0xff, 0xf8, 0x00, 0x0f, 0x80, 0x00,
0x00, 0x0f, 0x80, 0x00, 0x00, 0x0f, 0x80, 0x00, 0x00, 0x0f, 0x80, 0x00, 0x00, 0x0f, 0xc0, 0x00,
0x00, 0x3f, 0xf0, 0x00, 0x00, 0x7f, 0xf8, 0x00, 0x01, 0xc0, 0x18, 0x00, 0x01, 0x80, 0x0c, 0x00,
0x01, 0x80, 0x0c, 0x00, 0x01, 0xff, 0xfc, 0x00, 0x01, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'focoBMP', 26x36px
const unsigned char Bit_focoBMP [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0xff, 0xc0, 0x00, 0x07, 0x00, 0x18, 0x00,
0x0c, 0x00, 0x0c, 0x00, 0x08, 0x00, 0x06, 0x00, 0x10, 0x00, 0x03, 0x00, 0x30, 0x00, 0x01, 0x80,
0x60, 0x00, 0x01, 0x80, 0x40, 0x20, 0x00, 0x80, 0x40, 0x00, 0x00, 0x80, 0x40, 0x0a, 0x20, 0x80,
0x41, 0xff, 0x90, 0x80, 0x41, 0x22, 0x80, 0x80, 0x61, 0x80, 0x20, 0x80, 0x20, 0x00, 0x41, 0x80,
0x30, 0x40, 0x81, 0x80, 0x10, 0x09, 0x02, 0x00, 0x08, 0x21, 0x06, 0x00, 0x08, 0x0a, 0x0c, 0x00,
0x04, 0x04, 0x0c, 0x00, 0x06, 0x09, 0x18, 0x00, 0x06, 0x1b, 0x10, 0x00, 0x02, 0x09, 0x10, 0x00,
0x00, 0x09, 0x00, 0x00, 0x00, 0x09, 0x10, 0x00, 0x01, 0xff, 0xe0, 0x00, 0x01, 0xff, 0xe0, 0x00,
0x01, 0xc0, 0xe0, 0x00, 0x01, 0x8e, 0x60, 0x00, 0x01, 0x81, 0xe0, 0x00, 0x01, 0xb8, 0x60, 0x00,
0x01, 0x8e, 0x60, 0x00, 0x00, 0xff, 0xc0, 0x00, 0x00, 0x23, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00
};
// Botones
const int but_izq = 11;
const int but_ok = 10;
const int but_der = 9;
const int Led = 13;
const int Venti = 3;
// Variables
float Vpanel = 0.0;
float Vbat = 0.0;
float Vcor = 0.0;
float Temp = 0.0;
float Pot = 0.0;
int Util = 200;
float Porutil = 0.0;
int cnt_oled = 0 ;
int bitmenu = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!Oled.begin(SSD1306_SWITCHCAPVCC, ADDRESS)) {
Serial.println("SSD1306 failed");
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
Oled.setTextColor(WHITE);
//Oled.display();
//delay(1000); // Pause for 1 seconds
Oled.clearDisplay();
Oled.setCursor(0,16);
Oled.setTextSize(2);
Oled.print(" INICIANDO ");
Oled.setCursor(0,32);
Oled.print(" SISTEMA ");
Oled.display();
delay(500); // Pause for 1 seconds
Oled.clearDisplay();
pinMode(Led, OUTPUT);
digitalWrite(Led, LOW);
pinMode(Venti, OUTPUT);
digitalWrite(Venti, HIGH);
pinMode(but_ok, INPUT_PULLUP); //declara un pin como entrada y activa la resistencia pullup interna para ese pin
pinMode(but_izq, INPUT_PULLUP); //declara un pin como entrada y activa la resistencia pullup interna para ese pin
pinMode(but_der, INPUT_PULLUP); //declara un pin como entrada y activa la resistencia pullup interna para ese pin
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(Led, !digitalRead(Led));
Botones();
Vpanel = 22.325*LeerADC(A0)*1.1/1023;
Vbat = 13.393*LeerADC(A2)*1.1/1023;
Vcor = LeerADC(A1)*4.3327;
Vcor = map(Vcor,0,5000,0,10000);
Vcor = (Vcor-5000)/1000;
Temp = LeerADC(A3)/4.65;
//Temp = 135.5;
Pot = Vbat*Vcor;
Porutil = 0.01*map(Util,0,255,0,10000);
cnt_oled++;
if(cnt_oled>=5){
cnt_oled=0;
Menu();
}
delay(200);
}
void Botones(){
if(digitalRead(but_izq)==false){
delay(100);
if(digitalRead(but_izq)==false){
bitmenu--;
if(bitmenu<0){
bitmenu = 6;
}
Menu ();
delay(100);
}
}
else if(digitalRead(but_ok)==false){
delay(100);
if(digitalRead(but_ok)==false){
bitmenu = 0;
Menu ();
delay(100);
}
}
else if(digitalRead(but_der)==false){
delay(100);
if(digitalRead(but_der)==false){
bitmenu++;
if(bitmenu>6){
bitmenu = 0;
}
Menu ();
delay(100);
}
}
}
void Menu (){
Oled.clearDisplay();
if(Temp>80){
digitalWrite(Venti, LOW);
Oled.setTextSize(1);
Oled.setCursor(55,56);
Oled.write(126);
Oled.write(126);
Oled.write(126);
}
else if(Temp<60){
digitalWrite(Venti, HIGH);
}
switch(bitmenu){
case 0:
if(Vpanel>14){
Oled.drawBitmap(0,20,Bit_panelBMP,29,32,WHITE);
}
if(Vbat>8){
Oled.drawBitmap(40,16,Bit_bateria,50,30,WHITE);
}
if(Vbat>11.5){
Oled.drawBitmap(100,16,Bit_focoBMP,26,36,WHITE);
}
Oled.setCursor(0,5);
Oled.setTextSize(1);
Oled.print(Vpanel,2);
Oled.print("V");
Oled.setCursor(45,44);
Oled.print(Vbat,2);
Oled.print(" V");
Oled.setCursor(0,56);
Oled.print(Vcor,2);
Oled.print("A");
Oled.setCursor(88,5);
Oled.print(Temp,1);
Oled.write(167);
Oled.setCursor(90,56);
Oled.print(Pot,2);
Oled.print("W");
Oled.setCursor(48,0);
Oled.print(Porutil,1);
Oled.print("%");
Oled.setCursor(20,56);
Oled.print("Bulk");
Oled.display();
break;
case 1:
Oled.setCursor(32,0);
Oled.setTextSize(2);
Oled.print("Panel");
Oled.setCursor(24,32);
Oled.print(Vpanel,2);
Oled.print(" V");
Oled.display();
break;
case 2:
Oled.setCursor(24,0);
Oled.setTextSize(2);
Oled.print("Bateria");
Oled.setCursor(24,32);
Oled.print(Vbat,2);
Oled.print(" V");
Oled.display();
break;
case 3:
Oled.setCursor(10,0);
Oled.setTextSize(2);
Oled.print("Corriente");
Oled.setCursor(28,32);
Oled.print(Vcor,2);
Oled.print(" A");
Oled.display();
break;
case 4:
Oled.setCursor(0,0);
Oled.setTextSize(2);
Oled.print(" Potencia");
Oled.setCursor(20,32);
Oled.print(Pot,2);
Oled.print(" W");
Oled.display();
break;
case 5:
Oled.setCursor(35,0);
Oled.setTextSize(2);
Oled.print("Temp");
Oled.setCursor(16,32);
Oled.print(Temp,2);
Oled.write(167);
Oled.print("C");
Oled.display();
break;
case 6:
Oled.setCursor(35,0);
Oled.setTextSize(2);
Oled.print("Util");
Oled.setCursor(16,32);
Oled.print(Porutil,2);
Oled.print(" %");
Oled.display();
break;
}
}
float LeerADC(int pin) {
float Vol = 0;
int sensor = 0;
for (int i = 0;i <=49;i++) {
sensor = analogRead(pin);
Vol = Vol + sensor;
delayMicroseconds(15);
}
//Serial.println("Fin Lectura");
return Vol/50;
}