#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>


#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
RTC_DS1307 rtc;

int barWidth = 18;
int numBars = 8;
int barHeight = SCREEN_HEIGHT / numBars;
int barSpacing = 1;
int screen = 1;
int scrnSw = 0;
int Power = 0;
int avgPower = 0;
int prevM = 0;
int currM = 0;
int var;
int start = 0;
int x = 0;
int y = 0;
int pwr = 0;
int filledBars = 0;
int l = 0;
int RedLED = 9;
int GreenLED = 10;
int BlueLED = 11;
int Red = 0;
int Green = 0;
int Blue = 0;
int security = 0;


const int analogPin = A0;
const int currentSensorPin = A1;
const float R1 = 33000.0;
const float R2 = 10000.0;
const float ACS_OFFSET = 2.5;
const float ACS_SENSITIVITY = 0.066;

float Voltage = 0.0;
float avgVoltage = 0;
float Current = 0.0;
float avgCurrent = 0;
float avgV = 0;
float avgC = 0;
float ref = 0;
float fuelV = 0;
float bars = 0;
float fuelLevel = 0;

//icons to display

const unsigned char sysCh [] PROGMEM = {
	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, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x01, 0x83, 0xfc, 0x18, 0x00, 0x00, 0x00, 0x03, 
	0xcf, 0xff, 0x3c, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 
	0xfe, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xf8, 0x00, 
	0x00, 0x00, 0x03, 0xff, 0x9f, 0xf8, 0x00, 0x00, 0x00, 0x03, 0xfc, 0x07, 0xfc, 0x00, 0x00, 0x00, 
	0x03, 0xf8, 0x01, 0xfc, 0x00, 0x00, 0x00, 0x07, 0xf0, 0x01, 0xfc, 0x00, 0x00, 0x00, 0x0f, 0xf0, 
	0x00, 0xfe, 0x00, 0x00, 0x00, 0x7f, 0xe0, 0x00, 0xff, 0xc0, 0x00, 0x00, 0x7f, 0xe0, 0x00, 0x7f, 
	0xc0, 0x00, 0x00, 0x7f, 0xe0, 0x00, 0x7f, 0xce, 0x00, 0x00, 0x7f, 0xe0, 0x00, 0x7f, 0xcf, 0x00, 
	0x00, 0x3f, 0xe0, 0x00, 0xff, 0xcf, 0x00, 0x00, 0x07, 0xf0, 0x00, 0xff, 0x1f, 0x8c, 0x00, 0x07, 
	0xf0, 0x01, 0xff, 0xff, 0xfe, 0x00, 0x03, 0xf8, 0x03, 0xff, 0xff, 0xfe, 0x00, 0x03, 0xfe, 0x07, 
	0xff, 0xff, 0xfc, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x03, 0xff, 0xff, 0xff, 0xe0, 
	0x7c, 0x00, 0x07, 0xff, 0xff, 0xff, 0xc0, 0x3e, 0x00, 0x07, 0xff, 0xff, 0xff, 0x80, 0x3f, 0x00, 
	0x07, 0xff, 0xff, 0xff, 0x80, 0x1f, 0xc0, 0x03, 0xcf, 0xfe, 0x3f, 0x80, 0x1f, 0xc0, 0x01, 0x83, 
	0xf8, 0x3f, 0x80, 0x1f, 0xc0, 0x00, 0x01, 0xf0, 0x0f, 0x80, 0x3f, 0x00, 0x00, 0x01, 0xf0, 0x07, 
	0xc0, 0x3e, 0x00, 0x00, 0x01, 0xf0, 0x07, 0xe0, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x03, 0xfb, 0xfc, 
	0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xfe, 0x00, 0x00, 
	0x00, 0x00, 0x07, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x03, 0x3f, 0x8c, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 
	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
};

