#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// If using software SPI (the default case):
#define OLED_MOSI 6
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
#define XRANGE 19
int x,gss,y;
const int sensorPin = A0;
int sensorValue = 0; // the sensor value
int sensorMin = 700; // minimum sensor value
int sensorMax = 200; // maximum sensor value
void setup() {
Serial.begin(9600);
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC);
// init done
// Show image buffer on the display hardware.
// Since the buffer is intialized with an Adafruit splashscreen
// internally, this will display the splashscreen.
display.display();
delay(2000);
// Clear the buffer.
display.clearDisplay();
while (millis() < 10000) {
sensorValue = analogRead(sensorPin);
// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
}
void loop() {
#define VBATPIN A7
float measuredvbat = analogRead(VBATPIN);
measuredvbat *= 2; // we divided by 2, so multiply back
measuredvbat *= 3.3; // Multiply by 3.3V, our reference voltage
measuredvbat /= 1024; // convert to voltage
int percent;
const float lipoDc[2][11] = { { 3.2, 3.680,3.740,3.780,3.810,3.850,3.870,3.960,4.02,4.100,4.200 } ,{ 0.000, 0.100,0.200,0.300,0.400,0.500,0.600,0.700,0.800,0.900,1.000 } };
int ind;
if(measuredvbat>=4.2)
percent=100;
else
{
while (!(measuredvbat<=lipoDc[0][ind+1] && measuredvbat > lipoDc[0][ind])&& ind<=10)
{
ind++;
}
if (measuredvbat <= lipoDc[0][ind + 1] && measuredvbat > lipoDc[0][ind])
{
float CapacPers = (((lipoDc[1][ind + 1] - lipoDc[1][ind])/ (lipoDc[0][ind + 1] - lipoDc[0][ind]))*(measuredvbat - lipoDc[0][ind])) + lipoDc[1][ind];
// int intCapacPers = (int)(CapacPers * 100);
percent = (int)(CapacPers*100.0);
}
}
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(48,0);
display.clearDisplay();
display.print(percent);
//display.print(measuredvbat);
display.println("%");
display.setCursor(0,30);
display.setTextSize(1);
int aValue =analogRead(0);
y = map(aValue, sensorMax, sensorMin, 0, 1024);
if (y < 552 && y > 472)
y = 512;
if (y < 0 || y < 10)
y = 0;
if (y > 1024 || y > 1014)
y = 1024;
x = map(y, 0, 1024, 0, 18);
display.print("|");
for (int i=0;i<x;i++){
if(i==XRANGE/2)display.print("|");
else display.print("-");
}
display.print("O");
for (int i=x+1;i<XRANGE;i++){
if(i==XRANGE/2)display.print("|");
else display.print("-");
}
display.print("|");
display.setCursor(53,50);
display.print(y);
display.display();
}