const unsigned char pwrOff [] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x03, 0x81, 
	0xf0, 0x78, 0x00, 0x00, 0x00, 0x07, 0xc1, 0xf0, 0x7c, 0x00, 0x00, 0x00, 0x0f, 0xc1, 0xf0, 0xfe, 
	0x00, 0x00, 0x00, 0x1f, 0xc1, 0xf0, 0x7f, 0x00, 0x00, 0x00, 0x3f, 0x81, 0xf0, 0x7f, 0x80, 0x00, 
	0x00, 0x7f, 0x01, 0xf0, 0x1f, 0xc0, 0x00, 0x00, 0xfe, 0x01, 0xf0, 0x0f, 0xc0, 0x00, 0x00, 0xfc, 
	0x01, 0xf0, 0x07, 0xe0, 0x00, 0x01, 0xf8, 0x01, 0xf0, 0x07, 0xe0, 0x00, 0x01, 0xf8, 0x01, 0xf0, 
	0x03, 0xf0, 0x00, 0x03, 0xf0, 0x01, 0xf0, 0x01, 0xf0, 0x00, 0x03, 0xf0, 0x01, 0xf0, 0x01, 0xf8, 
	0x00, 0x03, 0xe0, 0x01, 0xf0, 0x01, 0xf8, 0x00, 0x03, 0xe0, 0x01, 0xf0, 0x00, 0xf8, 0x00, 0x03, 
	0xe0, 0x00, 0xe0, 0x00, 0xf8, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x07, 0xe0, 0x00, 
	0x00, 0x00, 0xf8, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x00, 
	0xf8, 0x00, 0x03, 0xe0, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x03, 0xe0, 0x00, 0x00, 0x00, 0xf8, 0x00, 
	0x03, 0xe0, 0x00, 0x00, 0x01, 0xf8, 0x00, 0x03, 0xf0, 0x00, 0x00, 0x01, 0xf8, 0x00, 0x01, 0xf0, 
	0x00, 0x00, 0x01, 0xf0, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x03, 0xf0, 0x00, 0x01, 0xf8, 0x00, 0x00, 
	0x07, 0xe0, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x0f, 0xc0, 
	0x00, 0x00, 0x7f, 0x00, 0x00, 0x3f, 0xc0, 0x00, 0x00, 0x3f, 0xc0, 0x00, 0x7f, 0x80, 0x00, 0x00, 
	0x1f, 0xe0, 0x01, 0xff, 0x00, 0x00, 0x00, 0x0f, 0xfe, 0x0f, 0xfe, 0x00, 0x00, 0x00, 0x07, 0xff, 
	0xff, 0xfc, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xe0, 
	0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x03, 0xf8, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

const unsigned char lowFuel [] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0x80, 0x00, 0x00, 0x07, 0xff, 0xff, 0xc0, 
	0x00, 0x00, 0x0f, 0xff, 0xff, 0xe0, 0xe0, 0x00, 0x0f, 0x00, 0x01, 0xe0, 0xf0, 0x00, 0x0e, 0x00, 
	0x00, 0xe0, 0x78, 0x00, 0x0e, 0x00, 0x00, 0x60, 0x3c, 0x00, 0x0e, 0x00, 0x00, 0x60, 0x1e, 0x00, 
	0x0e, 0x00, 0x00, 0x60, 0x0f, 0x00, 0x0e, 0x00, 0x00, 0x60, 0x0f, 0x80, 0x0e, 0x00, 0x00, 0x60, 
	0x07, 0xc0, 0x0e, 0x00, 0x00, 0x60, 0x07, 0xc0, 0x0e, 0x00, 0x00, 0x60, 0x03, 0x80, 0x0e, 0x00, 
	0x00, 0x60, 0x03, 0x80, 0x0e, 0x00, 0x00, 0x60, 0x03, 0x80, 0x0e, 0x00, 0x00, 0x60, 0x03, 0x00, 
	0x0e, 0x00, 0x00, 0xe0, 0x03, 0x00, 0x0f, 0xff, 0xff, 0xe0, 0x03, 0x00, 0x0f, 0xff, 0xff, 0xf0, 
	0x03, 0x00, 0x0f, 0xff, 0xff, 0xfc, 0x03, 0x00, 0x0f, 0xff, 0xff, 0xfe, 0x03, 0x00, 0x0f, 0xff, 
	0xff, 0xff, 0x03, 0x00, 0x0f, 0xff, 0xff, 0xe7, 0x03, 0x00, 0x0f, 0xff, 0xff, 0xe3, 0x83, 0x00, 
	0x0f, 0xff, 0xff, 0xe3, 0x83, 0x80, 0x0f, 0xff, 0xff, 0xe3, 0x83, 0x80, 0x0f, 0xff, 0xff, 0xe3, 
	0x83, 0x80, 0x0f, 0xff, 0xff, 0xe3, 0x81, 0x80, 0x0f, 0xff, 0xff, 0xe3, 0x81, 0x80, 0x0f, 0xff, 
	0xff, 0xe3, 0x81, 0xc0, 0x0f, 0xff, 0xff, 0xe3, 0x81, 0xc0, 0x0f, 0xff, 0xff, 0xe3, 0x80, 0xc0, 
	0x0f, 0xff, 0xff, 0xe3, 0x80, 0xc0, 0x0f, 0xff, 0xff, 0xe3, 0x80, 0xe0, 0x0f, 0xff, 0xff, 0xe3, 
	0x80, 0xe0, 0x0f, 0xff, 0xff, 0xe3, 0x80, 0xe0, 0x0f, 0xff, 0xff, 0xe3, 0x80, 0xe0, 0x0f, 0xff, 
	0xff, 0xe3, 0x80, 0xe0, 0x0f, 0xff, 0xff, 0xe3, 0x80, 0xe0, 0x0f, 0xff, 0xff, 0xe1, 0xc0, 0xe0, 
	0x0f, 0xff, 0xff, 0xe1, 0xf3, 0xc0, 0x0f, 0xff, 0xff, 0xe0, 0xff, 0xc0, 0x0f, 0xff, 0xff, 0xe0, 
	0x7f, 0x80, 0x0f, 0xff, 0xff, 0xe0, 0x0c, 0x00, 0x0f, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x1f, 0xff, 
	0xff, 0xf8, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xf8, 0x00, 0x00, 
	0x1f, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

const unsigned char sysErr [] PROGMEM = {
	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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x01, 
	0x83, 0xf8, 0x20, 0x00, 0x10, 0x00, 0x03, 0xcf, 0xfe, 0x70, 0x00, 0x38, 0x00, 0x07, 0xff, 0xff, 
	0xf8, 0x00, 0x28, 0x00, 0x07, 0xff, 0xff, 0xfc, 0x00, 0x4c, 0x00, 0x03, 0xff, 0xff, 0xf8, 0x00, 
	0xd6, 0x00, 0x03, 0xff, 0xff, 0xf0, 0x00, 0x92, 0x00, 0x03, 0xff, 0x1f, 0xf0, 0x01, 0x93, 0x00, 
	0x03, 0xfc, 0x07, 0xf8, 0x01, 0x01, 0x00, 0x03, 0xf0, 0x03, 0xf8, 0x03, 0x11, 0x80, 0x07, 0xf0, 
	0x01, 0xfc, 0x06, 0x10, 0x80, 0x3f, 0xe0, 0x01, 0xff, 0x07, 0xff, 0xc0, 0x3f, 0xe0, 0x00, 0xff, 
	0x87, 0xff, 0xc0, 0x3f, 0xe0, 0x00, 0xff, 0x80, 0x00, 0x00, 0x3f, 0xe0, 0x00, 0xff, 0x9c, 0x00, 
	0x00, 0x3f, 0xe0, 0x00, 0xff, 0x9c, 0x00, 0x00, 0x0f, 0xe0, 0x01, 0xfe, 0x3e, 0x10, 0x00, 0x07, 
	0xf0, 0x01, 0xff, 0xff, 0xf8, 0x00, 0x03, 0xf8, 0x03, 0xff, 0xff, 0xf8, 0x00, 0x03, 0xfc, 0x07, 
	0xff, 0xff, 0xf8, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x03, 0xff, 0xff, 0xff, 0xc1, 
	0xf8, 0x00, 0x07, 0xff, 0xff, 0xff, 0x80, 0xf8, 0x00, 0x07, 0xff, 0xff, 0xff, 0x00, 0x7e, 0x00, 
	0x03, 0xff, 0xff, 0xff, 0x00, 0x7f, 0x00, 0x01, 0x87, 0xfc, 0x7f, 0x00, 0x7f, 0x00, 0x00, 0x03, 
	0xf0, 0x7f, 0x00, 0x7f, 0x00, 0x00, 0x01, 0xf0, 0x1f, 0x00, 0xf8, 0x00, 0x00, 0x01, 0xf0, 0x0f, 
	0x80, 0xf8, 0x00, 0x00, 0x01, 0xe0, 0x0f, 0xc3, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xf0, 
	0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xf8, 0x00, 0x00, 
	0x00, 0x00, 0x0e, 0xff, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

const unsigned char lowFuelS [] PROGMEM = {
  0x00, 0x00, 0x30, 0x3f, 0xc0, 0x30, 0x3f, 0xe4, 0x30, 0x20, 0x23, 0x30, 0x20, 0x21, 0x30, 0x20, 
	0x21, 0xb0, 0x20, 0x21, 0xb0, 0x20, 0x21, 0x30, 0x3f, 0xf1, 0x30, 0x3f, 0xf9, 0x30, 0x3f, 0xe9, 
	0x30, 0x3f, 0xe9, 0xb0, 0x3f, 0xe8, 0xb0, 0x3f, 0xe8, 0xb0, 0x3f, 0xe8, 0xb0, 0x3f, 0xe8, 0xb0, 
	0x3f, 0xec, 0xb0, 0x3f, 0xe7, 0xb0, 0x7f, 0xe0, 0x30, 0x7f, 0xf0, 0x30
};

const unsigned char keySensor [] PROGMEM = {
	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, 
	0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xc0, 
	0x00, 0x00, 0x00, 0x00, 0xfe, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x01, 0xf0, 0x00, 0xf8, 0x00, 0x00, 
	0x00, 0x03, 0xc0, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x07, 0x83, 0xfc, 0x1e, 0x00, 0x00, 0x00, 0x07, 
	0x0f, 0xff, 0x0e, 0x00, 0x00, 0x00, 0x0e, 0x1f, 0x9f, 0x87, 0x00, 0x00, 0x00, 0x1e, 0x3c, 0x03, 
	0xc7, 0x00, 0x00, 0x00, 0x1c, 0x78, 0x01, 0xe3, 0x80, 0x00, 0x00, 0x1c, 0xf0, 0xf0, 0xe3, 0x80, 
	0x00, 0x00, 0x38, 0xe3, 0xfc, 0x71, 0xc0, 0x00, 0x00, 0x30, 0xc7, 0xfc, 0x71, 0xc0, 0x00, 0x00, 
	0x36, 0x01, 0x0e, 0x39, 0xc0, 0x00, 0x00, 0x2f, 0xfc, 0x0e, 0x39, 0xc0, 0x00, 0x00, 0x2f, 0xff, 
	0xc6, 0x39, 0xc0, 0x00, 0x00, 0x2f, 0xff, 0xce, 0x39, 0xc0, 0x00, 0x00, 0x2f, 0xff, 0xce, 0x31, 
	0xc0, 0x00, 0x00, 0x0f, 0xff, 0xdc, 0x71, 0xc0, 0x00, 0x03, 0xef, 0xff, 0xd8, 0x71, 0x80, 0x00, 
	0x0f, 0xcf, 0xff, 0xd0, 0xe3, 0x80, 0x00, 0x1f, 0xdf, 0xff, 0x81, 0xe3, 0x80, 0x00, 0x3f, 0xdf, 
	0xff, 0x83, 0xc7, 0x00, 0x00, 0x3f, 0x1f, 0xff, 0x9f, 0x87, 0x00, 0x00, 0x7e, 0xdf, 0xff, 0xbf, 
	0x0e, 0x00, 0x00, 0xfc, 0x1f, 0xff, 0xbc, 0x1c, 0x00, 0x00, 0xfc, 0x9f, 0xff, 0x80, 0x7c, 0x00, 
	0x01, 0xfd, 0x9f, 0xff, 0x80, 0xf8, 0x00, 0x03, 0xfc, 0x3f, 0xff, 0x07, 0xe0, 0x00, 0x07, 0xfc, 
	0xb8, 0x3f, 0x7f, 0xc0, 0x00, 0x0f, 0xfe, 0x03, 0x9f, 0x7f, 0x00, 0x00, 0x1f, 0xff, 0xbf, 0xdf, 
	0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xcf, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 
	0x00, 0x7f, 0xff, 0xfc, 0xff, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xe1, 0xfe, 0x00, 0x00, 0x00, 0x3f, 
	0xff, 0x80, 0x06, 0x00, 0x00, 0x00, 0x3f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x80, 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
};



void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(7, OUTPUT);
  pinMode(Red, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(Blue, OUTPUT);  
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    for (;;);
  }
   if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  
  
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.clearDisplay();
  display.display();

  x = 0;
}

void printTime(){
  DateTime now = rtc.now();  // Get the current time
  
  display.drawFastHLine(30, 10, 69, WHITE);
  display.fillRect(50, 0, 30, 10, BLACK); // Clear previous time
  display.setCursor(52, 0);

  // Print hour with leading zero if needed
  if (now.hour() < 10) {
    display.print("0");
  }
  display.print(now.hour());
  display.print(":");

  // Print minute with leading zero if needed
  if (now.minute() < 10) {
    display.print("0");
  }
  display.print(now.minute());
  currM = now.minute();

  display.display();
}

void ReadValues() { //obtain all the values
  int AsensorValue = analogRead(A2);  // Read the analog input (0-1023)
  ref = AsensorValue * (5.0 / 1023.0);   // Convert the reading to voltage (assuming 5V reference)
  
  int sensorValue = analogRead(analogPin);
  float voltage = sensorValue * (4.8 / 1023.0);
  Voltage = voltage * ((R1 + R2) / R2);

  fuelV = Voltage - 11.3;
  fuelV = fuelV * 10;

  
  int currentValue = analogRead(currentSensorPin);
  float currentVoltage = currentValue * (3.6 / 1023.0);
  Current = (currentVoltage - ACS_OFFSET) / ACS_SENSITIVITY;
  if(Current < 1){
    Current  = 0;
  }

  for(int i=0; i<100; i++){
    avgV = avgV + Voltage;
 }
 avgV = avgV/100;


 for(int i=0; i<100; i++){
    avgC = avgC + Current;
 }
 avgC = avgC/100;

 Power = avgV * avgC;



}
void updateValue(){

}
void updateFuel() { //display the amount of fuel (battery level) left

 fuelLevel = map(fuelV, 0, 23, 0, 8); // Assuming 8 is the max number of bars
 bars = fuelLevel;

  display.fillRect(0, 0, 30, 64, BLACK); //used to clear only the fuel guage part, substitute to clearing the display

  display.setCursor(0, 0);
  display.println("F");
  display.setCursor(0, 55);
  display.println("E");

   // Map fuel level (0 to 100) to number of filled bars (0 to numBars)
    filledBars = fuelLevel;
    
    // Draw the bars
    for (int i = 0; i < 8; i++) {
        if (i < bars) {
            display.fillRect(10, SCREEN_HEIGHT - (i + 1) * barHeight, barWidth, barHeight - barSpacing, SSD1306_WHITE);
        } else {
            display.drawRect(10, SCREEN_HEIGHT - (i + 1) * barHeight, barWidth, barHeight - barSpacing, SSD1306_WHITE);
        }
    }

    
      
  


  display.display();
}

 void showScreen() {
  display.fillRect(53, 58, 30, 11, BLACK); // Clear the area of the squares

  if(digitalRead(3) == LOW) {
    if(scrnSw == 0){
    screen++;
    scrnSw = 1;
  }}
  if(digitalRead(3) == HIGH){
    scrnSw = 0;
  }

  if(screen > 2) {
    screen = 1;
  }

 if(screen == 1){
    display.fillRect(54, 59, 5, 5, WHITE);
    display.fillRect(61, 61, 3, 3, WHITE);
    display.fillRect(68, 61, 3, 3, WHITE);
    display.fillRect(74, 61, 3, 3, WHITE);

     
  }
  else if(screen == 2){
    display.fillRect(54, 60, 3, 3, WHITE);
    display.fillRect(60, 59, 5, 5, WHITE);
    display.fillRect(68, 61, 3, 3, WHITE);
    display.fillRect(74, 61, 3, 3, WHITE);
  }
  else if(screen == 3){
    display.fillRect(54, 61, 3, 3, WHITE);
    display.fillRect(61, 61, 3, 3, WHITE);
    display.fillRect(67, 59, 5, 5, WHITE);
    display.fillRect(74, 61, 3, 3, WHITE);
  }
  else if(screen == 4){
    display.fillRect(54, 61, 3, 3, WHITE);
    display.fillRect(61, 61, 3, 3, WHITE);
    display.fillRect(68, 61, 3, 3, WHITE);
    display.fillRect(74, 59, 5, 5, WHITE);
  }

  display.display(); // Refresh the display to show changes
}
void warnings(){
  display.drawFastVLine(100, 0, 64, WHITE);
  if(bars < 2){
    display.fillRect(102, 0, 128, 22, BLACK);
    display.drawBitmap(102, 0, lowFuelS, 18, 20, WHITE);
    display.display();
  }
  else if(bars > 1){
    display.fillRect(102, 0, 128, 22, BLACK);
    display.display();
  }
}

void shutdown(){
  
  digitalWrite(7, LOW);
 
  digitalWrite(GreenLED, LOW);
  digitalWrite(BlueLED, LOW);

  display.clearDisplay();
  display.drawBitmap(40, 4, pwrOff, 50, 50, WHITE);
  display.display();
  delay(1000);

  digitalWrite(RedLED, LOW);
  
  display.clearDisplay();
  display.display();
  start = 0;
  x = 0;
  var = 0;
  delay(2000);
  display.clearDisplay();
  display.display();

}


  void loop() {
   
    // Read Values and update conditions
    ReadValues();
    

    // Check power button on D2
    if(digitalRead(4) == LOW && digitalRead(5) == LOW) {
      if(start == 0){
        analogWrite(RedLED, 176);
        analogWrite(GreenLED, 200);
        display.clearDisplay(); // Clear any previous display
        display.drawBitmap(40, 4, sysCh, 50, 50, WHITE);
        display.setCursor(35, 55);
        display.print("System Check");
        display.display(); // Refresh the display
        delay(1000); // Show for 1 second
        digitalWrite(Red, LOW);
        digitalWrite(Green, LOW);
        if(Voltage < 11.4){
          analogWrite(Red, 180);
          digitalWrite(GreenLED, LOW);
          digitalWrite(BlueLED, LOW);
          display.clearDisplay();
          display.drawBitmap(35, 10, sysErr, 50, 48, WHITE);
          display.setCursor(20, 54);
          display.print("Unable To Start!");
          display.display();
          delay(2000);

          display.clearDisplay();
          display.drawBitmap(45, 0, lowFuel, 43, 50, WHITE);
          display.setCursor(48, 54);
          display.print("No Fuel!");
          display.display();
          delay(1500);
          display.clearDisplay();
          display.display();
          shutdown();
        }

        // Clear the display after the delay
        display.clearDisplay();
        display.display(); // Refresh to show the cleared display

        
    
        if(Voltage > 11.3) {
          
            start = 1;
        }
        
    }}

    if(digitalRead(4) == LOW && digitalRead(5) == LOW){
      if(start == 1){
        shutdown();
      }
    }

    if(start == 1) {
      if(Voltage < 11.3){
        display.clearDisplay();
       display.drawBitmap(45, 0, lowFuel, 43, 50, WHITE);
       display.setCursor(48, 54);
       display.print("No fuel!");
       display.display();
       delay(2500);
       shutdown();
      }}

    if(start == 1) {

      digitalWrite(7, HIGH);

      

      analogWrite(RedLED, 140);
      analogWrite(GreenLED, 210);
      analogWrite(BlueLED, 255);

      warnings();
      
        printTime();
        showScreen();
        if(x == 0) {
            ReadValues();
            updateFuel();
            x = 1;
        }
        if(screen == 1){
          display.fillRect(31, 11, 68, 40, BLACK);
          display.setCursor(35, 20);
          display.print("V :");         
          display.setCursor(55, 20);
          display.print(avgV, 1);
          display.setCursor(35, 30);
          display.print("I :");
          display.setCursor(55, 30);
          display.print(avgC);
          display.display();
        }



    if (screen == 2){
    display.fillRect(31, 11, 68, 40, BLACK);
   
    display.setTextSize(2);
    display.setTextColor(WHITE);
   
    display.setCursor(50, 32);
    display.print(Power);
    display.setCursor(90, 32);
    display.setTextSize(1);
    display.print("W");
    delay(100);}

        
 
        // Check if the minute has changed
        if (currM > prevM) {
            var = 0;
            if (var == 0) {
                ReadValues();  
                updateFuel();
                prevM = currM; 
                var = 1;        
            }
        }
    }
}