/******************************************************************************
* File name: Fitbit_4C3
* Author:  David Truong
* ID    :  A15402277
* Email :  dtruong@ucsd.edu
* 
* Credit:         
* mikalhart - TinyGPSplus
* mdraber - Analog style clock
* Mantex Electronics - Tetris
* shveytank - Pong         
* Peter Clement - Wifi Scanner
* cyaninfinite - MPU6050 cube
* Jochen Peters - step counter
* xtronical - Heartrate graph
* Chris Rouse - Moon Calculator
*          
******************************************************************************/

/*-----------------------------------(Libaries)--------------------------------------------*/
#include <RTClib.h>
#include <TimeLib.h>  
#include <SPI.h>
#include <Wire.h>
#include <TinyGPS++.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ezButton.h>
#include <ezBuzzer.h> 
#include <HardwareSerial.h>
#include "WiFi.h"

/*Unused*/
//#include "BluetoothSerial.h"

/*-----------------------------------(time control Setup)--------------------------------------------*/
long previousMillis = 0;       
long interval = 1000; 

/*-----------------------------------(Wifi/bluetooth Setup)--------------------------------------------*/
#define STA_SSID "<YOUR-SSID>"
#define STA_PASS "<YOUR-PASSWD>"
int WirelE = 0; 
int Ewifi = 0;  
int WifiScan = 0;  

/*Unused*/
//BluetoothSerial SerialBT;
//int Ebluetooth = 0;  
//int BleScan = 0;  

/*-----------------------------------(Sleep Setup)--------------------------------------------*/
int sleepsecM = 0;
int sleeptimemenu = 0;  

/*-----------------------------------(Screen Setup)--------------------------------------------*/
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define WIDTH 64 
#define HEIGHT 128 
#define OLED_RESET -1   
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

/*-----------------------------------(GPS setup)--------------------------------------------*/
#define RXD1 20
#define TXD1 21
#define time_offset -28800 //cali time
HardwareSerial ss(1);
TinyGPSPlus gps;
byte last_second, Second, Minute, Hour, Day, Month;
int Year;
double latStart = 0.0;
double longStart = 0.0;
double latEnd = 0.0;
double longEnd = 0.0;
int stateDC = 0;
int choiceDC = 0;
int phase = 0;

/*-----------------------------------(Calendar Setup)--------------------------------------------*/
int startDay = 0; // Sunday's value is 0, Saturday is 6
String week1 ="";
String week2 ="";
String week3 ="";
String week4 ="";
String week5 ="";
int newWeekStart = 0; // used to show start of next week of the month
char monthString2[37]= {"JanFebMarAprMayJunJulAugSepOctNovDec"};
int  monthIndex2[122] ={0,3,6,9,12,15,18,21,24,27,30,33};
char monthName2[3]="";
int monthLength = 0;

/*-----------------------------------(Clock Setup)--------------------------------------------*/
const float pi = 3.14159267;
const int clock_center_x=64;
const int clock_center_y=32;
int o=1; int xb1; int yb1;
float xw1; float yw1;

/*-----------------------------------(Compass Setup)--------------------------------------------*/
int hSpin = 60; int vSpin = 32;
int SpinRadius = 60;
static int armLength = 29;
int degressinput;

/*-----------------------------------(GPS/RTC time setup)--------------------------------------------*/
int Seconds, Minutes, Hours, Days, Months, Years;
int weekoftheday; int curhdop = 0;
double currlat = 0;
double currlon = 0;
double curralt = 0;
double currspeed = 0;
double curralt1 = 0;
double currspeed1 = 0;
double currvalue = 0;
int currsat = 0;
int degrees1 = 0;
RTC_DS3231 rtc1; //Main time

/*-----------------------------------(GPS status setup)--------------------------------------------*/
int ssValid = 0;
int locValid = 0;
int timeValid = 0;
int dateValid = 0;
int satValid = 0;

/*-----------------------------------(Battery setup)--------------------------------------------*/
#define BATTV_MAX    4.2     // maximum voltage of battery
#define BATTV_MIN    3.2     // what we regard as an empty battery
#define BATTV_LOW    3.4     // voltage considered to be low battery
float sensorValue;  
float calibration = 0.89;
int bat_percentage;
long interval1 = 400; int batsec = 0;
float totalV = 0; int totalP = 0;
float totalVV = 0; int totalPP = 0;

/*-----------------------------------(Stopwatch setup)--------------------------------------------*/
//hours, minutes, seconds
int h = 0; int m = 0; int s = 0;
int starttime = 0;
int stoptime = 0;
int cleartime = 0;
int transfertime = 0;
int millisec = 0;
int savedtime = 0;
String savedtime1 = " ";
String savedtime2 = " ";
String savedtime3 = " ";
String savedtime4 = " ";
String savedtime5 = " ";
String savedtime6 = " ";
String savedtime7 = " ";
String savedtime8 = " ";

/*-----------------------------------(Timer setup)--------------------------------------------*/
int ht = 0; int mt = 0; int st = 0;
int alarmtime = 0;
int timephase = 0;
int timeadjust = 0;
int adjustt = 0;
int vertify = 0;
int millistart = 0;

/*-----------------------------------(Calc setup)--------------------------------------------*/
int selectkey = 0; 
int enterkey = 0;
String inputString = " "; 
int charlimit = 0;
float num1 = 0.0; 
float num2 = 0.0; 
float numberFinal = 0.0;
float storedvalue = 0.0; int enOP = 0; int enPls = 0; int enMin = 0; int enDiv = 0; int enMul = 0; int enExp = 0;
int enEnt = 0;  int enDec = 0; int recursive = 0;

/*-----------------------------------(MPU6050 setup)--------------------------------------------*/
const int MPU=0x69;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
long timer = 0; int steps = 0; float kalori = 0;
float distanceinonestep = 71; //change it according to your distance between your legs in cm
float distance; int range=0; double dT; 
long stoss; long firstStoss =-1; int val;
long barrier = 0; long treshold = 6; 
int UpperThrestep = 1022;
int LowerThrestep = 1000;
int readingsteps = 0;
float SPM = 0.0;
bool IgnoreStepReading = false;
bool FirstStepDetected = false;
unsigned long FirstStepTime = 0;
unsigned long SecondStepTime = 0;
unsigned long StepInterval = 0;
int stepchoice = 0;

/*-----------------------------------(Heartrate setup)--------------------------------------------*/
int sec = 0;
int i = 0; int j = 20;
int jj = 10; int beats = 0; 
const int sensorPin = 3;
int UpperThreshold = 922;
int LowerThreshold = 900;
int reading = 0; float BPM = 0.0;
bool IgnoreReading = false;
bool FirstPulseDetected = false;
unsigned long FirstPulseTime = 0;
unsigned long SecondPulseTime = 0;
unsigned long PulseInterval = 0;

/*--Variable Declarations for the graph-----------------------------------------------------------------*/
unsigned long previousMillisGetHR = 0; 
unsigned long previousMillisResultHR = 0; 
const long intervalGetHR = 20; 
const long intervalResultHR = 10000; 
int PulseSensorSignal;  int cntHB = 0;
boolean ThresholdStat = true; 
int BPMval = 0;
int x=0; int y=0; 
int lastx=0; int lasty=0; 

/*-----------------------------------(Tetris setup)--------------------------------------------*/
const char pieces_S_l[2][2][4] = {{{0, 0, 1, 1}, {0, 1, 1, 2}},
                                   {{0, 1, 1, 2}, {1, 1, 0, 0}}};
                                   
const char pieces_S_r[2][2][4] = {{{1, 1, 0, 0}, {0, 1, 1, 2}},
                                 {{0, 1, 1, 2}, {0, 0, 1, 1}}};
                                 
const char pieces_L_l[4][2][4] = {{{0, 0, 0, 1}, {0, 1, 2, 2}},
                                 {{0, 1, 2, 2}, {1, 1, 1, 0}},
                                 {{0, 1, 1, 1}, {0, 0, 1, 2}},
                                 {{0, 0, 1, 2}, {1, 0, 0, 0}}};
                                 
const char pieces_Sq[1][2][4] = {{{0, 1, 0, 1}, {0, 0, 1, 1}}};

const char pieces_T[4][2][4] = {{{0, 0, 1, 0},{0, 1, 1, 2}},
                                 {{0, 1, 1, 2},{1, 0, 1, 1}},
                                 {{1, 0, 1, 1},{0, 1, 1, 2}},
                                 {{0, 1, 1, 2},{0, 0, 1, 0}}};
                                 
const char pieces_l[2][2][4] = {{{0, 1, 2, 3}, {0, 0, 0, 0}},
                                 {{0, 0, 0, 0}, {0, 1, 2, 3}}};

const short MARGIN_TOP = 19;
const short MARGIN_LEFT = 3;
const short SIZE = 5;
const short TYPES = 6;
int tetphase = 0;
word currentType, nextType, rotation;
short pieceX, pieceY;
short piece[2][4];
int intervalTetris = 500, score;
long TimerTet, delayer;
boolean grid[10][18];
boolean b1, b2, b3;
int seedpin = 2; 
/*-----------------------------------(Pong setup)--------------------------------------------*/
#define UP_BUTTON 10
#define DOWN_BUTTON 9
const unsigned long PADDLE_RATE = 33;
const unsigned long BALL_RATE = 16;
const uint8_t PADDLE_HEIGHT = 24;
void drawCourt();
uint8_t ball_x = 64, ball_y = 32;
uint8_t ball_dir_x = 1, ball_dir_y = 1;
unsigned long ball_updatepong;
unsigned long paddle_updatepong;
const uint8_t CPU_X = 12;
uint8_t cpu_y = 16;
const uint8_t PLAYER_X = 115;
uint8_t player_y = 16;
int pongphase = 0;

/*-----------------------------------(Cube setup)--------------------------------------------*/
float xx,xy,xz; float yx,yy,yz; float zx,zy,zz; float fact;
int Xan,Yan; int Xoff; int Yoff; int Zoff;
struct Point3d {int x; int y; int z;};
struct Point2d {int x; int y; };
int LinestoRender; // lines to render.
int OldLinestoRender; // lines to render just in case it changes. this makes sure the old lines all get erased.
struct Line3d {Point3d p0; Point3d p1;};
struct Line2d {Point2d p0; Point2d p1;};
Line3d Lines[12];  //Number of lines to render
Line2d Render[12];
Line2d ORender[12];

/*-----------------------------------(Buttons/states setup)--------------------------------------------*/
int choice = 0; int enter = 0; int Spause = 0; int Sclear = 0; 
int stateM = 0; int substateM = 0; 
int gpsstate = 0; int subgpsstate = 0;
int healthstate = 0; int subhealthstate = 0;
int gamessstate = 0; int subgamessstate = 0; 
int utilitystate = 0; int subutilitystate = 0;
int otherstate = 0; int subotherstate = 0;
int networkstate = 0; int subnetworkstate = 0;
int settingsstate = 0; int subsettingsstate = 0; 
ezButton button1(5); //Enter
ezButton button2(4); ezButton button5(10); //L1 and R1
ezButton button3(8); ezButton button4(9); //L2 and R2
int bpress1 = 5; int bstate1;  
int bpress2 = 4; int bstate2; int bpress5 = 10; int bstate5;
int bpress3 = 8; int bstate3; int bpress4 = 9; int bstate4;
/*Unused*/
//const int buzzer1 = 18;
//ezBuzzer buzzer(buzzer1);

/*-----------------------------------(Settings setup)--------------------------------------------*/
int cpuspeed = 80; int cpuFreq;
int cpuspeedState = 3; 
int screenE = 1;
int setSleep = 200; int sleepState = 1;
int settime = 0;
int timecali = 0;
int helpsel = 0;
String WifiYN = " ";

/*Unused*/
//String BltYN = " ";

/*-----------------------------------(Total bytes used to store images in PROGMEM = 7280)--------------------------------------------*/

const unsigned char Logo [] 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, 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, 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, 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, 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, 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x03, 0xf0, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x0f, 0x80, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x1f, 0x83, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x3f, 0x8f, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x3f, 0x9c, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x7f, 0xf0, 0x1f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x7f, 0xf0, 0x1f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x7f, 0xb8, 0x3b, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x0c, 0x06, 0xff, 0x9c, 0x72, 0xfd, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x1e, 0x0e, 0xff, 0x8c, 0xe2, 0x3d, 0xc0, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x12, 0x1e, 0xff, 0x80, 0x02, 0x1d, 0xe0, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x13, 0x1e, 0xff, 0x80, 0x02, 0x0d, 0xf3, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x1e, 0xbe, 0xff, 0x84, 0x42, 0x1d, 0xf6, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x1e, 0x3e, 0xff, 0x8c, 0x62, 0x1c, 0xf8, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x1e, 0x3c, 0x63, 0x98, 0x72, 0x1c, 0xf8, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x16, 0x1c, 0x71, 0xf0, 0x3e, 0x1c, 0x70, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x16, 0x00, 0x70, 0x70, 0x1e, 0x38, 0x00, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x06, 0x00, 0x38, 0x18, 0x7e, 0x38, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x38, 0x0e, 0xfe, 0x70, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x08, 0x00, 0x1c, 0x03, 0xfe, 0xf0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x08, 0x00, 0x1e, 0x0f, 0xff, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x08, 0x00, 0x0f, 0xbf, 0xff, 0xc0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x08, 0x00, 0x07, 0xff, 0xff, 0x80, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfc, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf0, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x40, 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, 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, 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, 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, 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, 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, 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const unsigned char bitGps [] PROGMEM = {
  0x00, 0x00, 0x03, 0xc0, 0x07, 0xe0, 0x06, 0x60, 0x0c, 0x30, 0x0c, 0x30, 0x06, 0x60, 0x07, 0xe0, 
  0x07, 0xe0, 0x03, 0xe0, 0x03, 0xc0, 0x01, 0xc0, 0x01, 0x80, 0x00, 0x80, 0x08, 0x10, 0x00, 0x00
};
const unsigned char bitBattery [] PROGMEM = {
  0x00, 0x00, 0x3f, 0xfe, 0x3f, 0xfe, 0x60, 0x02, 0x60, 0x02, 0xe0, 0x02, 0xc0, 0x02, 0xc0, 0x02, 
  0xc0, 0x02, 0xc0, 0x02, 0xe0, 0x02, 0x60, 0x02, 0x60, 0x02, 0x3f, 0xfe, 0x3f, 0xfe, 0x00, 0x00
};
const unsigned char bitWifi [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x0f, 0xf0, 0x3c, 0x3c, 0x60, 0x06, 0xc7, 0xe3, 0x1e, 0x78, 0x30, 0x0c, 
  0x27, 0xe4, 0x0e, 0x70, 0x0c, 0x30, 0x01, 0x80, 0x03, 0xc0, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00
};
const unsigned char bitBluetooth [] PROGMEM = {
  0x00, 0x80, 0x00, 0xc0, 0x00, 0xe0, 0x18, 0xf0, 0x0c, 0x98, 0x06, 0xf0, 0x03, 0xe0, 0x01, 0xc0, 
  0x01, 0xc0, 0x03, 0xe0, 0x06, 0xf0, 0x0c, 0x98, 0x18, 0xf0, 0x00, 0xe0, 0x00, 0xc0, 0x00, 0x80
};

const unsigned char full_moon_bits[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xc0, 0x00, 0x00, 0x7f, 0xf8, 0x00, 0x01, 0xff, 0xfe, 0x00, 
  0x03, 0xff, 0xff, 0x00, 0x07, 0xff, 0xff, 0x80, 0x0f, 0xff, 0xff, 0xc0, 0x1f, 0xff, 0xff, 0xe0, 
  0x1f, 0xff, 0xff, 0xe0, 0x3f, 0xff, 0xff, 0xf0, 0x3f, 0xff, 0xff, 0xf0, 0x7f, 0xff, 0xff, 0xf8, 
  0x7f, 0xff, 0xff, 0xf8, 0x7f, 0xff, 0xff, 0xf8, 0x7f, 0xff, 0xff, 0xf8, 0x7f, 0xff, 0xff, 0xf8, 
  0x7f, 0xff, 0xff, 0xf8, 0x7f, 0xff, 0xff, 0xf8, 0x7f, 0xff, 0xff, 0xf8, 0x3f, 0xff, 0xff, 0xf0, 
  0x3f, 0xff, 0xff, 0xf0, 0x1f, 0xff, 0xff, 0xe0, 0x1f, 0xff, 0xff, 0xe0, 0x0f, 0xff, 0xff, 0xc0, 
  0x07, 0xff, 0xff, 0x80, 0x03, 0xff, 0xff, 0x00, 0x01, 0xff, 0xfe, 0x00, 0x00, 0x7f, 0xf8, 0x00, 
  0x00, 0x0f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00
};

const unsigned char waning_gibbous_bits[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xe0, 0x00, 0x00, 0x7f, 0xc8, 0x00, 0x01, 0xff, 0xe2, 0x00, 
  0x03, 0xff, 0xf1, 0x00, 0x07, 0xff, 0xf0, 0x80, 0x0f, 0xff, 0xf8, 0x40, 0x1f, 0xff, 0xf8, 0x00, 
  0x1f, 0xff, 0xfc, 0x00, 0x3f, 0xff, 0xfc, 0x00, 0x3f, 0xff, 0xfc, 0x10, 0x7f, 0xff, 0xfe, 0x00, 
  0x7f, 0xff, 0xfe, 0x00, 0x7f, 0xff, 0xfe, 0x00, 0x7f, 0xff, 0xfe, 0x08, 0x7f, 0xff, 0xfe, 0x00, 
  0x7f, 0xff, 0xfe, 0x00, 0x7f, 0xff, 0xfe, 0x00, 0x3f, 0xff, 0xfc, 0x10, 0x3f, 0xff, 0xfc, 0x00, 
  0x1f, 0xff, 0xfc, 0x00, 0x1f, 0xff, 0xf8, 0x00, 0x0f, 0xff, 0xf8, 0x40, 0x07, 0xff, 0xf0, 0x80, 
  0x03, 0xff, 0xf1, 0x00, 0x01, 0xff, 0xe0, 0x00, 0x00, 0x7f, 0xc8, 0x00, 0x00, 0x1f, 0xc0, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
 
const unsigned char last_quarter_bits[] PROGMEM = {
0x00, 0x0f, 0xc0, 0x00, 0x00, 0x7e, 0x18, 0x00, 0x01, 0xfe, 0x02, 0x00, 0x03, 0xfe, 0x01, 0x00, 
  0x0f, 0xfe, 0x00, 0x80, 0x0f, 0xfe, 0x00, 0x40, 0x1f, 0xfe, 0x00, 0x20, 0x3f, 0xfe, 0x00, 0x10, 
  0x3f, 0xfe, 0x00, 0x00, 0x7f, 0xfe, 0x00, 0x08, 0x7f, 0xfe, 0x00, 0x08, 0xff, 0xfe, 0x00, 0x00, 
  0xff, 0xfe, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x04, 0xff, 0xfe, 0x00, 0x04, 0xff, 0xfe, 0x00, 0x04, 
  0xff, 0xfe, 0x00, 0x04, 0xff, 0xfe, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, 0x7f, 0xfe, 0x00, 0x08, 
  0x7f, 0xfe, 0x00, 0x08, 0x7f, 0xfe, 0x00, 0x00, 0x3f, 0xfe, 0x00, 0x10, 0x1f, 0xfe, 0x00, 0x00, 
  0x1f, 0xfe, 0x00, 0x20, 0x0f, 0xfe, 0x00, 0x40, 0x07, 0xfe, 0x00, 0x80, 0x01, 0xfe, 0x02, 0x00, 
  0x00, 0xfe, 0x0c, 0x00, 0x00, 0x1f, 0xe0, 0x00
};
 
const unsigned char crescent_old_bits[] PROGMEM = {
0x00, 0x07, 0xc0, 0x00, 0x00, 0x30, 0x18, 0x00, 0x00, 0xf0, 0x02, 0x00, 0x03, 0xe0, 0x01, 0x00, 
  0x07, 0xc0, 0x00, 0x80, 0x0f, 0x80, 0x00, 0x40, 0x1f, 0x80, 0x00, 0x20, 0x1f, 0x00, 0x00, 0x10, 
  0x3f, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x08, 0x3e, 0x00, 0x00, 0x08, 0x7e, 0x00, 0x00, 0x00, 
  0x7e, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x04, 0x7e, 0x00, 0x00, 0x04, 0x7e, 0x00, 0x00, 0x04, 
  0x7e, 0x00, 0x00, 0x04, 0x7e, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x08, 
  0x3f, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x10, 0x1f, 0x80, 0x00, 0x00, 0x0f, 0x80, 0x00, 0x00, 
  0x07, 0xc0, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x01, 0xe0, 0x02, 0x00, 0x00, 0x60, 0x08, 0x00, 
  0x00, 0x0f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00
};
 
const unsigned char new_moon_bits[] PROGMEM = {
0x00, 0x0f, 0xe0, 0x00, 0x00, 0x60, 0x08, 0x00, 0x01, 0x80, 0x02, 0x00, 0x02, 0x00, 0x00, 0x80, 
  0x04, 0x00, 0x00, 0x40, 0x08, 0x00, 0x00, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 
  0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x40, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x04, 
  0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 
  0x00, 0x00, 0x00, 0x04, 0x40, 0x00, 0x00, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 
  0x20, 0x00, 0x00, 0x08, 0x20, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10, 0x08, 0x00, 0x00, 0x20, 
  0x04, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x80, 0x01, 0x00, 0x02, 0x00, 0x00, 0x40, 0x0c, 0x00, 
  0x00, 0x3f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00
};
 
const unsigned char crescent_new_bits[] PROGMEM = {
0x00, 0x18, 0xe0, 0x00, 0x00, 0xc0, 0x18, 0x00, 0x01, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x0f, 0x00, 
  0x00, 0x00, 0x0f, 0x80, 0x00, 0x00, 0x07, 0xc0, 0x10, 0x00, 0x03, 0xe0, 0x20, 0x00, 0x03, 0xe0, 
  0x00, 0x00, 0x03, 0xf0, 0x40, 0x00, 0x01, 0xf0, 0x40, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x01, 0xf8, 
  0x00, 0x00, 0x01, 0xf8, 0x80, 0x00, 0x01, 0xf8, 0x80, 0x00, 0x01, 0xf8, 0x80, 0x00, 0x01, 0xf8, 
  0x00, 0x00, 0x01, 0xf8, 0x00, 0x00, 0x01, 0xf8, 0x40, 0x00, 0x01, 0xf8, 0x40, 0x00, 0x01, 0xf0, 
  0x00, 0x00, 0x03, 0xf0, 0x20, 0x00, 0x03, 0xe0, 0x10, 0x00, 0x03, 0xe0, 0x00, 0x00, 0x07, 0xc0, 
  0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x1e, 0x00, 0x00, 0x40, 0x18, 0x00, 
  0x00, 0x1c, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00
};

const unsigned char first_quarter_bits[] PROGMEM = {
0x00, 0x03, 0x80, 0x00, 0x00, 0x30, 0xf0, 0x00, 0x00, 0x81, 0xfc, 0x00, 0x02, 0x01, 0xff, 0x00, 
  0x04, 0x01, 0xff, 0x80, 0x00, 0x01, 0xff, 0xc0, 0x00, 0x01, 0xff, 0xe0, 0x10, 0x01, 0xff, 0xe0, 
  0x20, 0x01, 0xff, 0xf0, 0x20, 0x01, 0xff, 0xf0, 0x00, 0x01, 0xff, 0xf8, 0x40, 0x01, 0xff, 0xf8, 
  0x40, 0x01, 0xff, 0xf8, 0x40, 0x01, 0xff, 0xfc, 0x40, 0x01, 0xff, 0xfc, 0x40, 0x01, 0xff, 0xfc, 
  0x40, 0x01, 0xff, 0xf8, 0x40, 0x01, 0xff, 0xf8, 0x00, 0x01, 0xff, 0xf8, 0x20, 0x01, 0xff, 0xf0, 
  0x20, 0x01, 0xff, 0xf0, 0x10, 0x01, 0xff, 0xe0, 0x00, 0x01, 0xff, 0xe0, 0x08, 0x01, 0xff, 0xc0, 
  0x04, 0x01, 0xff, 0x80, 0x00, 0x01, 0xff, 0x00, 0x00, 0x81, 0xfc, 0x00, 0x00, 0x21, 0xf0, 0x00, 
  0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00
};
 
const unsigned char waxing_gibbous_bits[] PROGMEM = {
  0x00, 0x07, 0xc0, 0x00, 0x00, 0x6f, 0xf8, 0x00, 0x00, 0x9f, 0xfe, 0x00, 0x02, 0x3f, 0xff, 0x00, 
  0x04, 0x3f, 0xff, 0x80, 0x08, 0x7f, 0xff, 0xc0, 0x10, 0x7f, 0xff, 0xe0, 0x00, 0xff, 0xff, 0xf0, 
  0x20, 0xff, 0xff, 0xf0, 0x01, 0xff, 0xff, 0xf8, 0x41, 0xff, 0xff, 0xf8, 0x41, 0xff, 0xff, 0xf8, 
  0x01, 0xff, 0xff, 0xfc, 0x01, 0xff, 0xff, 0xfc, 0x01, 0xff, 0xff, 0xfc, 0x01, 0xff, 0xff, 0xfc, 
  0x01, 0xff, 0xff, 0xfc, 0x41, 0xff, 0xff, 0xf8, 0x41, 0xff, 0xff, 0xf8, 0x41, 0xff, 0xff, 0xf8, 
  0x01, 0xff, 0xff, 0xf8, 0x20, 0xff, 0xff, 0xf0, 0x00, 0xff, 0xff, 0xf0, 0x10, 0x7f, 0xff, 0xe0, 
  0x08, 0x7f, 0xff, 0xc0, 0x04, 0x3f, 0xff, 0x80, 0x02, 0x1f, 0xff, 0x00, 0x00, 0x9f, 0xfe, 0x00, 
  0x00, 0x2f, 0xf8, 0x00, 0x00, 0x07, 0xc0, 0x00
};

// 'GPS', 40x40px
const unsigned char epd_bitmap_GPS [] PROGMEM = {
  0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x19, 0x80, 0x00, 0x00, 0x00, 
  0x30, 0xc0, 0x00, 0x00, 0x00, 0x60, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00, 0x70, 
  0x30, 0x00, 0x00, 0x00, 0x38, 0x18, 0x00, 0x00, 0x00, 0x0c, 0x1c, 0x00, 0x00, 0x00, 0x07, 0x0c, 
  0x00, 0x00, 0x07, 0x03, 0x86, 0x00, 0x00, 0x0f, 0x00, 0xec, 0x70, 0x00, 0x1e, 0x00, 0x7c, 0xf8, 
  0x00, 0x3e, 0x20, 0x11, 0x98, 0x00, 0x3c, 0xf0, 0x03, 0x18, 0x00, 0x78, 0xf0, 0x06, 0x30, 0x00, 
  0x79, 0xe0, 0x0c, 0x60, 0x00, 0x71, 0xc2, 0x18, 0xc4, 0x00, 0xf3, 0xcf, 0x0d, 0x9f, 0x00, 0xf3, 
  0x8f, 0x0f, 0x1b, 0x80, 0xe3, 0x9e, 0x02, 0x31, 0xc0, 0xe3, 0x9e, 0x00, 0x18, 0x60, 0xe3, 0x9c, 
  0x00, 0x18, 0x38, 0xe3, 0x9e, 0x00, 0x0c, 0x1c, 0xe3, 0x8e, 0x0c, 0x06, 0x06, 0xf3, 0x8f, 0xbe, 
  0x06, 0x03, 0xf3, 0xc7, 0xfc, 0x03, 0x03, 0x71, 0xc3, 0xfc, 0x01, 0x86, 0x79, 0xe0, 0xf0, 0xc0, 
  0xcc, 0x78, 0xf0, 0x01, 0xe0, 0xf8, 0x3c, 0x7c, 0x07, 0xc0, 0x70, 0x3e, 0x3f, 0xff, 0xc0, 0x00, 
  0x1e, 0x1f, 0xff, 0x0c, 0x00, 0x0f, 0x87, 0xfc, 0x3c, 0x00, 0x07, 0xc0, 0x00, 0x7c, 0x00, 0x03, 
  0xf0, 0x01, 0xf8, 0x00, 0x01, 0xfe, 0x0f, 0xf0, 0x00, 0x00, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x3f, 
  0xff, 0x80, 0x00, 0x00, 0x07, 0xfc, 0x00, 0x00
};
// 'Other', 40x40px
const unsigned char epd_bitmap_Other [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 
  0x07, 0xff, 0xc0, 0x00, 0x00, 0x1f, 0xff, 0xf0, 0x00, 0x00, 0x3f, 0xff, 0xf8, 0x00, 0x00, 0x7f, 
  0xff, 0xfc, 0x00, 0x00, 0xff, 0xff, 0xfe, 0x00, 0x00, 0xff, 0x83, 0xff, 0x00, 0x01, 0xfe, 0x00, 
  0xff, 0x00, 0x01, 0xfc, 0x00, 0x7f, 0x00, 0x03, 0xfc, 0x00, 0x7f, 0x00, 0x03, 0xf8, 0x00, 0x7f, 
  0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 
  0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0x01, 0xfe, 0x00, 0x00, 0x00, 0x07, 0xfc, 0x00, 0x00, 
  0x00, 0x0f, 0xf8, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x00, 0x00, 0x00, 
  0x7f, 0x80, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0x01, 0xfc, 
  0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x01, 0xfc, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x00, 
  0x01, 0xfc, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x00, 0x00, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'Settings', 37x40px
const unsigned char epd_bitmap_Settings [] PROGMEM = {
  0x07, 0x80, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x03, 0xf0, 0x00, 0x00, 0xc0, 0x00, 
  0xf8, 0x00, 0x01, 0xc0, 0x00, 0x78, 0x00, 0x03, 0x80, 0x00, 0x78, 0x00, 0x07, 0x80, 0x00, 0xf8, 
  0x00, 0x07, 0x00, 0x60, 0xf8, 0x00, 0x0f, 0x00, 0x31, 0xfc, 0x00, 0x1c, 0x00, 0x3f, 0xfc, 0x00, 
  0x38, 0x00, 0x1f, 0xfe, 0x00, 0x70, 0x00, 0x0f, 0xff, 0x00, 0xe0, 0x00, 0x07, 0xff, 0x81, 0xc0, 
  0x00, 0x00, 0x7f, 0xc3, 0x80, 0x00, 0x00, 0x1f, 0xe7, 0x00, 0x00, 0x00, 0x0f, 0xee, 0x00, 0x00, 
  0x00, 0x07, 0xdc, 0x00, 0x00, 0x00, 0x03, 0x98, 0x00, 0x00, 0x00, 0x01, 0x30, 0x00, 0x00, 0x00, 
  0x00, 0x66, 0x00, 0x00, 0x00, 0x04, 0xef, 0x00, 0x00, 0x00, 0x0f, 0xdf, 0x80, 0x00, 0x00, 0x0f, 
  0x9f, 0xc0, 0x00, 0x00, 0x1f, 0x9f, 0xe0, 0x00, 0x00, 0x3f, 0xcf, 0xf8, 0x00, 0x00, 0xff, 0x87, 
  0xff, 0x80, 0x03, 0xfe, 0x03, 0xff, 0xc0, 0x03, 0xfc, 0x01, 0xff, 0xe0, 0x07, 0xf8, 0x00, 0xff, 
  0xf0, 0x0f, 0xf8, 0x00, 0xfe, 0x30, 0x1f, 0xf0, 0x00, 0x7c, 0x18, 0x3f, 0xf0, 0x00, 0x7c, 0x00, 
  0x7f, 0xe0, 0x00, 0x78, 0x00, 0xff, 0xc0, 0x00, 0x78, 0x00, 0xff, 0x80, 0x00, 0x7c, 0x00, 0xff, 
  0x00, 0x00, 0x3f, 0x00, 0x7e, 0x00, 0x00, 0x1f, 0x80, 0x3e, 0x00, 0x00, 0x07, 0x80, 0x1c, 0x00, 
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'Utility', 40x40px
const unsigned char epd_bitmap_Utility [] PROGMEM = {
  0x1f, 0xff, 0xff, 0xff, 0xf8, 0x70, 0x00, 0x00, 0x00, 0x0e, 0xc0, 0x00, 0x00, 0x00, 0x03, 0xc0, 
  0x00, 0x00, 0x00, 0x03, 0xc7, 0xff, 0xff, 0xff, 0xe3, 0xc6, 0x00, 0x00, 0x00, 0x23, 0xc6, 0x00, 
  0x00, 0x00, 0x23, 0xc6, 0x00, 0x00, 0x00, 0x23, 0xc6, 0x00, 0x00, 0x00, 0x23, 0xc6, 0x00, 0x00, 
  0x00, 0x23, 0xc6, 0x00, 0x00, 0x00, 0x23, 0xc6, 0x00, 0x00, 0x00, 0x23, 0xc7, 0xff, 0xff, 0xff, 
  0xe3, 0xc7, 0xff, 0xff, 0xff, 0xe3, 0xc0, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x03, 
  0xc3, 0x81, 0x81, 0xc1, 0xc3, 0xc7, 0xe7, 0xe7, 0xe7, 0xe3, 0xcc, 0x2c, 0x3c, 0x34, 0x33, 0xcc, 
  0x2c, 0x3c, 0x34, 0x33, 0xc7, 0xe7, 0xe7, 0xe7, 0xe3, 0xc3, 0x83, 0xc3, 0xc1, 0xc3, 0xc0, 0x00, 
  0x00, 0x00, 0x03, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc6, 0x66, 0x66, 0x66, 0x73, 0xcc, 0x2c, 0x3c, 
  0x34, 0x33, 0xcc, 0x6c, 0x36, 0x34, 0x33, 0xc7, 0xe7, 0xe7, 0xe4, 0x33, 0xc0, 0x00, 0x00, 0x04, 
  0x33, 0xc0, 0x00, 0x00, 0x04, 0x33, 0xc7, 0xe7, 0xe7, 0xe4, 0x33, 0xcc, 0x6e, 0x76, 0x34, 0x33, 
  0xcc, 0x2c, 0x3c, 0x34, 0x33, 0xce, 0x66, 0x66, 0x76, 0x33, 0xc7, 0xc3, 0xc3, 0xc3, 0xe3, 0xc0, 
  0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x03, 0x70, 0x00, 
  0x00, 0x00, 0x0e, 0x1f, 0xff, 0xff, 0xff, 0xf8
};
// 'Health', 40x40px
const unsigned char epd_bitmap_Health [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00, 0x3f, 0x00, 0x07, 0xff, 0x00, 0xff, 0xc0, 0x0f, 
  0xff, 0x83, 0xff, 0xf0, 0x1f, 0xff, 0xc3, 0xff, 0xf8, 0x3f, 0xff, 0xe7, 0xff, 0xf8, 0x3f, 0xff, 
  0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, 0xfe, 0x7f, 0xff, 0xff, 
  0xff, 0xfe, 0x7f, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xe7, 0xff, 0xfe, 0xff, 0xff, 0xc7, 0xff, 
  0xfe, 0xff, 0xff, 0xc7, 0xff, 0xfe, 0x7f, 0xff, 0xc7, 0xff, 0xfe, 0x7f, 0xff, 0xc7, 0xff, 0xfe, 
  0x7f, 0xfb, 0xc3, 0xff, 0xfe, 0x7f, 0xf1, 0x93, 0xff, 0xfe, 0x7f, 0xf1, 0x93, 0xff, 0xfc, 0x3f, 
  0xf1, 0x93, 0xdf, 0xfc, 0x3f, 0xe0, 0x93, 0x8f, 0xfc, 0x1f, 0xe0, 0x39, 0x8f, 0xf8, 0x1f, 0xe4, 
  0x39, 0x87, 0xf8, 0x00, 0x04, 0x39, 0x00, 0x00, 0x00, 0x0c, 0x39, 0x30, 0x00, 0x07, 0xfe, 0x39, 
  0x3f, 0xe0, 0x03, 0xfe, 0x7c, 0x3f, 0xc0, 0x03, 0xfe, 0x7c, 0x7f, 0x80, 0x01, 0xff, 0xfc, 0x7f, 
  0x80, 0x00, 0xff, 0xfc, 0x7f, 0x00, 0x00, 0x7f, 0xfc, 0xfe, 0x00, 0x00, 0x3f, 0xff, 0xfc, 0x00, 
  0x00, 0x1f, 0xff, 0xf8, 0x00, 0x00, 0x0f, 0xff, 0xf0, 0x00, 0x00, 0x07, 0xff, 0xe0, 0x00, 0x00, 
  0x03, 0xff, 0x80, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00, 
  0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'clock', 40x40px
const unsigned char epd_bitmap_clock [] PROGMEM = {
  0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xf0, 0x00, 0x00, 0x3c, 0x00, 0x3c, 0x00, 0x00, 
  0x70, 0x00, 0x0e, 0x00, 0x01, 0xc0, 0x18, 0x03, 0x80, 0x03, 0x80, 0x18, 0x01, 0xc0, 0x07, 0x00, 
  0x18, 0x00, 0xe0, 0x0e, 0x00, 0x18, 0x00, 0x70, 0x0c, 0x00, 0x18, 0x00, 0x30, 0x18, 0x00, 0x18, 
  0x00, 0x18, 0x30, 0x00, 0x18, 0x00, 0x0c, 0x30, 0x00, 0x18, 0x00, 0x0c, 0x60, 0x00, 0x18, 0x00, 
  0x06, 0x60, 0x00, 0x18, 0x00, 0x06, 0x40, 0x00, 0x18, 0x00, 0x02, 0x40, 0x00, 0x18, 0x00, 0x02, 
  0xc0, 0x00, 0x18, 0x00, 0x03, 0xc0, 0x00, 0x18, 0x00, 0x03, 0xc0, 0x00, 0x3c, 0x00, 0x03, 0xc0, 
  0x00, 0x3c, 0x00, 0x03, 0xc0, 0x00, 0x3c, 0x00, 0x03, 0xc0, 0x00, 0x7c, 0x00, 0x03, 0xc0, 0x00, 
  0xe0, 0x00, 0x03, 0xc0, 0x01, 0xc0, 0x00, 0x03, 0x40, 0x03, 0x80, 0x00, 0x02, 0x40, 0x07, 0x00, 
  0x00, 0x02, 0x60, 0x06, 0x00, 0x00, 0x06, 0x60, 0x00, 0x00, 0x00, 0x06, 0x30, 0x00, 0x00, 0x00, 
  0x0c, 0x30, 0x00, 0x00, 0x00, 0x0c, 0x18, 0x00, 0x00, 0x00, 0x18, 0x0c, 0x00, 0x00, 0x00, 0x30, 
  0x0e, 0x00, 0x00, 0x00, 0x70, 0x07, 0x00, 0x00, 0x00, 0xe0, 0x03, 0x80, 0x00, 0x01, 0xc0, 0x01, 
  0xc0, 0x00, 0x03, 0x80, 0x00, 0x70, 0x00, 0x0e, 0x00, 0x00, 0x3c, 0x00, 0x3c, 0x00, 0x00, 0x0f, 
  0xff, 0xf0, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00
};
// 'Games', 40x30px
const unsigned char epd_bitmap_Games [] PROGMEM = {
  0x00, 0x0f, 0xff, 0xf0, 0x00, 0x00, 0x7f, 0xff, 0xfe, 0x00, 0x01, 0xff, 0xff, 0xff, 0x80, 0x03, 
  0xfc, 0x00, 0x1f, 0xc0, 0x07, 0xc0, 0x00, 0x03, 0xe0, 0x07, 0x00, 0x00, 0x00, 0xe0, 0x0e, 0x00, 
  0x00, 0x00, 0x70, 0x1c, 0x10, 0x00, 0x08, 0x38, 0x1c, 0x38, 0x00, 0x1c, 0x38, 0x38, 0x38, 0x00, 
  0x18, 0x1c, 0x38, 0x78, 0x00, 0x00, 0x1c, 0x39, 0xfe, 0x00, 0x63, 0x1c, 0x71, 0xfe, 0x00, 0xe3, 
  0x8e, 0x70, 0xfe, 0x00, 0x41, 0x0e, 0x60, 0x38, 0x00, 0x08, 0x06, 0x60, 0x38, 0x00, 0x1c, 0x06, 
  0xe0, 0x30, 0x00, 0x1c, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x3c, 0x00, 0x07, 0xe0, 
  0x01, 0xff, 0x80, 0x07, 0xc0, 0x07, 0xff, 0xe0, 0x03, 0xc0, 0x0f, 0xff, 0xf0, 0x07, 0xe0, 0x0f, 
  0x00, 0xf8, 0x07, 0xe0, 0x1c, 0x00, 0x38, 0x07, 0xe0, 0x3c, 0x00, 0x3c, 0x07, 0x70, 0x78, 0x00, 
  0x1e, 0x0e, 0x71, 0xf0, 0x00, 0x0f, 0x8e, 0x3f, 0xe0, 0x00, 0x07, 0xfc, 0x3f, 0xc0, 0x00, 0x03, 
  0xfc, 0x1f, 0x00, 0x00, 0x00, 0xf8
};
// 'networking', 40x40px
const unsigned char epd_bitmap_networking [] PROGMEM = {
  0x00, 0x01, 0xff, 0x80, 0x00, 0x00, 0x03, 0xff, 0xc0, 0x00, 0x00, 0x03, 0xff, 0xc0, 0x00, 0x00, 
  0x03, 0xff, 0xc0, 0x00, 0x00, 0x03, 0x81, 0xc0, 0x00, 0x00, 0x03, 0x81, 0xc0, 0x00, 0x00, 0x03, 
  0x81, 0xc0, 0x00, 0x00, 0x03, 0x81, 0xc0, 0x00, 0x00, 0x03, 0x81, 0xc0, 0x00, 0x00, 0x03, 0x81, 
  0xc0, 0x00, 0x00, 0x03, 0x81, 0xc0, 0x00, 0x00, 0x03, 0xff, 0xc0, 0x00, 0x00, 0x03, 0xff, 0xc0, 
  0x00, 0x00, 0x03, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 
  0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xe0, 0x07, 
  0xff, 0xff, 0xff, 0xe0, 0x07, 0xff, 0xff, 0xff, 0xe0, 0x07, 0x00, 0x3c, 0x00, 0xe0, 0x06, 0x00, 
  0x18, 0x00, 0x60, 0x06, 0x00, 0x18, 0x00, 0x60, 0x06, 0x00, 0x18, 0x00, 0x60, 0x06, 0x00, 0x3c, 
  0x00, 0x60, 0xff, 0xf3, 0xff, 0xcf, 0xff, 0xff, 0xf3, 0xff, 0xcf, 0xff, 0xff, 0xf3, 0xff, 0xcf, 
  0xff, 0xe0, 0x73, 0x81, 0xce, 0x07, 0xe0, 0x73, 0x81, 0xce, 0x07, 0xe0, 0x73, 0x81, 0xce, 0x07, 
  0xe0, 0x73, 0x81, 0xce, 0x07, 0xe0, 0x73, 0x81, 0xce, 0x07, 0xe0, 0x73, 0x81, 0xce, 0x07, 0xe0, 
  0x73, 0x81, 0xce, 0x07, 0xe0, 0x73, 0x81, 0xce, 0x07, 0xff, 0xf3, 0xff, 0xcf, 0xff, 0xff, 0xf3, 
  0xff, 0xcf, 0xff, 0x7f, 0xf3, 0xff, 0xcf, 0xfe
};

// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 1744)
const int epd_bitmap_allArray_LEN = 8;
const unsigned char* epd_bitmap_allArray[8] = {
  epd_bitmap_GPS,
  epd_bitmap_Games,
  epd_bitmap_Health,
  epd_bitmap_Other,
  epd_bitmap_Settings,
  epd_bitmap_Utility,
  epd_bitmap_clock,
  epd_bitmap_networking
};
/*--------------------------------------------------------------------------------------------------*/
String nfm = ""; // days to next full moon
/*--------------------------------------------------------------------------------------------------*/
void setup()
{
  Serial.begin(115200);
  Wire.begin();
  rtc1.begin();
  fact = 180 / 3.14159265358979323846264338327950;
  Xoff = 90; Yoff = 32; Zoff = 750;   
  Lines[0].p0.x=-50; Lines[0].p0.y=-50; Lines[0].p0.z=50; Lines[0].p1.x=50; Lines[0].p1.y=-50; Lines[0].p1.z=50;
  Lines[1].p0.x=50; Lines[1].p0.y=-50;  Lines[1].p0.z=50; Lines[1].p1.x=50; Lines[1].p1.y=50; Lines[1].p1.z=50;
  Lines[2].p0.x=50; Lines[2].p0.y=50; Lines[2].p0.z=50; Lines[2].p1.x=-50; Lines[2].p1.y=50; Lines[2].p1.z=50;
  Lines[3].p0.x=-50; Lines[3].p0.y=50; Lines[3].p0.z=50; Lines[3].p1.x=-50; Lines[3].p1.y=-50; Lines[3].p1.z=50;
  Lines[4].p0.x=-50; Lines[4].p0.y=-50; Lines[4].p0.z=-50; Lines[4].p1.x=50; Lines[4].p1.y=-50; Lines[4].p1.z=-50;
  Lines[5].p0.x=50; Lines[5].p0.y=-50; Lines[5].p0.z=-50; Lines[5].p1.x=50; Lines[5].p1.y=50; Lines[5].p1.z=-50;
  Lines[6].p0.x=50; Lines[6].p0.y=50; Lines[6].p0.z=-50; Lines[6].p1.x=-50; Lines[6].p1.y=50; Lines[6].p1.z=-50;
  Lines[7].p0.x=-50; Lines[7].p0.y=50; Lines[7].p0.z=-50; Lines[7].p1.x=-50; Lines[7].p1.y=-50; Lines[7].p1.z=-50;
  Lines[8].p0.x=-50; Lines[8].p0.y=-50; Lines[8].p0.z=50; Lines[8].p1.x=-50; Lines[8].p1.y=-50; Lines[8].p1.z=-50;
  Lines[9].p0.x=50; Lines[9].p0.y=-50; Lines[9].p0.z=50; Lines[9].p1.x=50; Lines[9].p1.y=-50; Lines[9].p1.z=-50;
  Lines[10].p0.x=-50; Lines[10].p0.y=50; Lines[10].p0.z=50; Lines[10].p1.x=-50; Lines[10].p1.y=50; Lines[10].p1.z=-50;
  Lines[11].p0.x=50; Lines[11].p0.y=50; Lines[11].p0.z=50; Lines[11].p1.x=50; Lines[11].p1.y=50; Lines[11].p1.z=-50; 
  LinestoRender=12; OldLinestoRender=LinestoRender;
  //pinMode(18, OUTPUT); 
  pinMode(2, INPUT); pinMode(3, INPUT); 
  button1.setDebounceTime(60); button2.setDebounceTime(60); button3.setDebounceTime(60); button4.setDebounceTime(60); button5.setDebounceTime(60);
  Wire.beginTransmission(MPU); Wire.write(0x6B); Wire.write(0); Wire.endTransmission(true); 
  ss.begin(9600, SERIAL_8N1, RXD1, TXD1);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display1.begin(SSD1306_SWITCHCAPVCC, 0x3D);
  display.setTextSize(3);display.setTextColor(WHITE);display.setCursor(1,0);
  display1.setTextSize(3);display1.setTextColor(WHITE);display1.setCursor(1,0);
  display.clearDisplay(); display1.clearDisplay();
  display.setRotation(2);
  display.print("Welcome"); 
  display.print(" Back!\n");
  display.setTextSize(1);display.setTextColor(WHITE);display.setCursor(24,54);
  display.display();    
  display1.drawBitmap(18, -14, Logo, 128, 96, WHITE);
  display1.display(); 
  delay(2000); 
  display.clearDisplay();
  display.display(); 
  display1.clearDisplay();
  display1.display(); 
}
void loop() {
/*------------------------------------GUI---------------------------------------------------*/
  while (enter == 0){
      setCpuFrequencyMhz(cpuspeed);cpuFreq = getCpuFrequencyMhz(); sleeptimemenu = setSleep;
      /*buzzer.loop();*/ button1.loop(); button2.loop(); button3.loop(); button4.loop(); button5.loop(); 
      if(button1.isReleased()){/*buzzer.beep(100);*/ enter = 1; sleepsecM = 0;}
      if(button3.isReleased()){/*buzzer.beep(100);*/ stateM = stateM - 1; sleepsecM = 0;} 
      if(button4.isReleased()){/*buzzer.beep(100);*/ stateM = stateM + 1; sleepsecM = 0;}
      if(stateM > 7){stateM = 0;} if(stateM < 0){stateM = 7;}
      if(stateM == 0 || stateM == 1){substateM = 0;} if(stateM == 2 || stateM == 3){substateM = 1;} if(stateM == 4 || stateM == 5){substateM = 2;} if(stateM == 6 || stateM == 7){substateM = 3;} 
      /*****************************************************************************************************************************************************/
      sleepermenu(); batterylevel(); gpsstatus();
      display1.setTextSize(1); display1.setCursor(0,4); display1.setTextColor(WHITE); display1.clearDisplay();
      display1.print("M.\n"); EnableWifi(); /*EnableBluetooth();*/ display1.drawBitmap(47, 0, bitGps, 16, 16, WHITE); display1.drawBitmap(87, 0, bitBattery, 16, 16, WHITE);
      display1.setTextSize(1); display1.setCursor(60,4); display1.setTextColor(WHITE); display1.print(ssValid + timeValid + dateValid + satValid + locValid); display1.print("%\n");
      display1.setTextSize(1); display1.setCursor(104,4); display1.setTextColor(WHITE); display1.print(totalPP);display1.print("%\n");
      display1.drawLine(0, 16, 128, 16, WHITE); 
      /*****************************************************************************************************************************************************/
      if(button2.isPressed()){display1.fillTriangle(10, 20, 1, 28, 10, 36, WHITE);} //L1
      else{display1.drawTriangle(10, 20, 1, 28, 10, 36, WHITE);} 
      if(button5.isPressed()){display1.fillTriangle(118, 20, 127, 28, 118, 36, WHITE);} //R1
      else{display1.drawTriangle(118, 20, 127, 28, 118, 36, WHITE);}
      if(button3.isPressed()){display1.fillTriangle(10, 42, 1, 50, 10, 58, WHITE);} //L2
      else{display1.drawTriangle(10, 42, 1, 50, 10, 58, WHITE);}
      if(button4.isPressed()){display1.fillTriangle(118, 42, 127, 50, 118, 58, WHITE);} //R2
      else{display1.drawTriangle(118, 42, 127, 50, 118, 58, WHITE);}
      /*****************************************************************************************************************************************************/
      display1.setTextColor(WHITE); 
      display1.setTextSize(2);
      switch (substateM){
         case 0:
          display1.drawBitmap(18, 20, epd_bitmap_clock, 40, 40, WHITE);
          display1.drawBitmap(70, 20, epd_bitmap_GPS, 40, 40, WHITE);
          display1.display(); 
          break;
         case 1:
          display1.drawBitmap(18, 20, epd_bitmap_Health, 40, 40, WHITE);
          display1.drawBitmap(70, 26, epd_bitmap_Games, 40, 30, WHITE);
          display1.display(); 
          break;
         case 2:
          display1.drawBitmap(18, 20, epd_bitmap_Utility, 40, 40, WHITE);
          display1.drawBitmap(70, 20, epd_bitmap_Other, 40, 40, WHITE);
          display1.display(); 
          break;
         case 3:
          display1.drawBitmap(18, 20, epd_bitmap_networking, 40, 40, WHITE);
          display1.drawBitmap(70, 20, epd_bitmap_Settings, 40, 40, WHITE);
          display1.display(); 
          break;
      }
      /*****************************************************************************************************************************************************/
      switch (stateM){
         case 0: display1.drawRect(16, 18, 44, 44, WHITE); display1.display(); break; // Time
         case 1: display1.drawRect(68, 18, 44, 44, WHITE); display1.display(); break; // GPS
         case 2: display1.drawRect(16, 18, 44, 44, WHITE); display1.display(); break; // Health
         case 3: display1.drawRect(68, 18, 44, 44, WHITE); display1.display(); break; // Games
         case 4: display1.drawRect(16, 18, 44, 44, WHITE); display1.display(); break; // Utility
         case 5: display1.drawRect(68, 18, 44, 44, WHITE); display1.display(); break; // Other
         case 6: display1.drawRect(16, 18, 44, 44, WHITE); display1.display(); break; // Network
         case 7: display1.drawRect(68, 18, 44, 44, WHITE); display1.display(); break; // Settings
       }
/*-------------------------------------WatchTime----------------------------------------------------*/
       if(stateM  == 0){
          setCpuFrequencyMhz(cpuspeed);cpuFreq = getCpuFrequencyMhz(); sleeptimemenu = setSleep;
          if(button2.isReleased()){/*buzzer.beep(100);*/ settime = settime - 1; sleepsecM = 0;} 
          if(button5.isReleased()){/*buzzer.beep(100);*/ settime = settime + 1; sleepsecM = 0;}
          if(settime > 5){settime = 0;} if(settime < 0){settime = 5;}
          /*****************************************************************************************************************************************************/
          display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
          display.print(totalVV);display.print("V");
          display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
          display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
          display.drawLine(0, 16, 128, 16, WHITE);
          /*****************************************************************************************************************************************************/
          DateTime now1 = rtc1.now(); // From the RTC module to the structure
          display.setCursor(0,22);display.setTextSize(3);display.setTextColor(WHITE);
          switch (settime){
             case 0:
                display.print(now1.hour());display.print(":");
                display.setTextSize(2);display.print(now1.minute());display.print(":");
                display.print(now1.second());display.print("\n");
                display.setTextSize(3);display.print("-------");display.print("\n");
                display.display(); 
                break;
             case 1: 
                display.setTextSize(1); display.setTextColor(WHITE); display.clearDisplay();
                if (now1.second()!= Seconds){
                  draw_second(Seconds,0);
                  draw_minute(Minutes,0);
                  draw_hour(Hours,Minutes,0);
                  draw_second(now1.second(),1);
                  draw_minute(now1.minute(),1);
                  draw_hour(now1.hour(),now1.minute(),1);
                  Seconds=now1.second();
                  Minutes=now1.minute();
                  Hours=now1.hour();
                  draw_clock_face();
                  display.display(); 
                 }
                 break;
              case 2:
                 display.setTextSize(2); 
                 display.print(now1.month()); display.print("-"); display.print(now1.day()); display.print("-"); display.print(now1.year()); display.print("\n");
                 weekoftheday = weekday(now1.year(),now1.month(),now1.day());
                 switch(weekoftheday){
                   case 0:display.println("Sunday");break;
                   case 1:display.println("Monday");break;
                   case 2:display.println("Tuesday");break;
                   case 3:display.println("Wednesday");break;
                   case 4:display.println("Thursday");break;
                   case 5:display.println("Friday");break;
                   case 6:display.println("Saturday");break;
                 }
                 display.display(); 
                 break;
              case 3:
                 drawCalendar(); 
                 display.display(); 
                 break;
              case 4:
                 drawMoon();
                 display.display(); 
                 break;
              case 5: 
                 display.setTextSize(2);display.print("   SYNC\n");
                 display.print(" GPS->RTC");display.print("\n");
                 display.setTextSize(3);display.print("-------");
                 display.display(); 
                 break;
           }
           /*****************************************************************************************************************************************************/
           if (enter == 1 && settime == 5 && (ssValid == 20 || ss.available() > 0)){
              for(int i = 0; i <= 1001; i++){
                gps.encode(ss.read());
                if (gps.time.isValid()){Minute = gps.time.minute(); Second = gps.time.second(); Hour = gps.time.hour();}
                if (gps.date.isValid()){Day = gps.date.day(); Month = gps.date.month(); Year = gps.date.year();}
              }
              setTime(Hour, Minute, Second, Day, Month, Year);adjustTime(time_offset);  //From GPS to the the temp-libary where it will be offset and then from temp-libary to the RTC module
              Seconds = second(); Minutes = minute(); Hours = hour(); Days = day(); Months = month(); Years= year();
              rtc1.adjust(DateTime(Years, Months, Days, Hours, Minutes, Second));
              display.setTextSize(3);display.setTextColor(WHITE);display.setCursor(1,0);
              display.clearDisplay();
              display.print("-------");
              display.print("SYNCED!"); 
              display.print("-------");
              display.display(); delay(2000); 
              display.clearDisplay();
              display.display(); 
              enter = 0;
              settime = 0;
            }
            /*****************************************************************************************************************************************************/
            if (enter == 1 && settime == 5 && (ssValid == 0 || ss.available() <= 0)){
              display.setTextSize(3);display.setTextColor(WHITE);display.setCursor(1,0);
              display.clearDisplay();
              display.print("-------");
              display.print("No GPS?"); 
              display.print("-------");
              display.display(); delay(2000); 
              display.clearDisplay();
              display.display(); 
              enter = 0;
              settime = 0;
            }
       }
/*------------------------------------GUI(GPS)---------------------------------------------------*/
      if(stateM  == 1){
         setCpuFrequencyMhz(cpuspeed);cpuFreq = getCpuFrequencyMhz();
         sleeptimemenu = setSleep;
         if(button2.isReleased()){/*buzzer.beep(100);*/ gpsstate = gpsstate - 1;sleepsecM = 0;}
         if(button5.isReleased()){/*buzzer.beep(100);*/ gpsstate = gpsstate + 1;sleepsecM = 0;}
         if(gpsstate > 5){gpsstate = 0;} if(gpsstate < 0){gpsstate = 5;}
         if(gpsstate >= 0 && gpsstate <= 2){subgpsstate = 0;} if(gpsstate >= 3 && gpsstate <= 5){subgpsstate = 1;}
         /*****************************************************************************************************************************************************/
         display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
         display.print(totalVV);display.print("V");
         display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
         display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
         display.drawLine(0, 16, 128, 16, WHITE);
        /*****************************************************************************************************************************************************/
         display.setCursor(0,16);
         display.setTextSize(2);
         switch (subgpsstate){
            case 0:
               display.print("1.Locat.  \n");
               display.print("2.Sp/Alt. \n");  
               display.print("3.Degrees \n"); 
               break;
            case 1:
               display.print("4.GPS T/D \n");
               display.print("5.Distance\n");
               display.print("6.Ruler   \n");
               break;
         }
         /*****************************************************************************************************************************************************/
         switch (gpsstate){
             case 0: display.drawRect(0, 16, 128, 15, WHITE); display.display(); break;
             case 1: display.drawRect(0, 32, 128, 15, WHITE); display.display(); break;
             case 2: display.drawRect(0, 48, 128, 15, WHITE); display.display(); break;
             case 3: display.drawRect(0, 16, 128, 15, WHITE); display.display(); break;
             case 4: display.drawRect(0, 32, 128, 15, WHITE); display.display(); break;
             case 5: display.drawRect(0, 48, 128, 15, WHITE); display.display(); break;
           }
      }
/*------------------------------------GUI(Health)---------------------------------------------------*/
      if(stateM  == 2){
         setCpuFrequencyMhz(cpuspeed);cpuFreq = getCpuFrequencyMhz();
         sleeptimemenu = setSleep;
         if(button2.isReleased()){/*buzzer.beep(100);*/ healthstate =  healthstate - 1;sleepsecM = 0;}
         if(button5.isReleased()){/*buzzer.beep(100);*/ healthstate =  healthstate + 1;sleepsecM = 0;}
         if(healthstate > 1){healthstate = 0;} if(healthstate < 0){healthstate = 1;}
         if(healthstate >= 0 && healthstate <= 2){subhealthstate = 0;} 
         /*****************************************************************************************************************************************************/
         display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
         display.print(totalVV);display.print("V");
         display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
         display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
         display.drawLine(0, 16, 128, 16, WHITE);
         /*****************************************************************************************************************************************************/
         display.setCursor(0,16);
         display.setTextSize(2);
         display.print("1.HeartRa.\n");  
         display.print("2.PedoMet.\n"); 
         /*****************************************************************************************************************************************************/
         switch (healthstate){
           case 0: display.drawRect(0, 16, 128, 15, WHITE); display.display(); break;
           case 1: display.drawRect(0, 32, 128, 15, WHITE); display.display(); break;
         }
      }
/*------------------------------------GUI(Games)---------------------------------------------------*/      
      if(stateM  == 3){
         setCpuFrequencyMhz(cpuspeed);cpuFreq = getCpuFrequencyMhz();
         sleeptimemenu = setSleep;
         if(button2.isReleased()){/*buzzer.beep(100);*/ gamessstate = gamessstate - 1;sleepsecM = 0;}
         if(button5.isReleased()){/*buzzer.beep(100);*/ gamessstate = gamessstate + 1;sleepsecM = 0;}
         if(gamessstate > 1){gamessstate = 0;} if(gamessstate < 0){gamessstate = 1;}
         /*****************************************************************************************************************************************************/
         display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
         display.print(totalVV);display.print("V");
         display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
         display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
         display.drawLine(0, 16, 128, 16, WHITE);
        /*****************************************************************************************************************************************************/
         display.setCursor(0,16);
         display.setTextSize(2);
         display.print("1.Tetris\n");  
         display.print("2.Pong  \n"); 
         /*****************************************************************************************************************************************************/
         switch (gamessstate){
            case 0: display.drawRect(0, 16, 128, 15, WHITE); display.display(); break;
            case 1: display.drawRect(0, 32, 128, 15, WHITE); display.display(); break;
          } 
      }
/*------------------------------------GUI(Utility)---------------------------------------------------*/   
      if(stateM  == 4){
         setCpuFrequencyMhz(cpuspeed);cpuFreq = getCpuFrequencyMhz();
         sleeptimemenu = setSleep;
         if(button2.isReleased()){/*buzzer.beep(100);*/ utilitystate = utilitystate - 1;sleepsecM = 0;}
         if(button5.isReleased()){/*buzzer.beep(100);*/ utilitystate = utilitystate + 1;sleepsecM = 0;}
         if(utilitystate > 2){utilitystate = 0;} if(utilitystate < 0){utilitystate = 2;}
         /*****************************************************************************************************************************************************/
         display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
         display.print(totalVV);display.print("V");
         display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
         display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
         display.drawLine(0, 16, 128, 16, WHITE);
         /*****************************************************************************************************************************************************/
         display.setCursor(0,16);
         display.setTextSize(2);
         display.print("1.StopW.\n");  
         display.print("2.Timer \n"); 
         display.print("3.Calc. \n"); 
         /*****************************************************************************************************************************************************/
         switch (utilitystate){
            case 0: display.drawRect(0, 16, 128, 15, WHITE); display.display(); break;
            case 1: display.drawRect(0, 32, 128, 15, WHITE); display.display(); break;
            case 2: display.drawRect(0, 48, 128, 15, WHITE); display.display(); break;
         }
      }
/*------------------------------------GUI(Other)---------------------------------------------------*/
      if(stateM  == 5){
         setCpuFrequencyMhz(cpuspeed);cpuFreq = getCpuFrequencyMhz();
         sleeptimemenu = setSleep;
         if(button2.isReleased()){/*buzzer.beep(100);*/ otherstate =  otherstate - 1;sleepsecM = 0;}
         if(button5.isReleased()){/*buzzer.beep(100);*/ otherstate =  otherstate + 1;sleepsecM = 0;}
         if(otherstate > 1){otherstate = 0;} if(otherstate < 0){otherstate = 1;}
         /*****************************************************************************************************************************************************/
         display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
         display.print(totalVV);display.print("V");
         display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
         display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
         display.drawLine(0, 16, 128, 16, WHITE);
         /*****************************************************************************************************************************************************/
         display.setCursor(0,16);
         display.setTextSize(2);
         display.print("1.Tempert \n");
         display.print("2.Acce/Gy \n");  
         /*****************************************************************************************************************************************************/
         switch (otherstate){
            case 0: display.drawRect(0, 16, 128, 15, WHITE); display.display(); break;
            case 1: display.drawRect(0, 32, 128, 15, WHITE); display.display(); break;
         }
      }
/*------------------------------------GUI(Networking)---------------------------------------------------*/
      if(stateM  == 6){
         setCpuFrequencyMhz(cpuspeed);cpuFreq = getCpuFrequencyMhz();
         sleeptimemenu = setSleep;
         //if(button5.isReleased()){/*buzzer.beep(100);*/ networkstate =  networkstate - 1;sleepsecM = 0;}
         //if(button4.isReleased()){/*buzzer.beep(100);*/ networkstate =  networkstate + 1;sleepsecM = 0;}
         //if(button2.isReleased()){/*buzzer.beep(100);*/ layer = 0;sleepsecM = 0;}
         //if(button1.isReleased()){/*buzzer.beep(100);*/ shutoff = shutoff + 1;sleepsecM = 0;}
         //if(button1.isReleased() && shutoff > 1){/*buzzer.beep(100);*/ shutoff = 0;sleepsecM = 0;}
         //if(networkstate > 1){networkstate = 0;} if(networkstate < 0){networkstate = 1;}
         /*****************************************************************************************************************************************************/
         display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
         display.print(totalVV);display.print("V");
         display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
         display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
         display.drawLine(0, 16, 128, 16, WHITE);
         /*****************************************************************************************************************************************************/
         display.drawLine(0, 16, 128, 16, WHITE);
         display.setCursor(0,16);
         display.setTextSize(2);
         display.print("1.Wifi Sc.\n");
         //display.print("2.Ble Sc. \n");  
         /*****************************************************************************************************************************************************/
         switch (networkstate){
            case 0: display.drawRect(0, 16, 128, 15, WHITE); display.display(); break;
         // case 1: display.drawRect(0, 32, 128, 15, WHITE); display.display(); break;
         }
      } 
/*------------------------------------GUI(Settings)---------------------------------------------------*/
      if(stateM  == 7){
         setCpuFrequencyMhz(cpuspeed);cpuFreq = getCpuFrequencyMhz();
         sleeptimemenu = setSleep;
         if(button2.isReleased()){/*buzzer.beep(100);*/ settingsstate =  settingsstate - 1;sleepsecM = 0;}
         if(button5.isReleased()){/*buzzer.beep(100);*/ settingsstate =  settingsstate + 1;sleepsecM = 0;}
         if(settingsstate > 4){settingsstate = 0;} if(settingsstate < 0){settingsstate = 4;}
         if(settingsstate >= 0 && settingsstate <= 2){subsettingsstate = 0;} if(settingsstate >= 3 && settingsstate <= 5){subsettingsstate = 1;} 
         /*****************************************************************************************************************************************************/
         display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
         display.print(totalVV);display.print("V");
         display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
         display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
         display.drawLine(0, 16, 128, 16, WHITE);
         /*****************************************************************************************************************************************************/
         switch (Ewifi){
             case 0: WifiYN = "Off"; break; //10 
             case 1: WifiYN = "On"; break; //20
         }
        /* switch (Ebluetooth){
             case 0: BltYN = "Off";break; //10 
             case 1: BltYN = "On"; break; //20
         } */
         /*****************************************************************************************************************************************************/
         display.setCursor(0,16);
         display.setTextSize(2);
         switch (subsettingsstate){
            case 0:
              display.print("1.CPUFreq \n");
              display.print("2.SleepT  \n");  
              display.print("3.Wifi:" + WifiYN + "\n");
              break;
            case 1:
              //display.print("4.Blue:" + BltYN + "\n");
              display.print("4.Help    \n");  
              display.print("5.Firmware\n");
              break;
          }
         /*****************************************************************************************************************************************************/
         switch (settingsstate){
            case 0: display.drawRect(0, 16, 128, 15, WHITE); display.display(); break;
            case 1: display.drawRect(0, 32, 128, 15, WHITE); display.display(); break;
            case 2: display.drawRect(0, 48, 128, 15, WHITE); display.display(); break;
            case 3: display.drawRect(0, 16, 128, 15, WHITE); display.display(); break;
            case 4: display.drawRect(0, 32, 128, 15, WHITE); display.display(); break;
            //case 5: display.drawRect(0, 48, 128, 15, WHITE); display.display(); break;
         }
      }   
}
/*****************************************************************************************************************************************************/
if (enter == 1){display.clearDisplay();  display1.clearDisplay(); display.display(); display1.display();}
if (enter == 1 && stateM  == 0){enter = 0;}
if (enter == 1 && settingsstate == 2 && stateM == 7 && Ewifi == 0){Ewifi = 1;enter = 0;}
if (enter == 1 && settingsstate == 2 && stateM == 7 && Ewifi == 1){Ewifi = 0;enter = 0;}
//if (enter == 1 && settingsstate == 3 && stateM == 7 && Ebluetooth == 0){Ebluetooth = 1;enter = 0;}
//if (enter == 1 && settingsstate == 3 && stateM == 7 && Ebluetooth == 1){Ebluetooth = 0;enter = 0;}
/*****************************************************************************************************************************************************/
/*------------------------------------Loca./Sat.---------------------------------------------------*/
  while(enter == 1 && gpsstate == 0 && stateM == 1){
    if(ss.available() > 0){
        /*buzzer.loop();*/ button1.loop();
        if(button1.isReleased()){/*buzzer.beep(100);*/ enter = 0;}
        /*****************************************************************************************************************************************************/
        display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
        display.print(totalVV);display.print("V");
        display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
        display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
        display.drawLine(0, 16, 128, 16, WHITE);
        /*****************************************************************************************************************************************************/
         batterylevel(); gpsstatus();
        display1.setTextSize(1); display1.setCursor(0,4); display1.setTextColor(WHITE); display1.clearDisplay();
        display1.print("1.\n"); EnableWifi(); /*EnableBluetooth();*/ display1.drawBitmap(47, 0, bitGps, 16, 16, WHITE); display1.drawBitmap(87, 0, bitBattery, 16, 16, WHITE);
        display1.setTextSize(1); display1.setCursor(60,4); display1.setTextColor(WHITE); display1.print(ssValid + timeValid + dateValid + satValid + locValid); display1.print("%\n");
        display1.setTextSize(1); display1.setCursor(104,4); display1.setTextColor(WHITE); display1.print(totalPP);display1.print("%\n");
        display1.drawLine(0, 16, 128, 16, WHITE);
        /*****************************************************************************************************************************************************/  
        gps.encode(ss.read());
        if(gps.location.isValid() == 1){
          currsat = gps.satellites.value();
          curhdop = gps.hdop.value();
          currlat = gps.location.lat();
          currlon = gps.location.lng();
        }
        /*****************************************************************************************************************************************************/
        display.setCursor(0,16); display.setTextSize(2); display.setTextColor(WHITE); 
        display.print("Sats: "); 
        display.print(currsat);
        display.print("\n"); 
        display.print("Hdop: "); 
        display.println(curhdop);
        display.display(); 
        /*****************************************************************************************************************************************************/
        display1.setCursor(0,16); display1.setTextSize(2);display1.setTextColor(WHITE); 
        display1.print("Lat/Long:\n"); 
        display1.print(currlat, 6);
        display1.print("\n"); 
        display1.print(currlon, 6);
        display1.display(); 
     }
     /*****************************************************************************************************************************************************/
     else if(ss.available() <= 0){
       display.setTextSize(3);display.setTextColor(WHITE);display.setCursor(1,0);
       display.clearDisplay();
       display.print("-------");
       display.print("No GPS?"); 
       display.print("-------");
       display.display(); delay(2000); 
       display.clearDisplay(); display.display(); 
       enter = 0;
     }
   }
/*------------------------------------Speed/Alt.---------------------------------------------------*/
  while (enter == 1 && gpsstate == 1  && stateM == 1){
    if(ss.available() > 0){
        /*buzzer.loop();*/ button1.loop(); button2.loop(); button3.loop(); button4.loop(); button5.loop(); 
        if(button2.isReleased()){/*buzzer.beep(100);*/ choice = choice - 1;} 
        if(button5.isReleased()){/*buzzer.beep(100);*/ choice = choice + 1;}
        if(button1.isReleased()){/*buzzer.beep(100);*/ enter = 0;}
        if(choice > 1){choice = 0;} if(choice < 0){choice = 1;}
        /*****************************************************************************************************************************************************/
        display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
        display.print(totalVV);display.print("V");
        display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
        display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
        display.drawLine(0, 16, 128, 16, WHITE);
        /*****************************************************************************************************************************************************/
        batterylevel(); gpsstatus();
        display1.setTextSize(1); display1.setCursor(0,4); display1.setTextColor(WHITE); display1.clearDisplay();
        display1.print("1.\n"); EnableWifi(); /*EnableBluetooth();*/ display1.drawBitmap(47, 0, bitGps, 16, 16, WHITE); display1.drawBitmap(87, 0, bitBattery, 16, 16, WHITE);
        display1.setTextSize(1); display1.setCursor(60,4); display1.setTextColor(WHITE); display1.print(ssValid + timeValid + dateValid + satValid + locValid); display1.print("%\n");
        display1.setTextSize(1); display1.setCursor(104,4); display1.setTextColor(WHITE); display1.print(totalPP);display1.print("%\n");
        display1.drawLine(0, 16, 128, 16, WHITE);
        /*****************************************************************************************************************************************************/  
        gps.encode(ss.read());
        if(choice == 0 && gps.location.isValid() == 1){
          curralt = gps.altitude.miles();
          currspeed = gps.speed.mph();
          curralt1 = gps.altitude.feet();
          currspeed1 = gps.speed.mph()/3600;
        }
        if(choice == 1 && gps.location.isValid() == 1){
          curralt = gps.altitude.kilometers();
          currspeed = gps.speed.kmph();
          curralt1 = gps.altitude.meters();
          currspeed1 = gps.speed.knots();
        } 
        /*****************************************************************************************************************************************************/ 
        switch (choice){
           case 0:
              display.setCursor(0,16);
              display.setTextSize(2);display.print("Altitude:\n"); display.print(curralt);display.print("Mi"); 
              display.print("\n"); display.print(curralt1);display.print("Ft"); 
              display.display(); 
              display1.setTextSize(2);display1.setCursor(0,16);display1.print("Speed:\n"); display1.print(currspeed);display1.print("Mph"); 
              display1.print("\n"); display1.print(currspeed1);display1.print("Mps"); 
              display1.display(); 
              break;
            case 1:
              display.setCursor(0,16);
              display.setTextSize(2);display.print("Altitude:\n"); display.print(curralt);display.print("Km"); 
              display.print("\n"); display.print(curralt1);display.print("Me"); 
              display.display(); 
              display1.setTextSize(2);display1.setCursor(0,16);display1.print("Speed:\n"); display1.print(currspeed);display1.print("Kmph"); 
              display1.print("\n"); display1.print(currspeed1);display1.print("Knot"); 
              display1.display(); 
              break;
        }
      }
      /*****************************************************************************************************************************************************/
      else if(ss.available() <= 0){
        display.setTextSize(3);display.setTextColor(WHITE);display.setCursor(1,0);
        display.clearDisplay();
        display.print("-------");
        display.print("No GPS?"); 
        display.print("-------");
        display.display(); delay(2000); 
        display.clearDisplay(); display.display(); 
        enter = 0;
    }
  }
/*-------------------------------------Val./Dgrs.-----------------------------------------------------------*/
  while (enter == 1 && gpsstate == 2  && stateM == 1){
    if(ss.available() > 0){
        /*buzzer.loop();*/ button1.loop(); 
        if(button1.isReleased()){/*buzzer.beep(100);*/ enter = 0;}
        /*****************************************************************************************************************************************************/
        display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
        display.print(totalVV);display.print("V");
        display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
        display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
        display.drawLine(0, 16, 128, 16, WHITE);
        /*****************************************************************************************************************************************************/
        gps.encode(ss.read());  
        if(gps.location.isValid() == 1){
          currvalue = gps.course.value();
          degrees1 = gps.course.deg();
        }
        /*****************************************************************************************************************************************************/
        display.setTextSize(2); display.setCursor(0,18);
        display.print("Val/Deg.:\n"); display.print(currvalue);
        display.print("\n"); display.print(degrees1);
        display.display(); 
        visualcompass(degrees1);
      }
      /*****************************************************************************************************************************************************/
      else if(ss.available() <= 0){
        display.setTextSize(3);display.setTextColor(WHITE);display.setCursor(1,0);
        display.clearDisplay();
        display.print("-------");
        display.print("No GPS?"); 
        display.print("-------");
        display.display(); delay(2000); 
        display.clearDisplay(); display.display(); 
        enter = 0;
      }    
  }
/*-------------------------------------Time/Date-----------------------------------------------------------*/
  while (enter == 1 && gpsstate == 3  && stateM == 1){
        /*buzzer.loop();*/ button1.loop(); 
        if(button1.isReleased()){/*buzzer.beep(100);*/ enter = 0;}
        /*****************************************************************************************************************************************************/
        batterylevel(); gpsstatus();
        display1.setTextSize(1); display1.setCursor(0,4); display1.setTextColor(WHITE); display1.clearDisplay();
        display1.print("4.\n"); EnableWifi(); /*EnableBluetooth();*/ display1.drawBitmap(47, 0, bitGps, 16, 16, WHITE); display1.drawBitmap(87, 0, bitBattery, 16, 16, WHITE);
        display1.setTextSize(1); display1.setCursor(60,4); display1.setTextColor(WHITE); display1.print(ssValid + timeValid + dateValid + satValid + locValid); display1.print("%\n");
        display1.setTextSize(1); display1.setCursor(104,4); display1.setTextColor(WHITE); display1.print(totalPP); display1.print("%\n");
        display1.drawLine(0, 16, 128, 16, WHITE);
        /*****************************************************************************************************************************************************/
        gps.encode(ss.read());
        if (gps.time.isValid()){Minute = gps.time.minute(); Second = gps.time.second(); Hour = gps.time.hour();}
        if (gps.date.isValid()){Day = gps.date.day(); Month = gps.date.month(); Year = gps.date.year();}
        if(last_second != gps.time.second()) {
          last_second = gps.time.second();
          setTime(Hour, Minute, Second, Day, Month, Year);
          adjustTime(time_offset);
          display1.setTextSize(2); display1.setCursor(0,18);
          display1.print("Time/Date:\n");
          display1.print(String(hour()) +  ":" + String(minute()) + ":" + String(second()));         // print time (HH:MM:SS)
          display1.print("\n");
          display1.print(String(day()) +  "/" + String(month()) + "/" + String(year()));         // print date (DD-MM-YYYY) 
          display1.display();
         }
         /*****************************************************************************************************************************************************/
         display.setTextSize(1); display.setTextColor(WHITE); display.clearDisplay();
         if (second()!= Seconds){
            draw_second(Seconds,0);
            draw_minute(Minutes,0);
            draw_hour(Hours,Minutes,0);
            draw_second(second(),1);
            draw_minute(minute(),1);
            draw_hour(hour(),minute(),1);
            Seconds=second();
            Minutes=minute();
            Hours=hour();
            draw_clock_face();
            display.display(); 
         }
  }
/*------------------------------------Distance--------------------------------------------------------*/
  while (enter == 1 && gpsstate == 4  && stateM == 1){
    if(ss.available() > 0){
      bstate1 = digitalRead( bpress1 ); bstate3 = digitalRead( bpress3 ); bstate5 = digitalRead( bpress5 ); bstate2 = digitalRead( bpress2 ); bstate4 = digitalRead( bpress4 );
      if(bstate5 == LOW){/*tone(buzzer1, 100000);*/ choiceDC = choiceDC - 1; delay(100); /*noTone(buzzer1);*/ delay(50);}
      if(bstate4 == LOW){/*tone(buzzer1, 100000);*/ choiceDC = choiceDC + 1; delay(100); /*noTone(buzzer1);*/ delay(50);}
      if(bstate1 == LOW){/*tone(buzzer1, 100000);*/ phase = 0; stateDC = 0; choiceDC = 0; enter = 0; delay(100); /*noTone(buzzer1);*/ delay(50);}
      if(bstate2 == LOW || bstate3 == LOW){/*tone(buzzer1, 100000);*/ stateDC = 1; delay(100); /*noTone(buzzer1);*/ delay(50);}
      if(choiceDC > 1){choiceDC = 0;}
      if(choiceDC < 0){choiceDC = 1;}
      /*****************************************************************************************************************************************************/
      display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
      display.print(totalVV);display.print("V");
      display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
      display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
      display.drawLine(0, 16, 128, 16, WHITE);
      /*****************************************************************************************************************************************************/
      if (choiceDC == 0){
          batterylevel(); gpsstatus();
         display1.setTextSize(1); display1.setCursor(0,4); display1.setTextColor(WHITE); display1.clearDisplay();
         display1.print("1.\n"); EnableWifi(); /*EnableBluetooth();*/ display1.drawBitmap(47, 0, bitGps, 16, 16, WHITE); display1.drawBitmap(87, 0, bitBattery, 16, 16, WHITE);
         display1.setTextSize(1); display1.setCursor(60,4); display1.setTextColor(WHITE); display1.print(ssValid + timeValid + dateValid + satValid + locValid); display1.print("%\n");
         display1.setTextSize(1); display1.setCursor(104,4); display1.setTextColor(WHITE); display1.print(totalPP);display1.print("%\n");
         display1.drawLine(0, 16, 128, 16, WHITE); 
      }
      /*****************************************************************************************************************************************************/ 
      gps.encode(ss.read());
      if(gps.location.isValid() == 1){
          currsat = gps.satellites.value();
          curhdop = gps.hdop.value();
          currlat = gps.location.lat();
          currlon = gps.location.lng();
        }
      /*****************************************************************************************************************************************************/ 
      if(phase == 0){
        display.setTextSize(2); display.setCursor(0,18);
        display.print("Set point\n");
        display.print("   (A)\n\n");
        display.print(String(gps.speed.mph())+"Mph");
        display.display();
        display1.setCursor(0,16); display1.setTextSize(2);display1.setTextColor(WHITE); 
        display1.print("Lat/Long:\n"); 
        display1.print(currlat, 6);
        display1.print("\n"); 
        display1.print(currlon, 6);
        display1.display(); 
      if(phase == 0 && stateDC == 1){
        latStart = gps.location.lat();
        longStart = gps.location.lng();
        phase = 1; stateDC = 0; 
        delay(1000);
        }
      }
      /*****************************************************************************************************************************************************/ 
      if(phase == 1 && choiceDC == 0){
        if(gps.location.isValid() == 1){
          currspeed = gps.speed.mph();
          currlat = gps.location.lat(); latEnd = currlat;
          currlon = gps.location.lng(); longEnd = currlon;
          }
          double distanceM = TinyGPSPlus::distanceBetween(latStart,longStart,latEnd,longEnd);
          double distanceKM = TinyGPSPlus::distanceBetween(latStart,longStart,latEnd,longEnd) / 1000;
          display.setTextSize(2); display.setCursor(0,18); 
          display.print("Total Me:\n"); display.print(String(distanceM) + "Me\n");
          display.display();
          display1.setTextSize(2); display1.setCursor(0,18); display1.setTextColor(WHITE); 
          display1.print("Total Kme:\n");  display1.print(String(distanceKM) + "Km");
          display1.display();
          }
      /*****************************************************************************************************************************************************/ 
      if(phase == 1 && choiceDC == 1){
        if(gps.location.isValid() == 1){
          currspeed = gps.speed.mph();
          currlat = gps.location.lat(); latEnd = currlat;
          currlon = gps.location.lng(); longEnd = currlon;
          }
          double courseTo = TinyGPSPlus::courseTo(latStart,longStart,latEnd,longEnd);
          display.setTextSize(2); display.setCursor(0,18); 
          display.print("Course:\n");
          display.print(courseTo);
          display.print("\n");
          display.print("Cardinal:");
          display.print(TinyGPSPlus::cardinal(courseTo));
          display.display();
          degressinput = courseTo; //Main source
          visualcompass(degressinput);
          }
      }
      /*****************************************************************************************************************************************************/ 
      else if(ss.available() <= 0){
        display.setTextSize(3);display.setTextColor(WHITE);display.setCursor(1,0);
        display.clearDisplay();
        display.print("-------");
        display.print("No GPS?"); 
        display.print("-------");
        display.display(); delay(2000); 
        display.clearDisplay(); display.display(); 
        enter = 0;
      }
  }
/*------------------------------------Ruler--------------------------------------------------------*/
  while (enter == 1 && gpsstate == 5  && stateM == 1){
    if(ss.available() > 0){  
      bstate1 = digitalRead( bpress1 ); bstate3 = digitalRead( bpress3 ); bstate5 = digitalRead( bpress5 ); bstate2 = digitalRead( bpress2 ); bstate4 = digitalRead( bpress4 );
      if(bstate5 == LOW){/*tone(buzzer1, 100000);*/ choiceDC = choiceDC - 1; delay(100); /*noTone(buzzer1);*/ delay(50);}
      if(bstate4 == LOW){/*tone(buzzer1, 100000);*/ choiceDC = choiceDC + 1; delay(100); /*noTone(buzzer1);*/ delay(50);}
      if(bstate1 == LOW){/*tone(buzzer1, 100000);*/ phase = 0; stateDC = 0; choiceDC = 0; enter = 0; delay(100); /*noTone(buzzer1);*/ delay(50);}
      if(bstate2 == LOW || bstate3 == LOW){/*tone(buzzer1, 100000);*/ stateDC = 1; delay(100); /*noTone(buzzer1);*/ delay(50);}
      if(choiceDC > 1){choiceDC = 0;}
      if(choiceDC < 0){choiceDC = 1;}
      /*****************************************************************************************************************************************************/
      display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
      display.print(totalVV);display.print("V");
      display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
      display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
      display.drawLine(0, 16, 128, 16, WHITE);
      /*****************************************************************************************************************************************************/
      if (choiceDC == 0){
          batterylevel(); gpsstatus();
         display1.setTextSize(1); display1.setCursor(0,4); display1.setTextColor(WHITE); display1.clearDisplay();
         display1.print("1.\n"); EnableWifi(); /*EnableBluetooth();*/ display1.drawBitmap(47, 0, bitGps, 16, 16, WHITE); display1.drawBitmap(87, 0, bitBattery, 16, 16, WHITE);
         display1.setTextSize(1); display1.setCursor(60,4); display1.setTextColor(WHITE); display1.print(ssValid + timeValid + dateValid + satValid + locValid); display1.print("%\n");
         display1.setTextSize(1); display1.setCursor(104,4); display1.setTextColor(WHITE); display1.print(totalPP);display1.print("%\n");
         display1.drawLine(0, 16, 128, 16, WHITE); 
      }
      /*****************************************************************************************************************************************************/ 
      gps.encode(ss.read());
      if(gps.location.isValid() == 1){
          currsat = gps.satellites.value();
          curhdop = gps.hdop.value();
          currlat = gps.location.lat();
          currlon = gps.location.lng();
        }
      /*****************************************************************************************************************************************************/ 
      if(phase == 0){
        display.setTextSize(2); display.setCursor(0,18);
        display.print("Set point\n");
        display.print("   (A)\n\n");
        display.print(String(gps.speed.mph())+"Mph");
        display.display();
        display1.setCursor(0,16); display1.setTextSize(2);display1.setTextColor(WHITE); 
        display1.print("Lat/Long:\n"); 
        display1.print(currlat, 6);
        display1.print("\n"); 
        display1.print(currlon, 6);
        display1.display(); 
      if(phase == 0 && stateDC == 1){
        latStart = gps.location.lat();
        longStart = gps.location.lng();
        phase = 1; stateDC = 0; 
        delay(1000);
        }
      }
      /*****************************************************************************************************************************************************/ 
      if(phase == 1){
        display.setTextSize(2); display.setCursor(0,18);
        display.print("Set point\n");
        display.print("   (B)\n\n");
        display.print(String(gps.speed.mph())+"Mph");
        display.display();
        display1.setCursor(0,16); display1.setTextSize(2);display1.setTextColor(WHITE); 
        display1.print("Lat/Long:\n"); 
        display1.print(currlat, 6);
        display1.print("\n"); 
        display1.print(currlon, 6);
        display1.display(); 
      if(phase == 1 && stateDC == 1){
        latEnd = gps.location.lat();
        longEnd = gps.location.lng();
        phase = 2; stateDC = 0; 
        delay(1000);
        }
      }
      /*****************************************************************************************************************************************************/ 
      if(phase == 2 && choiceDC == 0){
        double distanceM = TinyGPSPlus::distanceBetween(latStart,longStart,latEnd,longEnd);
        double distanceKM = TinyGPSPlus::distanceBetween(latStart,longStart,latEnd,longEnd) / 1000;
        display.setTextSize(2); display.setCursor(0,18); 
        display.print("Total Me:\n"); display.print(String(distanceM) + "Me\n");
        display.display();
        display1.setTextSize(2); display1.setCursor(0,18); display1.setTextColor(WHITE); 
        display1.print("Total Kme:\n"); display1.print(String(distanceKM) + "Km");
        display1.display();
        }
      /*****************************************************************************************************************************************************/ 
      if(phase == 2 && choiceDC == 1){
        double courseTo = TinyGPSPlus::courseTo(latStart,longStart,latEnd,longEnd);
        display.setTextSize(2); display.setCursor(0,18); 
        display.print("Course:\n");
        display.print(courseTo);
        display.print("\n");
        display.print("Cardinal:");
        display.print(TinyGPSPlus::cardinal(courseTo));
        display.display();
        degressinput = courseTo; //Main source
        visualcompass(degressinput);
        }
      }
      /*****************************************************************************************************************************************************/ 
      else if(ss.available() <= 0){
        display.setTextSize(3);display.setTextColor(WHITE);display.setCursor(1,0);
        display.clearDisplay();
        display.print("-------");
        display.print("No GPS?"); 
        display.print("-------");
        display.display(); delay(2000); 
        display.clearDisplay(); display.display(); 
        enter = 0;
     }
   }
/*------------------------------------Heartbeat---------------------------------------------------*/
  while (enter == 1 && healthstate == 0 && stateM == 2){
      /*buzzer.loop();*/ button1.loop(); button4.loop(); button5.loop(); 
      if(button5.isReleased()){/*buzzer.beep(100);*/display.clearDisplay();  display1.clearDisplay(); display.display(); display1.display();  Sclear = 1;} 
      if(button4.isReleased()){/*buzzer.beep(100);*/display.clearDisplay();  display1.clearDisplay(); display.display(); display1.display();  Spause  = Spause  + 1;}
      if(button4.isReleased() && Spause > 1){/*buzzer.beep(100);*/display.clearDisplay();  display1.clearDisplay(); display.display(); display1.display();  Spause = 0;}
      if(button1.isReleased()){/*buzzer.beep(100);*/display.clearDisplay();  display1.clearDisplay(); display.display(); display1.display();  enter = 0;}
      /*****************************************************************************************************************************************************/
      display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
      display.print(totalVV);display.print("V");
      display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
      display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
      display.drawLine(0, 16, 128, 16, WHITE);
      /*****************************************************************************************************************************************************/
      display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(0, 18);
      display1.drawLine(0, 43, 127, 43, WHITE); //--> drawLine(x1, y1, x2, y2, color);
      i = analogRead(sensorPin) / 204;
      display.print(int(BPM)); display.setTextSize(2); display.println(" BPM");
      display.print("Rd:"); display.print(analogRead(sensorPin)/4);display.print("\n");
      display.print("Bs:"); display.print(beats);
      display.fillCircle(105, 40, i,  WHITE); display.drawCircle(105, 40, j, WHITE);
      display.display(); 
      /*****************************************************************************************************************************************************/
      if(Spause == 1){
        reading = analogRead(sensorPin) / 4;  
        if(reading > UpperThreshold && IgnoreReading == false){
          if(FirstPulseDetected == false){FirstPulseTime = millis(); FirstPulseDetected = true;}
          else{SecondPulseTime = millis(); PulseInterval = SecondPulseTime - FirstPulseTime; FirstPulseTime = SecondPulseTime; beats++;}
          IgnoreReading = true;
        }
        if(reading < LowerThreshold){
          IgnoreReading = false;
        }  
        BPM = (1.0/PulseInterval) * 60.0 * 1000;
        /*****************************************************************************************************************************************************/
        unsigned long currentMillisGetHR = millis();
        if (currentMillisGetHR - previousMillisGetHR >= intervalGetHR) {previousMillisGetHR = currentMillisGetHR;}
        PulseSensorSignal = analogRead(sensorPin) / 4;  // Loads the PulseSensor's value.
        if (PulseSensorSignal > UpperThreshold && ThresholdStat == true) { cntHB++; ThresholdStat = false;}
        if (PulseSensorSignal < LowerThreshold) {ThresholdStat = true;}
        unsigned long currentMillisResultHR = millis();
        if (currentMillisResultHR - previousMillisResultHR >= intervalResultHR) {
          previousMillisResultHR = currentMillisResultHR;
          BPMval = cntHB * 6; display1.setTextSize(1); display1.setTextColor(WHITE); display1.clearDisplay(); cntHB = 0;
         }
       }
       /*****************************************************************************************************************************************************/
       if(screenE == 1 && Spause == 1){  
        DrawGraph(); display1.setCursor(32, 48); display1.print(BPMval); display1.print(" BPM Avg"); display1.display(); 
       } 
       if(screenE == 1 && Spause == 0){  
        DrawGraph(); display1.setCursor(32, 48); display1.print(" Paused "); display1.display(); 
       } 
     if(Sclear == 1){BPM = 0; sec = 0; beats = 0; BPMval = 0; Sclear = 0;}         
  }
/*------------------------------------Pedometer--------------------------------------------------------*/
  while (enter == 1 && healthstate == 1 && stateM == 2){
      /*buzzer.loop();*/ button1.loop(); button2.loop(); button3.loop(); button4.loop(); button5.loop(); 
      if(button5.isReleased()){/*buzzer.beep(100);*/ display.clearDisplay();  display1.clearDisplay(); display.display(); display1.display(); Sclear = 1;} 
      if(button4.isReleased()){/*buzzer.beep(100);*/ display.clearDisplay();  display1.clearDisplay(); display.display(); display1.display(); Spause = Spause + 1;}
      if(button4.isReleased() && Spause > 1){/*buzzer.beep(100);*/ display.clearDisplay();  display1.clearDisplay(); display.display(); display1.display(); Spause = 0;}
      if(button1.isReleased()){/*buzzer.beep(100);*/ display.clearDisplay();  display1.clearDisplay(); display.display(); display1.display(); enter = 0;}
      /*****************************************************************************************************************************************************/
      display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
      display.print(totalVV);display.print("V");
      display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
      display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
      display.drawLine(0, 16, 128, 16, WHITE);
      /*****************************************************************************************************************************************************/
      Wire.beginTransmission(MPU);
      Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
      Wire.endTransmission(false);
      Wire.requestFrom(MPU,14,true);  // request a total of 14 registers
      GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
      GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
      GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
      Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
      AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)    
      AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
      AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
      dT = ( (double) Tmp + 12412.0) / 340.0;
      if(AcX < 0){AcX = AcX * -1;} if(AcY < 0){AcY = AcY * -1;} if(AcZ < 0){AcZ = AcZ * -1;}
      UpperThrestep = treshold*8000l + 18000l;
      LowerThrestep = treshold*8000l + 17900l;
      /*****************************************************************************************************************************************************/
      display.setTextColor(WHITE); display.setTextSize(2); display.setCursor(0,18);
      display.print("SPM:"); display.print(SPM);display.print("\n");
      display.setTextSize(2);display.print("Ds:");display.print(distance);display.print("\n");
      display.setTextSize(2);display.print("Ca:");display.print(kalori);
      display.display();    
      /*****************************************************************************************************************************************************/
      if(Spause == 1) {
        stoss = AcX + AcY + AcZ; 
        if(stoss > UpperThrestep && IgnoreStepReading == false){
          if(FirstStepDetected == false){FirstStepTime = millis(); FirstStepDetected = true;}
          else{SecondStepTime = millis(); StepInterval = SecondStepTime - FirstStepTime; FirstStepTime = SecondStepTime; display1.fillRect(0, 48, 107, 44, BLACK); display1.display(); steps++;}
          IgnoreStepReading = true;
        }
        if(stoss < LowerThrestep){
          IgnoreStepReading = false;
        }  
        SPM = (1.0/StepInterval) * 60.0 * 1000;
        distance = steps*distanceinonestep/100;
        kalori=steps*0.05;
      }
      if(screenE == 1 && Spause == 1){  
        DrawGraph1(); display1.setCursor(32, 48); display1.print(steps); display1.print(" Steps"); display1.display(); 
       } 
      if(screenE == 1 && Spause == 0){  
        DrawGraph1(); display1.setCursor(32, 48); display1.print(" Paused "); display1.display(); 
      } 
      if(Sclear == 1){steps = 0; distance = 0; Sclear = 0; kalori= 0; SPM = 0;} 
  }
/*------------------------------------Tetris--------------------------------------------------------*/
 while (enter == 1 && gamessstate == 0 && stateM == 3){
  display.clearDisplay(); display1.clearDisplay();
  display.setRotation(3);
  drawLayout();
  display.display(); display1.display();
  randomSeed(analogRead(seedpin));
  nextType = random(TYPES);
  generate();
  tetphase = 1;
  TimerTet = millis();
  delay(200);
   while(enter == 1 && tetphase == 1){
    bstate3 = digitalRead( bpress3 ); bstate2 = digitalRead( bpress2 ); bstate4 = digitalRead( bpress4 ); bstate1 = digitalRead( bpress1 ); bstate5 = digitalRead( bpress5 );
   /*****************************************************************************************************************************************************/
    if(bstate1 == LOW){/*tone(buzzer1, 1000);*/ enter = 0; tetphase = 0; display.setRotation(2);delay(100); /*noTone(buzzer1);*/ delay(50);}
   /*****************************************************************************************************************************************************/
    if(millis() - TimerTet > intervalTetris){
      checkLines();refresh();
      if(nextCollision()){
        for(short i = 0; i < 4; i++)
          grid[pieceX + piece[0][i]][pieceY + piece[1][i]] = 1;
        generate();
      }else
        pieceY++;
      TimerTet = millis();
    }
    /*****************************************************************************************************************************************************/
    if(bstate5 == LOW){
    if(b1){
      if(!nextHorizontalCollision(piece, -1)){
        pieceX--;refresh();
        }
      b1 = false;
     }
    }else{b1 = true;}
    /*****************************************************************************************************************************************************/
    if(bstate4 == LOW){
      if(b2){
        if(!nextHorizontalCollision(piece, 1)){
          pieceX++; refresh();
        }
        b2 = false;
      }
    }else{b2 = true;} 
    if(bstate3 == LOW){intervalTetris = 20;}
    else{intervalTetris = 1000;}
  /*****************************************************************************************************************************************************/
  if(bstate2 == LOW){
    if(b3){
      if(rotation == getMaxRotation(currentType) - 1 && canRotate(0)){
        rotation = 0;
      }else if(canRotate(rotation + 1)){rotation++;}
      copyPiece(piece, currentType, rotation); refresh();
      b3 = false; delayer = millis();
    }
  }else if(millis() - delayer > 50){ b3 = true;}
   }
 }
 /*------------------------------------Pong--------------------------------------------------------*/
 while (enter == 1 && gamessstate == 1 && stateM == 3){
  display.clearDisplay(); display1.clearDisplay();
  display.display(); display1.display();
  unsigned long startpong = millis();
  drawCourt();
  pongphase = 1;
  while(millis() - startpong < 500);
  display.display();
  ball_updatepong = millis();
  paddle_updatepong = ball_updatepong;
  delay(50);
   while(enter == 1 && pongphase == 1){
    bstate1 = digitalRead( bpress1 );  bstate4 = digitalRead( bpress4 ); bstate5 = digitalRead( bpress5 );
    if(bstate1 == LOW){/*tone(buzzer1, 1000);*/ enter = 0; pongphase = 0; delay(100); /*noTone(buzzer1);*/ delay(50);}
    /*****************************************************************************************************************************************************/
    bool updatepong = false;
    unsigned long timepong = millis();
    static bool up_state = false;
    static bool down_state = false;
    up_state |= (bstate5 == LOW);
    down_state |= (bstate4 == LOW);
    /*****************************************************************************************************************************************************/
    if(timepong > ball_updatepong) {
        uint8_t new_x = ball_x + ball_dir_x;
        uint8_t new_y = ball_y + ball_dir_y;
        // Check if we hit the vertical walls
        if(new_x == 0 || new_x == 127) {
            ball_dir_x = -ball_dir_x;
            new_x += ball_dir_x + ball_dir_x;
        }
        // Check if we hit the horizontal walls.
        if(new_y == 0 || new_y == 63) {
            ball_dir_y = -ball_dir_y;
            new_y += ball_dir_y + ball_dir_y;
        }
        // Check if we hit the CPU paddle
        if(new_x == CPU_X && new_y >= cpu_y && new_y <= cpu_y + PADDLE_HEIGHT) {
            ball_dir_x = -ball_dir_x;
            new_x += ball_dir_x + ball_dir_x;
        }

        // Check if we hit the player paddle
        if(new_x == PLAYER_X
           && new_y >= player_y
           && new_y <= player_y + PADDLE_HEIGHT)
        {
            ball_dir_x = -ball_dir_x;
            new_x += ball_dir_x + ball_dir_x;
        }
        display.drawPixel(ball_x, ball_y, BLACK);
        display.drawPixel(new_x, new_y, WHITE);
        ball_x = new_x;
        ball_y = new_y;

        ball_updatepong += BALL_RATE;
        updatepong = true;
    }
    /*****************************************************************************************************************************************************/
    if(timepong > paddle_updatepong) {
        paddle_updatepong += PADDLE_RATE;
        // CPU paddle
        display.drawFastVLine(CPU_X, cpu_y, PADDLE_HEIGHT, BLACK);
        const uint8_t half_paddle = PADDLE_HEIGHT >> 1;
        if(cpu_y + half_paddle > ball_y) {
            cpu_y -= 1;
        }
        if(cpu_y + half_paddle < ball_y) {
            cpu_y += 1;
        }
        if(cpu_y < 1) cpu_y = 1;
        if(cpu_y + PADDLE_HEIGHT > 63) cpu_y = 63 - PADDLE_HEIGHT;
        display.drawFastVLine(CPU_X, cpu_y, PADDLE_HEIGHT, WHITE);

        // Player paddle
        display.drawFastVLine(PLAYER_X, player_y, PADDLE_HEIGHT, BLACK);
        if(up_state) {
            player_y -= 1;
        }
        if(down_state) {
            player_y += 1;
        }
        up_state = down_state = false;
        if(player_y < 1) player_y = 1;
        if(player_y + PADDLE_HEIGHT > 63) player_y = 63 - PADDLE_HEIGHT;
        display.drawFastVLine(PLAYER_X, player_y, PADDLE_HEIGHT, WHITE);
        updatepong = true;
    }
    /*****************************************************************************************************************************************************/
    if(updatepong)
        display.display();
    }
 }
/*------------------------------------Stopwatch---------------------------------------------------*/
  while(enter == 1 && utilitystate == 0 && stateM == 4){
    if(bstate1 == LOW && Spause > 1){/*tone(buzzer1, 1000);*/ Spause = 0; delay(100); /*noTone(buzzer1);*/ delay(1000);}
    /*buzzer.loop();*/ button1.loop(); button2.loop(); button3.loop(); button4.loop(); button5.loop();
    if(button5.isReleased()){/*buzzer.beep(100);*/ transfertime = 1;}
    if(button4.isReleased()){/*buzzer.beep(100);*/ starttime = 1; stoptime = 0;} 
    if(button1.isReleased()){/*buzzer.beep(100);*/ enter = 0;}
    if(button3.isReleased()){/*buzzer.beep(100);*/ starttime = 0; stoptime = 1;}
    if(button2.isReleased()){/*buzzer.beep(100);*/ cleartime = 1;}
    /*****************************************************************************************************************************************************/
    if(starttime == 1) {
       unsigned long currentMillis = millis();
       millisec = millis() - previousMillis;
       if(currentMillis - previousMillis > interval) {
        previousMillis = currentMillis; s++; 
        }
        if(s == 60) {
          s = 0; m++;
          if(m == 60) {
            m = 0; h++;
            if(h == 24) {
              h = 0;
              if(h == 24) {
               h = 0; m = 0; s = 0;
              }
            }
          }
        }
      }
      /*****************************************************************************************************************************************************/
      if (cleartime == 1) { 
        h = 0; m = 0; s = 0; millisec = 0; 
        savedtime1 = " "; savedtime2 = " "; savedtime3 = " ";
        savedtime4 = " "; savedtime5 = " "; savedtime6 = " ";
        savedtime7 = " "; savedtime8 = " "; savedtime = 0;
        cleartime = 0;
      }
      /*****************************************************************************************************************************************************/
      if (transfertime == 1 && savedtime == 0){
        savedtime1 = (String(h) + ":" + String(m) + ":" + String(s) + ":" + String(millisec));
        transfertime = 0; savedtime++;
      }
      if (transfertime == 1 && savedtime == 1){
        savedtime2 = (String(h) + ":" + String(m) + ":" + String(s) + ":" + String(millisec));
        transfertime = 0; savedtime++;
      }
      if (transfertime == 1 && savedtime == 2){
        savedtime3 = (String(h) + ":" + String(m) + ":" + String(s) + ":" + String(millisec));
        transfertime = 0; savedtime++;
      }
      if (transfertime == 1 && savedtime == 3){
        savedtime4 = (String(h) + ":" + String(m) + ":" + String(s) + ":" + String(millisec));
        transfertime = 0; savedtime++;
      }
      if (transfertime == 1 && savedtime == 4){
        savedtime5 = (String(h) + ":" + String(m) + ":" + String(s) + ":" + String(millisec));
        transfertime = 0; savedtime++;
      }
      if (transfertime == 1 && savedtime == 5){
        savedtime6 = (String(h) + ":" + String(m) + ":" + String(s) + ":" + String(millisec));
        transfertime = 0; savedtime++;
      }
      if (transfertime == 1 && savedtime == 6){
        savedtime7 = (String(h) + ":" + String(m) + ":" + String(s) + ":" + String(millisec));
        transfertime = 0; savedtime++;
      }
      if (transfertime == 1 && savedtime == 7){
        savedtime8 = (String(h) + ":" + String(m) + ":" + String(s) + ":" + String(millisec));
        transfertime = 0; savedtime = 0;
      }
      /*****************************************************************************************************************************************************/
      display.setTextSize(2); display.setCursor(0,0); display.setTextColor(WHITE); display.clearDisplay();
      display.print("Stopwatch\n"); 
      display.drawLine(0, 16, 128, 16, WHITE);
      display.setTextSize(3);display.setCursor(0,18);
      display.print(h);display.print(":");
      display.setTextSize(2);
      display.print(m);display.print(":");display.print(s);
      display.print("\n\n"); display.setTextSize(2); display.print("Milli:");display.print(millisec);
      display.display();
      display1.setTextSize(1); display1.setCursor(0,0); display1.setTextColor(WHITE); display1.clearDisplay();
      display1.print("Time 1: " + String(savedtime1) + "\n"); 
      display1.print("Time 2: " + String(savedtime2) + "\n"); 
      display1.print("Time 3: " + String(savedtime3) + "\n"); 
      display1.print("Time 4: " + String(savedtime4) + "\n"); 
      display1.print("Time 5: " + String(savedtime5) + "\n"); 
      display1.print("Time 6: " + String(savedtime6) + "\n"); 
      display1.print("Time 7: " + String(savedtime7) + "\n"); 
      display1.print("Time 8: " + String(savedtime8) + "\n"); 
      display1.display();
   }
/*------------------------------------Timer---------------------------------------------------*/
  while(enter == 1 && utilitystate == 1 && stateM == 4){
    /*buzzer.loop();*/ button1.loop(); button2.loop(); button3.loop(); button4.loop(); button5.loop();
    if(button3.isReleased()){/*buzzer.beep(100);*/ timeadjust = timeadjust + 1;}
    if(button4.isReleased()){/*buzzer.beep(100);*/ starttime = 1; alarmtime = 0;} 
    if(button1.isReleased()){/*buzzer.beep(100);*/ enter = 0;}
    if(button5.isReleased()){/*buzzer.beep(100);*/ starttime = 0; alarmtime = 0;}
    if(button2.isReleased()){/*buzzer.beep(100);*/ cleartime = 1; millistart = 0;}
    /*****************************************************************************************************************************************************/
    if(starttime == 1) {
       unsigned long currentMillis = millis();
       millisec = millis() - previousMillis;
       if((currentMillis - previousMillis > interval) && timephase == 0) {
        previousMillis = currentMillis; st--; millistart = 1000;
        }
        if (st < 0 && mt > 0) {st = 59; mt--;}
        if (st < 0 && mt == 0) {mt = 60; ht--;}
        if (st == 0 && mt == 0 && ht == 0) {alarmtime = 1; starttime = 0;}
        if (ht < 0 ){ht = 0; mt = 0; st = 0; starttime = 0;}
        }
      if(alarmtime == 1){
        /*tone(buzzer1, 1000);*/ delay(100); /*noTone(buzzer1);*/ delay(50);
        display1.setTextSize(3); display1.setCursor(0,0); display1.setTextColor(WHITE); display1.clearDisplay();
        display1.print("DONE"); 
        display1.display();
        }  
      if (cleartime == 1) { ht = 0; mt = 0; st = 0; millisec = 0; cleartime = 0; display1.setCursor(0,0); display1.setTextColor(WHITE); display1.clearDisplay(); display1.display();}
      /*****************************************************************************************************************************************************/
      while(timeadjust >= 1){
      /*buzzer.loop();*/ button1.loop(); button2.loop(); button3.loop(); button4.loop(); button5.loop();
      if(button3.isReleased()){/*buzzer.beep(100);*/ adjustt = adjustt + 1;}
      if(button2.isReleased()){/*buzzer.beep(100);*/ timeadjust = timeadjust + 1;}
      if(button1.isReleased()){/*buzzer.beep(100);*/ adjustt = 0; timeadjust = 0; enter = 0;}
      if(timeadjust > 1){/*buzzer.beep(100);*/ adjustt = 0; timeadjust = 0;}
      /*****************************************************************************************************************************************************/
        if(adjustt == 0){
          display.setTextSize(2); display.setCursor(0,0); display.setTextColor(WHITE); display.clearDisplay();
          display.print("Change Hou\n"); 
          display.drawLine(0, 16, 128, 16, WHITE);
          if(button5.isReleased()){/*buzzer.beep(100);*/ ht = ht + 1;}
          if(button4.isReleased()){/*buzzer.beep(100);*/ ht = ht - 1;}
          if(ht < 0){ht = 0;}if(ht > 24){ht = 24;}
          }
        if(adjustt == 1){
          display.setTextSize(2); display.setCursor(0,0); display.setTextColor(WHITE); display.clearDisplay();
          display.print("Change Min\n"); 
          display.drawLine(0, 16, 128, 16, WHITE);
          if(button5.isReleased()){/*buzzer.beep(100);*/ mt = mt + 1;}
          if(button4.isReleased()){/*buzzer.beep(100);*/ mt = mt - 1;}
          if(mt < 0){mt = 0;}if(mt > 59){mt = 59;}
          }
        if(adjustt == 2){
          display.setTextSize(2); display.setCursor(0,0); display.setTextColor(WHITE); display.clearDisplay();
          display.print("Change Sec\n"); 
          display.drawLine(0, 16, 128, 16, WHITE);
          if(button5.isReleased()){/*buzzer.beep(100);*/ st = st + 1;}
          if(button4.isReleased()){/*buzzer.beep(100);*/ st = st - 1;}
          if(st < 0){st = 0;}if(st > 59){st = 59;}
          }
        if (adjustt > 2){adjustt = 0;}
        display.setTextSize(3);display.setCursor(0,18);
        display.print(ht);display.print(":");
        display.setTextSize(2);
        display.print(mt);display.print(":");display.print(st);
        display.print("\n\n"); display.setTextSize(2); display.print("Milli:");display.print(millisec);
        display.display();
      }
      /*****************************************************************************************************************************************************/
      display.setTextSize(2); display.setCursor(0,0); display.setTextColor(WHITE); display.clearDisplay();
      display.print("Timer\n"); 
      display.drawLine(0, 16, 128, 16, WHITE);
      display.setTextSize(3);display.setCursor(0,18);
      display.print(ht);display.print(":");
      display.setTextSize(2);
      display.print(mt);display.print(":");display.print(st);
      display.print("\n\n"); display.setTextSize(2); display.print("Milli:");display.print(millistart - millisec);
      display.display();
  }

/*------------------------------------Calculator---------------------------------------------------*/
  while(enter == 1 && utilitystate == 2 && stateM == 4){
      /*buzzer.loop();*/ button1.loop(); button2.loop(); button3.loop(); button4.loop(); button5.loop(); 
      if(charlimit >= 7){enterkey = 0;}
      if(button5.isReleased()){/*buzzer.beep(100);*/ selectkey = selectkey - 1;} 
      if(button4.isReleased()){/*buzzer.beep(100);*/ selectkey = selectkey + 1;}
      if(button1.isReleased()){/*buzzer.beep(100);*/ enter = 0;}
      if(button3.isReleased()){/*buzzer.beep(100);*/ enterkey = enterkey + 1;} 
      if(button2.isReleased()){/*buzzer.beep(100);*/ inputString = ' '; charlimit = 0;}
      if(selectkey > 19){selectkey = 0;} if(selectkey < 0){selectkey = 19;}
      /*****************************************************************************************************************************************************/
      if ((selectkey == 11 && enterkey > 0 && enOP != 1 && (enOP == 2 || enOP == 3 || enOP == 4 || enOP == 5))||
         (selectkey == 12 && enterkey > 0 && enOP != 2 && (enOP == 1 || enOP == 3 || enOP == 4 || enOP == 5))||
         (selectkey == 13 && enterkey > 0 && enOP != 3 && (enOP == 1 || enOP == 2 || enOP == 4 || enOP == 5))||
         (selectkey == 14 && enterkey > 0 && enOP != 4 && (enOP == 1 || enOP == 2 || enOP == 3 || enOP == 5))||
         (selectkey == 15 && enterkey > 0 && enOP != 5 && (enOP == 1 || enOP == 2 || enOP == 3 || enOP == 4))){
         enOP = 0; enExp = 0; enPls = 0; enMin = 0; enDiv = 0; enMul = 0; recursive = 1;
     }
     /*****************************************************************************************************************************************************/ 
     switch (selectkey){
        case 0: display1.drawRect(9, 1, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0 && charlimit < 7){inputString += '1'; charlimit += 1; enterkey = 0;}break; // key 1
        case 1: display1.drawRect(33, 1, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0 && charlimit < 7){inputString += '2'; charlimit += 1; enterkey = 0;}break; // key 2
        case 2: display1.drawRect(57, 1, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0 && charlimit < 7){inputString += '3'; charlimit += 1; enterkey = 0;}break; // key 3
        case 3: display1.drawRect(9, 16, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0 && charlimit < 7){inputString += '4'; charlimit += 1; enterkey = 0;}break; // key 4
        case 4: display1.drawRect(33, 16, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0 && charlimit < 7){inputString += '5'; charlimit += 1; enterkey = 0;}break; // key 5
        case 5: display1.drawRect(57, 16, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0 && charlimit < 7){inputString += '6'; charlimit += 1; enterkey = 0;}break; // key 6
        case 6: display1.drawRect(9, 32, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0 && charlimit < 7){inputString += '7'; charlimit += 1; enterkey = 0;}break; // key 7
        case 7: display1.drawRect(33, 32, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0 && charlimit < 7){inputString += '8'; charlimit += 1; enterkey = 0;}break; // key 8
        case 8: display1.drawRect(57, 32, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0 && charlimit < 7){inputString += '9'; charlimit += 1; enterkey = 0;}break; // key 9
        case 9: display1.drawRect(9, 48, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0 && charlimit < 7){inputString += '0'; charlimit += 1; enterkey = 0;}break; // key 0
        case 10: display1.drawRect(33, 48, 16, 15, WHITE); display1.display(); 
            if(enterkey > 0 && charlimit < 7 && enDec == 0){inputString += '.'; charlimit += 1; enDec = 1; enterkey = 0;}break; // key .
        /*****************************************************************************************************************************************************/    
        case 11: display1.drawRect(57, 48, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0 && enExp == 0 && enPls == 0 && enMin == 0 && enDiv == 0 && enMul == 0 && enOP == 0){
             enOP = 1; enExp = 1; enPls = 0; enMin = 0; enDiv = 0; enMul = 0; enDec = 0;
           if(recursive == 0){num1 = inputString.toFloat();}if(recursive == 1){num1 = numberFinal; recursive = 0;} enterkey = 0;} 
           else if(enterkey > 0 && enExp == 1 && enOP == 1){num2 = inputString.toFloat(); numberFinal = pow(num1,num2); num1 = numberFinal; enterkey = 0;} break; // key ^(1)
        /*****************************************************************************************************************************************************/    
        case 12: display1.drawRect(81, 1, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0 && enExp == 0 && enPls == 0 && enMin == 0 && enDiv == 0 && enMul == 0 && enOP == 0){
             enOP = 2; enExp = 0; enPls = 1; enMin = 0; enDiv = 0; enMul = 0; enDec = 0;
           if(recursive == 0){num1 = inputString.toFloat();}if(recursive == 1){num1 = numberFinal; recursive = 0;} enterkey = 0;} 
           else if(enterkey > 0 && enPls == 1 && enOP == 2){num2 = inputString.toFloat(); numberFinal = num1 + num2; num1 = numberFinal; enterkey = 0;} break; // key +(2)
        /*****************************************************************************************************************************************************/    
        case 13: display1.drawRect(81, 16, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0 && enExp == 0 && enPls == 0 && enMin == 0 && enDiv == 0 && enMul == 0 && enOP == 0){
             enOP = 3; enExp = 0; enPls = 0; enMin = 1; enDiv = 0; enMul = 0; enDec = 0;
           if(recursive == 0){num1 = inputString.toFloat();}if(recursive == 1){num1 = numberFinal; recursive = 0;} enterkey = 0;} 
           else if(enterkey > 0 && enMin == 1 && enOP == 3){num2 = inputString.toFloat(); numberFinal = num1 - num2; num1 = numberFinal; enterkey = 0;} break; // key -(3)
        /*****************************************************************************************************************************************************/    
        case 14: display1.drawRect(81, 32, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0 && enExp == 0 && enPls == 0 && enMin == 0 && enDiv == 0 && enMul == 0 && enOP == 0){
             enOP = 4; enExp = 0; enPls = 0; enMin = 0; enDiv = 1; enMul = 0; enDec = 0;
           if(recursive == 0){num1 = inputString.toFloat();}if(recursive == 1){num1 = numberFinal; recursive = 0;} enterkey = 0;} 
           else if(enterkey > 0 && enDiv == 1 && enOP == 4){num2 = inputString.toFloat(); numberFinal = num1 / num2; num1 = numberFinal; enterkey = 0;} break; // key /(4)
        /*****************************************************************************************************************************************************/     
        case 15: display1.drawRect(81, 48, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0 && enExp == 0 && enPls == 0 && enMin == 0 && enDiv == 0 && enMul == 0 && enOP == 0){
             enOP = 5; enExp = 0; enPls = 0; enMin = 0; enDiv = 0; enMul = 1; enDec = 0;
           if(recursive == 0){num1 = inputString.toFloat();}if(recursive == 1){num1 = numberFinal; recursive = 0;} enterkey = 0;} 
           else if(enterkey > 0 && enMul == 1 && enOP == 5){num2 = inputString.toFloat(); numberFinal = num1 * num2; num1 = numberFinal; enterkey = 0;} break; // key *(5)
        /*****************************************************************************************************************************************************/     
        case 16: display1.drawRect(105, 1, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0 && enOP != 0 && enEnt == 0){enEnt = 1; charlimit = 0; enDec = 0; enterkey = 0;} break; // key =
        case 17: display1.drawRect(105, 16, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0){inputString = ' '; charlimit = 0; enDec = 0; num1 = 0.0; num2 = 0.0; numberFinal = 0.0; enOP = 0; enExp = 0; enPls = 0; enMin = 0; enDiv = 0; enMul = 0; enterkey = 0;}break; // key Clear
        case 18: display1.drawRect(105, 32, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0){storedvalue = numberFinal; enterkey = 0;}break; // key Store
        case 19: display1.drawRect(105, 48, 16, 15, WHITE); display1.display(); 
           if(enterkey > 0){inputString = String(storedvalue, 2); enterkey = 0;}break; // key Load
       }
       /*****************************************************************************************************************************************************/
       display.setTextSize(1); display.setCursor(0,0); display.setTextColor(WHITE); display.clearDisplay();
       display.drawLine(0, 0, 128, 0, WHITE);
       display.drawLine(0, 16, 128, 16, WHITE);
       display.drawLine(107, 16, 107, 0, WHITE);
       display.drawLine(0, 0, 0, 16, WHITE);
       display.drawLine(127, 0, 127, 16, WHITE);
       display.setTextSize(2); display.setCursor(0,0);
       display.print(">");display.print(inputString);
       display.print("\n"); display.print(num1);
       display.print("\n"); display.print(num2);
       display.print("\n"); display.print(numberFinal);
       /*****************************************************************************************************************************************************/
       switch (enOP){
         case 1: display.setTextSize(2); display.setCursor(112,0); display.print('^'); 
           if(enEnt == 1 && enOP == 1){num2 = inputString.toFloat(); numberFinal = pow(num1,num2); inputString = ' ';  
           enOP = 0; enExp = 0; enPls = 0; enMin = 0; enDiv = 0; enMul = 0; enterkey = 0;enEnt = 0;} break; 
         case 2: display.setTextSize(2); display.setCursor(112,0); display.print('+'); 
           if(enEnt == 1 && enOP == 2){num2 = inputString.toFloat(); numberFinal = num1 + num2; inputString = ' ';  
           enOP = 0; enExp = 0; enPls = 0; enMin = 0; enDiv = 0; enMul = 0; enterkey = 0;enEnt = 0;} break; 
         case 3: display.setTextSize(2); display.setCursor(112,0); display.print('-'); 
           if(enEnt == 1 && enOP == 3){num2 = inputString.toFloat(); numberFinal = num1 - num2; inputString = ' ';  
           enOP = 0; enExp = 0; enPls = 0; enMin = 0; enDiv = 0; enMul = 0; enterkey = 0;enEnt = 0;} break; 
         case 4: display.setTextSize(2); display.setCursor(112,0); display.print('/'); 
           if(enEnt == 1 && enOP == 4){num2 = inputString.toFloat(); numberFinal = num1 / num2; inputString = ' ';  
           enOP = 0; enExp = 0; enPls = 0; enMin = 0; enDiv = 0; enMul = 0; enterkey = 0;enEnt = 0;} break; 
         case 5: display.setTextSize(2); display.setCursor(112,0); display.print('*'); 
           if(enEnt == 1 && enOP == 5){num2 = inputString.toFloat(); numberFinal = num1 * num2; inputString = ' ';  
           enOP = 0; enExp = 0; enPls = 0; enMin = 0; enDiv = 0; enMul = 0; enterkey = 0;enEnt = 0;} break;
       }
       /*****************************************************************************************************************************************************/
       display.display();
       display1.clearDisplay(); display1.setTextSize(2); display1.setCursor(0,0); display1.setTextColor(WHITE);
       display1.print(" 1 2 3 + =\n");
       display1.print(" 4 5 6 - C\n"); 
       display1.print(" 7 8 9 / S\n"); 
       display1.print(" 0 . ^ * L\n"); 
       display1.drawLine(0, 0, 128, 0, WHITE);
       display1.drawLine(0, 63, 128, 63, WHITE);
       display1.drawLine(100, 0, 100, 63, WHITE);
       display1.drawLine(76, 0, 76, 63, WHITE);
       display1.drawLine(0, 0, 0, 63, WHITE);
       display1.drawLine(127, 0, 127, 63, WHITE);
       display1.display();
  }
  
/*-------------------------------------Temp/Humidity-----------------------------------------------------------*/
  while (enter == 1 && otherstate == 0 && stateM == 5){
    /*buzzer.loop();*/ button1.loop(); 
    if(button1.isReleased()){/*buzzer.beep(100);*/ enter = 0;}
      Wire.beginTransmission(MPU);
      Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
      Wire.endTransmission(false);
      Wire.requestFrom(MPU,14,true);  // request a total of 14 registers
      /*****************************************************************************************************************************************************/
      GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
      GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
      GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
      Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
      AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)    
      AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
      AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
      float dC = ((Tmp/340) + 30.53); //36.53
      float dT = ((dC * 1.8) + 32);
      float dK = (dC + 273.15);
      /*****************************************************************************************************************************************************/
      display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
      display.print(totalVV);display.print("V");
      display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
      display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
      display.drawLine(0, 16, 128, 16, WHITE);
      /*****************************************************************************************************************************************************/
      batterylevel(); gpsstatus();
      display1.setTextSize(1); display1.setCursor(0,4); display1.setTextColor(WHITE); display1.clearDisplay();
      display1.print("T.\n"); EnableWifi(); /*EnableBluetooth();*/ display1.drawBitmap(47, 0, bitGps, 16, 16, WHITE); display1.drawBitmap(87, 0, bitBattery, 16, 16, WHITE);
      display1.setTextSize(1); display1.setCursor(60,4); display1.setTextColor(WHITE); display1.print(ssValid + timeValid + dateValid + satValid + locValid); display1.print("%\n");
      display1.setTextSize(1); display1.setCursor(104,4); display1.setTextColor(WHITE); display1.print(totalPP);display1.print("%\n");
      display1.drawLine(0, 16, 128, 16, WHITE);
      /*****************************************************************************************************************************************************/ 
      display.setTextSize(2); display.setCursor(0,18); display.setTextColor(WHITE); 
      display.drawLine(0, 16, 128, 16, WHITE);
      display.setCursor(0,20); 
      display.print("Fa: "); display.print(dT);
      display.setCursor(0,38);
      display.print("Ce: "); display.print(dC);
      display.display();
      display1.setTextSize(2); display1.setCursor(0,18); display1.setTextColor(WHITE); 
      display1.print("Ke: "); display1.print(dK);
      display1.display();
  }
/*------------------------------------Accel/Gyro--------------------------------------------------------*/
  while (enter == 1 && otherstate == 1 && stateM == 5){
      /*buzzer.loop();*/ button1.loop(); button2.loop(); button3.loop(); button4.loop(); button5.loop(); 
      if(button4.isReleased()){/*buzzer.beep(100);*/ Spause = Spause - 1;} 
      if(button5.isReleased()){/*buzzer.beep(100);*/ Spause = Spause + 1;}
      if(button1.isReleased()){/*buzzer.beep(100);*/ enter = 0;}
      if(Spause > 1){Spause = 0;}
      if(Spause < 0){Spause = 1;}
      /*****************************************************************************************************************************************************/ 
      Wire.beginTransmission(MPU);
      Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
      Wire.endTransmission(true);
      Wire.requestFrom(MPU,14,true);  // request a total of 14 registers
      AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)     
      AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
      AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
      Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
      GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
      GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
      GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
      /*****************************************************************************************************************************************************/ 
      display.setTextSize(2); display.setCursor(0,0); display.setTextColor(WHITE); display.clearDisplay();
      display.print("MPU6050:"); 
      display.drawLine(0, 16, 128, 16, WHITE);
      /*****************************************************************************************************************************************************/ 
      if(Spause == 1) {
        display.setTextColor(WHITE); display.setTextSize(2); display.setCursor(0,16);
        display.print("AcX:"); display.print(AcX);
        display.print("\n"); 
        display.print("AcY:"); display.print(AcY);
        display.print("\n"); 
        display.print("AcZ:"); display.print(AcZ);
        display.display();
        }
      if (Spause == 0) {
        display.setTextColor(WHITE); display.setTextSize(2); display.setCursor(0,16);
        display.print("GyX:"); display.print(GyX);
        display.print("\n"); 
        display.print("GyY:"); display.print(GyY);
        display.print("\n"); 
        display.print("GyZ:"); display.print(GyZ);
        display.display();
        }
        display1.display();
        display1.clearDisplay();   // clears the screen and buffer  
        //For cube rotation
        int xOut=0;
        int yOut=0;
        xOut = map(AcX,-17000,17000,-50,50);
        yOut = map(AcY,-17000,17000,-50,50);
        Xan+=xOut;
        Yan+=yOut;
        Yan=Yan % 360;
        Xan=Xan % 360; // prevents overflow.
        SetVars(); //sets up the global vars to do the conversion.
        for(int i=0; i<LinestoRender ; i++){
          ORender[i]=Render[i]; // stores the old line segment so we can delete it later.
          ProcessLine(&Render[i],Lines[i]); // converts the 3d line segments to 2d.
          }  
        RenderImage(); // go draw it!
        
  }
/*-------------------------------------Wifi Sc-----------------------------------------------------------*/
  while (enter == 1 && networkstate == 0 && stateM == 6){
    if(Ewifi == 1){      
        /*buzzer.loop();*/ button1.loop(); button2.loop(); button3.loop(); 
        if(button1.isReleased() || button1.isPressed()){/*buzzer.beep(100);*/ WifiScan = 0; enter = 0;}
        /*****************************************************************************************************************************************************/
        display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
        display.print(totalVV);display.print("V");
        display.setTextSize(1); display1.setCursor(60,4); display.print(" | "); display.print(cpuFreq);display.print("MHz");
        display.setTextSize(1); display1.setCursor(104,4); display.setTextColor(WHITE);display.print(" | "); display.print(sleepsecM/10);display.print("s");
        display.drawLine(0, 16, 128, 16, WHITE);
        /*****************************************************************************************************************************************************/
        display.setTextSize(2); display.setCursor(0,18);
        display1.setTextSize(1); display1.setCursor(0,0); display1.setTextColor(WHITE);display1.clearDisplay();
        int network = WiFi.scanNetworks();
        if (network == 0) { display.println("No Networks Found");}
        else {display.print(network); display.println(" Networks Found");
          for (int i = 0; i < network; ++i) {
            display1.print(i + 1);
            display1.print(":");
            display1.print(WiFi.SSID(i));
            display1.print (WiFi.RSSI(i));
            display1.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? "   O ":"   P");
            }
        }
        display.display(); display1.display();
      }
      /*****************************************************************************************************************************************************/
    else{
      enter = 0;
    }
         
  }
/*-------------------------------------Ble Sc-----------------------------------------------------------*/
  /*while (enter == 1 && networkstate == 1 && stateM == 6){
    if(Ebluetooth == 1){      
      /*buzzer.loop();*/ //button1.loop(); button2.loop(); button3.loop(); 
     // if(button3.isReleased()){/*buzzer.beep(100);*/ enter = 0;}
     // if(button2.isReleased()){/*buzzer.beep(100);*/ BleScan = BleScan + 1;}
     // if(button2.isReleased() && BleScan > 1){/*buzzer.beep(100);*/ BleScan = 0;}
     // if(button1.isReleased()){/*buzzer.beep(100);*/ shutoff = shutoff + 1;}
     // if(button1.isReleased() && shutoff > 1){/*buzzer.beep(100);*/ shutoff = 0;}
 
     //  batterylevel(); gpsstatus();
     //   display.setTextSize(1); display.setCursor(0,4); display.setTextColor(WHITE); display.clearDisplay();
     /*   display.print("3.\n"); EnableWifi(); *//*EnableBluetooth();*/ /*display.drawBitmap(47, 0, bitGps, 16, 16, WHITE); display.drawBitmap(87, 0, bitBattery, 16, 16, WHITE);
        display.setTextSize(1); display.setCursor(60,4); display.setTextColor(WHITE); display.print(ssValid + timeValid + dateValid + satValid + locValid); display.print("%\n");
        display.setTextSize(1); display.setCursor(104,4); display.setTextColor(WHITE); display.print(totalPP);display.print("%\n");
        display.drawLine(0, 16, 128, 16, WHITE);
        
     if(BleScan == 1){
     
     }
     display.display();
    }
    else{
      enter = 0;
    }
         
  }*/
/*------------------------------------CpuFreq---------------------------------------------------*/
  while(enter == 1 && settingsstate == 0 && stateM == 7){
      setCpuFrequencyMhz(cpuspeed);cpuFreq = getCpuFrequencyMhz();
      sleeptimemenu = setSleep;
      /*buzzer.loop();*/ button1.loop(); button2.loop(); button3.loop(); button4.loop(); button5.loop(); 
      if(button1.isReleased()){/*buzzer.beep(100);*/ enter = 0;}
      if(button4.isReleased()){/*buzzer.beep(100);*/ cpuspeedState = cpuspeedState - 1;} 
      if(button5.isReleased()){/*buzzer.beep(100);*/ cpuspeedState = cpuspeedState + 1;}
      if(cpuspeedState > 4){cpuspeedState = 0;} if(cpuspeedState < 0){cpuspeedState = 4;}
      /*****************************************************************************************************************************************************/
      display.setTextSize(2); display.setCursor(0,0); display.setTextColor(WHITE); display.clearDisplay();
      display1.setTextSize(2); display1.setCursor(0,0); display1.setTextColor(WHITE); display1.clearDisplay();
      display.print("CPU Speed\n"); 
      display.drawLine(0, 16, 128, 16, WHITE);
      display1.print("1. 10 Mhz \n");
      display1.print("2. 20 Mhz \n"); 
      display1.print("3. 40 Mhz \n"); 
      display.print("4. 80 Mhz \n");
      display.print("5. 160 Mhz\n"); 
      //display.print("6. 240 Mhz\n"); 
      display.display(); 
      display1.display();
      /*****************************************************************************************************************************************************/
      switch (cpuspeedState){
         case 0: display1.drawRect(0, 0, 128, 15, WHITE); cpuspeed = 10; display1.display(); break; //10 
         case 1: display1.drawRect(0, 16, 128, 15, WHITE); cpuspeed = 20; display1.display(); break; //20
         case 2: display1.drawRect(0, 32, 128, 15, WHITE); cpuspeed = 40; display1.display(); break; //40
         case 3: display.drawRect(0, 16, 128, 15, WHITE); cpuspeed = 80; display.display(); break; //80
         case 4: display.drawRect(0, 32, 128, 15, WHITE); cpuspeed = 160; display.display(); break; //160
         //case 5: display.drawRect(0, 48, 128, 15, WHITE); cpuspeed = 240; display.display(); break; //240
       }
  }
/*------------------------------------SleepTTime---------------------------------------------------*/
  while(enter == 1 && settingsstate == 1 && stateM == 7){
      sleeptimemenu = setSleep;
      /*buzzer.loop();*/ button1.loop(); button2.loop(); button3.loop(); button4.loop(); button5.loop(); 
      if(button1.isReleased()){/*buzzer.beep(100);*/ enter = 0;}
      if(button4.isReleased()){/*buzzer.beep(100);*/ sleepState = sleepState - 1;} 
      if(button5.isReleased()){/*buzzer.beep(100);*/ sleepState = sleepState + 1;}
      if(sleepState > 5){sleepState = 0;} if(sleepState < 0){sleepState = 5;}
      /*****************************************************************************************************************************************************/
      display.setTextSize(2); display.setCursor(0,0); display.setTextColor(WHITE); display.clearDisplay();
      display1.setTextSize(2); display1.setCursor(0,0); display1.setTextColor(WHITE); display1.clearDisplay();
      display.print("Sleep Time\n"); 
      display.drawLine(0, 16, 128, 16, WHITE);
      display.print("1. 10 Sec\n");
      display.print("2. 20 Sec \n"); 
      display.print("3. 40 Sec \n"); 
      display1.print("4. 60 Sec \n");
      display1.print("5. 80 Sec \n");
      display1.print("6. 100 Sec \n");
      display.display(); 
      display1.display();
      /*****************************************************************************************************************************************************/
      switch (sleepState){
         case 0: display.drawRect(0, 16, 128, 15, WHITE); setSleep = 100; display.display(); break; //10 
         case 1: display.drawRect(0, 32, 128, 15, WHITE); setSleep = 200; display.display(); break; //20
         case 2: display.drawRect(0, 48, 128, 15, WHITE); setSleep = 400; display.display(); break; //40
         case 3: display1.drawRect(0, 0, 128, 15, WHITE); setSleep = 600; display1.display(); break; //60
         case 4: display1.drawRect(0, 16, 128, 15, WHITE); setSleep = 800; display1.display(); break; //80 
         case 5: display1.drawRect(0, 32, 128, 15, WHITE); setSleep = 1000; display1.display(); break; //100
       }
  }
/*------------------------------------Help---------------------------------------------------*/
  while(enter == 1 && settingsstate == 3 && stateM == 7){
    /*buzzer.loop();*/ button1.loop(); button4.loop(); button5.loop(); 
      if(button5.isReleased()){/*buzzer.beep(100);*/ helpsel = helpsel - 1;} 
      if(button4.isReleased()){/*buzzer.beep(100);*/ helpsel = helpsel + 1;}
      if(button1.isReleased()){/*buzzer.beep(100);*/ enter = 0;}
      if(helpsel > 1){helpsel = 0;} if(helpsel < 0){helpsel = 1;}
      /*****************************************************************************************************************************************************/
      display.setTextSize(1); display.setCursor(16,0); display.setTextColor(WHITE); display.clearDisplay();
      display1.setTextSize(1); display1.setCursor(0,0); display1.setTextColor(WHITE); display1.clearDisplay();
      display.print("8 7 6 5 4 3 2 1\n"); 
      display.drawRect(30, 10, 70, 54, WHITE);
      display.setCursor(110,10);
      display.print("A\n"); 
      display.setCursor(110,19);
      display.print("B\n");
      display.setCursor(110,28);
      display.print("C\n");
      display.setCursor(110,37);
      display.print("D\n");
      display.setCursor(110,46);
      display.print("E\n");
      display.setCursor(110,55);
      display.print("F\n");
      display.setCursor(30,32);
      display.print("< Device >\n");
      display.display(); 
      /*****************************************************************************************************************************************************/
      switch (helpsel){
         case 0: 
            display1.print("A: Select up   (+)\n"); 
            display1.print("B: Select down (-)\n"); 
            display1.print("C: Enter/Exit  (X)\n"); 
            display1.print("D: Action 2    (?)\n"); 
            display1.print("E: Action 1    (?)\n"); 
            display1.print("F: Reset       (O)\n"); 
            break; //10 
         case 1: 
            display1.print("8: TXD input    (RXD)\n"); 
            display1.print("7: RXD input    (TXD)\n"); 
            display1.print("6: Positive out (+)\n"); 
            display1.print("5: Signal in    (S)\n"); 
            display1.print("4: Ground       (-)\n"); 
            display1.print("3: Charging G   (C-)\n"); 
            display1.print("2: Charging P   (C+)\n"); 
            display1.print("1: Charging G   (C-)\n"); 
            break; //20
      }
      /*****************************************************************************************************************************************************/
      display1.display();
  }
/*---------------------------------Firmware---------------------------------------------------------*/
  while(enter == 1 && settingsstate == 4 && stateM == 7){
      bstate1 = digitalRead( bpress1 ); button1.loop();
      if(bstate1 == LOW){/*tone(buzzer1, 1000);*/ delay(100); /*noTone(buzzer1);*/ enter = 0; delay(50);}
      /*****************************************************************************************************************************************************/
      display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(0,0); display.clearDisplay();
      display.print("  BASICOS \n\n");
      display.drawLine(0, 20, 128, 20, WHITE); 
      display.setTextSize(1); display.setTextColor(WHITE); 
      display.print("Created by:\n");
      display.print("David Truong\n");
      display.print("Year: 2023\n");
      display.print("Version: 4.1C3\n");
      display.print("Credits:\n");
      display.display(); 
      /*****************************************************************************************************************************************************/
      display1.clearDisplay(); display1.setTextSize(1); display1.setCursor(0,0); display1.setTextColor(WHITE); 
      display1.print("mdraber- A sty. clock\n");
      display1.print("Mantex E- Tetris\n");
      display1.print("shveytank- Pong\n");
      display1.print("Peter Clement- Wifi S\n");
      display1.print("cyaninfinite- MPUCube\n");
      display1.print("Jochen Peters- Step C\n");
      display1.print("xtronical- BPM graph\n");
      display1.print("Chris Rouse- Moon Cal\n");
      display1.display();     
   } 
}


/***********************************************************************************************************************************/
void RenderImage(void)
{
  // renders all the lines after erasing the old ones.
 for (int i=0; i<OldLinestoRender; i++ ){
   display1.drawLine(ORender[i].p0.x,ORender[i].p0.y,ORender[i].p1.x,ORender[i].p1.y, BLACK); // erase the old lines.
   }
  for (int i=0; i<LinestoRender; i++ ){
   display1.drawLine(Render[i].p0.x,Render[i].p0.y,Render[i].p1.x,Render[i].p1.y, WHITE);
   }
  OldLinestoRender=LinestoRender;
  // text display tests
  display1.setTextSize(2);display1.setTextColor(WHITE);display1.setCursor(0,0);
  display1.print("Cube: ");
}
/***********************************************************************************************************************************/
// Sets the global vars for the 3d transform. Any points sent through "process" will be transformed using these figures.
// only needs to be called if Xan or Yan are changed.
void SetVars(void) {
  float Xan2,Yan2,Zan2;
  float s1,s2,s3,c1,c2,c3;
  Xan2 = Xan / fact; // convert degrees to radians.
  Yan2 = Yan / fact;
  s1 = sin(Yan2);s2 = sin(Xan2);
  c1 = cos(Yan2);c2 = cos(Xan2);
  xx = c1; xy = 0; xz = -s1;
  yx = (s1 * s2); yy = c2; yz = (c1 * s2);
  zx = (s1 * c2); zy = -s2; zz = (c1 * c2);
}

/***********************************************************************************************************************************/
// processes x1,y1,z1 and returns rx1,ry1 transformed by the variables set in SetVars()
// fairly heavy on floating point here.
void ProcessLine(struct Line2d *ret,struct Line3d vec){
  float zvt1; float zvt2;
  int xv1,yv1,zv1; int xv2,yv2,zv2;
  int rx1,ry1; int rx2,ry2;
  int x1;int y1;int z1;
  int x2;int y2;int z2; int Ok;
  x1=vec.p0.x; x2=vec.p1.x;
  y1=vec.p0.y; y2=vec.p1.y;
  z1=vec.p0.z; z2=vec.p1.z;
  Ok=0; // defaults to not OK
  xv1 = (x1 * xx) + (y1 * xy) + (z1 * xz);
  yv1 = (x1 * yx) + (y1 * yy) + (z1 * yz);
  zv1 = (x1 * zx) + (y1 * zy) + (z1 * zz);
  zvt1 = zv1 - Zoff;
  if( zvt1 < -5){
    rx1 = 256 * (xv1 / zvt1) + Xoff;
    ry1 = 256 * (yv1 / zvt1) + Yoff;
    Ok=1; 
  }
  xv2 = (x2 * xx) + (y2 * xy) + (z2 * xz);
  yv2 = (x2 * yx) + (y2 * yy) + (z2 * yz);
  zv2 = (x2 * zx) + (y2 * zy) + (z2 * zz);
  zvt2 = zv2 - Zoff;
  if( zvt2 < -5){
    rx2 = 256 * (xv2 / zvt2) + Xoff;
    ry2 = 256 * (yv2 / zvt2) + Yoff;
  } else { Ok=0;}
  if(Ok==1){ret->p0.x=rx1; ret->p0.y=ry1; ret->p1.x=rx2; ret->p1.y=ry2;}
 // The ifs here are checks for out of bounds. needs a bit more code here to "safe" lines that will be way out of whack, so they dont get drawn and cause screen garbage.
}
/***********************************************************************************************************************************/
void DrawGraph() {
  if (x > 127) {display1.fillRect(0, 0, 128, 42, BLACK); x = 0; lastx = 0;}
  int ySignal = PulseSensorSignal; 
  if (ySignal > 1022) ySignal = 1022;
  if (ySignal < 511) ySignal = 511;
  int ySignalMap = map(ySignal, 511, 1022, 0, 40); //--> The y-axis used on OLEDs is from 0 to 40
  y = 40 - ySignalMap;
  display1.writeLine(lastx,lasty,x,y,WHITE);
  display1.display(); 
  lastx = x;
  lasty = y;
  x++;
}
/***********************************************************************************************************************************/
void DrawGraph1() {
  if (x > 127) {display1.fillRect(0, 0, 128, 42, BLACK); x = 0; lastx = 0;}
  int ySignal = stoss; 
  if (ySignal > 100000) ySignal = 100000;
  if (ySignal < 10000) ySignal = 10000;
  int ySignalMap = map(ySignal, 10000, 100000, 0, 40); //--> The y-axis used on OLEDs is from 0 to 40
  y = 40 - ySignalMap;
  display1.writeLine(lastx,lasty,x,y,WHITE);
  display1.display(); 
  lastx = x;
  lasty = y;
  x++;
}
/***********************************************************************************************************************************/
void visualcompass(int x) {
  degrees1 = x; //Main source
  display1.fillRect(0, 0, 128, 64, BLACK);      //Blanks out last frame to black
  display1.setTextColor(WHITE);
  display1.setTextSize(2);
  display1.setCursor(74,25);                                       // Position of text                            
  display1.fillCircle(hSpin, vSpin, SpinRadius / 2, WHITE);        // Draws a white filled circle
  display1.fillCircle(hSpin, vSpin, (SpinRadius / 2) - 1 , BLACK); // draws a black circle to refresh line movement
  display1.setCursor(56,4);            
  display1.println(F("N"));
  display1.setCursor(32,26);             
  display1.println(F("W"));
  display1.setCursor(78,26);           
  display1.println(F("E"));
  display1.setCursor(56,47);             
  display1.println(F("S"));
  float heading = degrees1;
  float bearingRad = heading/57.2957795;   //convert degree to radian
  int armY = armLength*cos(bearingRad);
  int armX = -armLength*sin(bearingRad);
  display1.drawLine(hSpin, vSpin, hSpin-armX, vSpin-armY, WHITE);
  display1.display();         
}
/***********************************************************************************************************************************/
void batterylevel() {
  sensorValue = analogRead(2);
  float voltage = ((sensorValue / 4095) * 2 * 3.3 * calibration); //multiply by two as voltage divider network is 100K & 100K Resistor
  bat_percentage = (uint8_t)(((voltage - BATTV_MIN) / (BATTV_MAX - BATTV_MIN)) * 100);
  unsigned long currentMillis = millis();
   if(currentMillis - previousMillis > interval1) {
    previousMillis = currentMillis; 
    if (bat_percentage >= 100){bat_percentage = 100;}
    if (bat_percentage <= 0) {bat_percentage = 1;}
    if (voltage >= 4.2){voltage = 4.2;}
    if (voltage <= 3.2) {voltage = 3.2;}
    totalV = totalV + voltage; 
    totalP = totalP + bat_percentage; 
    batsec++;
   }
  if (batsec == 10){
  totalV = totalV / 10; totalP = totalP / 10; 
  totalVV = totalV; totalPP = totalP;
  totalV = 0; totalP = 0; batsec = 0;
  }
}
/***********************************************************************************************************************************/
void EnableWifi(){
  if(Ewifi == 1){
    display1.drawBitmap(29, 0, bitWifi, 16, 16, WHITE); 
    WiFi.setSleep(false);
    WiFi.disconnect(false);  // Reconnect the network
    WiFi.mode(WIFI_STA);  
    }
  else if(Ewifi == 0){
    display1.setCursor(29,4); display1.print("D");
    WiFi.setSleep(true);     
    WiFi.disconnect(true);  // Disconnect from the network
    WiFi.mode(WIFI_OFF);    // Switch WiFi off
    }
}
/***********************************************************************************************************************************/
/*void EnableBluetooth(){
  if(Ebluetooth == 1){
    display1.drawBitmap(12, 0, bitBluetooth, 16, 16, WHITE); 
    btStart();
    }
  else if(Ebluetooth == 0){
    display1.setCursor(12,4); display1.print("D");
    btStop();
    }
}*/
/***********************************************************************************************************************************/
void gpsstatus(){
  if(ss.available() > 0){gps.encode(ss.read()); ssValid = 20;}
  if(ss.available() <= 0){ssValid = 0; timeValid = 0; dateValid = 0; satValid = 0; locValid = 0;}
  if((gps.time.isValid() == true || gps.time.isUpdated() == true) && gps.encode(ss.read())){timeValid = 20;} 
  if((gps.date.isValid() == true || gps.date.isUpdated() == true) && gps.encode(ss.read())){dateValid = 20;} 
  if((gps.satellites.isValid() == true || gps.satellites.isUpdated() == true) && gps.encode(ss.read())){satValid = 20;} 
  if((gps.location.isValid() == true || gps.location.isUpdated() == true) && gps.encode(ss.read())){locValid = 20;} 
}
/***********************************************************************************************************************************/
void sleepermenu(){
    if (sleepsecM++ >= sleeptimemenu){
      if (screenE == 1){display1.ssd1306_command(SSD1306_DISPLAYOFF);}
      display.setTextSize(3);display.setTextColor(WHITE);display.setCursor(1,0);
      display.clearDisplay();
      display.print("-------");
      display.print(" SLEEP "); 
      display.print("-------");
      display.display(); delay(2000); 
      display.clearDisplay();
      display.display(); 
      display.ssd1306_command(SSD1306_DISPLAYOFF);
      sleepsecM = 0;
      esp_light_sleep_start();
    }  
    esp_sleep_enable_gpio_wakeup();
    gpio_wakeup_enable(GPIO_NUM_10, GPIO_INTR_LOW_LEVEL);
    gpio_wakeup_enable(GPIO_NUM_9, GPIO_INTR_LOW_LEVEL);
    gpio_wakeup_enable(GPIO_NUM_8, GPIO_INTR_LOW_LEVEL);
    gpio_wakeup_enable(GPIO_NUM_5, GPIO_INTR_LOW_LEVEL);
    gpio_wakeup_enable(GPIO_NUM_4, GPIO_INTR_LOW_LEVEL);
    display.ssd1306_command(SSD1306_DISPLAYON);
    if (screenE == 1){display1.ssd1306_command(SSD1306_DISPLAYON);}
}

/***********************************************************************************************************************************/
void draw_second(int second, int mode){
   yb1= (24*cos(pi-(2*pi)/60*second))+clock_center_y;
   xb1 =(24*sin(pi-(2*pi)/60*second))+clock_center_x;
   if (mode==1) display.drawCircle(xb1, yb1, 2, SSD1306_WHITE); 
   else display.drawCircle(xb1, yb1, 2, SSD1306_BLACK);
}

void draw_hour(int hour, int minute, int mode){
  
   yb1= (18*cos(pi-(2*pi)/12*hour-(2*PI)/720*minute))+clock_center_y;
   xb1 =(18*sin(pi-(2*pi)/12*hour-(2*PI)/720*minute))+clock_center_x;
   yw1=(18*cos(pi-(2*pi)/12*hour-(2*PI)/720*minute))+clock_center_y+1;
   xw1=(18*sin(pi-(2*pi)/12*hour-(2*PI)/720*minute))+clock_center_x+1;
   if (mode==1){
    display.drawLine(clock_center_x,clock_center_y,xb1,yb1,SSD1306_WHITE);
    display.drawLine(clock_center_x+1,clock_center_y+1,xw1,yw1,SSD1306_WHITE);
   }
   else{
    display.drawLine(clock_center_x,clock_center_y,xb1,yb1,SSD1306_BLACK);
    display.drawLine(clock_center_x+1,clock_center_y+1,xw1,yw1,SSD1306_BLACK);
   }  
}
void draw_minute(int minute, int mode){
   yb1= (24*cos(pi-(2*pi)/60*minute))+clock_center_y;
   xb1 =(24*sin(pi-(2*pi)/60*minute))+clock_center_x;
   if (mode==1)display.drawLine(clock_center_x,clock_center_y,xb1,yb1,SSD1306_WHITE); else display.drawLine(clock_center_x,clock_center_y,xb1,yb1,SSD1306_BLACK);
}

void draw_clock_face(void){
// draw the center of the clock
  display.drawCircle(clock_center_x, clock_center_y,3, SSD1306_WHITE);
  display.fillCircle(clock_center_x, clock_center_y,3, SSD1306_WHITE);

// draw hour pointers around the face of a clock
for (int i=0;i<12;i++){
   yb1 = (32*cos(pi-(2*pi)/12*i))+clock_center_y;
   xb1 =(32*sin(pi-(2*pi)/12*i))+clock_center_x;
   yw1= (28*cos(pi-(2*pi)/12*i))+clock_center_y;
   xw1 =(28*sin(pi-(2*pi)/12*i))+clock_center_x;
   display.drawLine(xw1,yw1,xb1,yb1,SSD1306_WHITE);
}
// print string "12" at the top of the face of the clock  
  display.drawCircle(26*sin(pi)+clock_center_x, (26*cos(pi))+clock_center_y, 6, SSD1306_BLACK);
  display.fillCircle(26*sin(pi)+clock_center_x, (26*cos(pi))+clock_center_y, 5, SSD1306_BLACK);
  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(clock_center_x-3,0);             // Start at top-left corner
  display.println(F("12"));
  display.setCursor(clock_center_y-3,28);             // Start at top-left corner
  display.println(F("9"));
  display.setCursor(3*(clock_center_y),28);             // Start at top-left corner
  display.println(F("3"));
  display.setCursor(clock_center_x-3,56);             // Start at top-left corner
  display.println(F("6"));
}
/***********************************************************************************************************************************/
void drawCalendar(){
  // display a full month on a calendar 
  display.setTextSize(1);display1.setTextColor(WHITE);
  display.clearDisplay();
  display.setCursor(2,0);
  display.print("Su Mo Tu We Th Fr Sa"); 
  // display this month
  DateTime now = rtc1.now();
  // get number of days in month
  if (now.month() == 1 || now.month() == 3 || now.month() == 5 || now.month() == 7 || now.month() == 8 || now.month() == 10 || now.month() == 12){
    monthLength = 31;
  }
  else {monthLength = 30;}
  if(now.month() == 2){monthLength = 28;}
  startDay = startDayOfWeek(now.year(),now.month(),1); // Sunday's value is 0
  // now build first week string
  switch (startDay){
    case 0:
      // Sunday
      week1 = " 1  2  3  4  5  6  7";
      break;
    case 1:
      // Monday
      week1 = "    1  2  3  4  5  6";
      break;      
     case 2:
      // Tuesday
      week1 = "       1  2  3  4  5";
      break;           
     case 3:
      // Wednesday
      week1 = "          1  2  3  4";
      break;  
     case 4:
      // Thursday
      week1 = "             1  2  3";
      break; 
     case 5:
      // Friday
      if(monthLength == 28 || monthLength == 30){week1 = "                1  2";}      
      if(monthLength == 31){week1 = "31              1  2";}      
      break; 
     case 6:
      // Saturday
      if(monthLength == 28){week1 = "                   1";}
      if(monthLength == 30){week1 = "30                 1";}      
      if(monthLength == 31){week1 = "30 31              1";}       
      
      break;           
  } // end first week
  newWeekStart = (7-startDay)+1;
  const char* newWeek1 = (const char*) week1.c_str();  
  display.setCursor(2,9);
  display.print(newWeek1); 
  // display week 2
  week2 ="";
  for (int f = newWeekStart; f < newWeekStart + 7; f++){
    if(f<10){
      week2 = week2 +  " " + String(f) + " ";
    }  
    else{week2 = week2 + String(f) + " ";}    
  }
  const char* newWeek2 = (const char*) week2.c_str();  
  display.setCursor(2,18);
  display.print(newWeek2); 
  // display week 3
  newWeekStart = (14-startDay)+1; 
  week3 ="";
  for (int f = newWeekStart; f < newWeekStart + 7; f++){
    if(f<10){
      week3 = week3 +  " " + String(f) + " ";
    }  
    else{week3 = week3 + String(f) + " ";}    
  }
  const char* newWeek3 = (const char*) week3.c_str();  
  display.setCursor(2,27);
  display.print(newWeek3);     
  // display week 4
  newWeekStart = (21-startDay)+1; 
  week4 ="";
  for (int f = newWeekStart; f < newWeekStart + 7; f++){
    if(f<10){
      week4 = week4 +  " " + String(f) + " ";
    }  
    else{week4 = week4 + String(f) + " ";}    
    }
   const char* newWeek4 = (const char*) week4.c_str();  
  display.setCursor(2,36);
  display.print(newWeek4); 
   // do we need a fifth week
   week5="";
   newWeekStart = (28-startDay)+1;   
   // is is February?
   if(newWeekStart > 28 && now.month() == 2){
   // do nothing unless its a leap year
     if (now.year()==(now.year()/4)*4){ // its a leap year
       week5 = "29";
     }       
   }
   else{ // print up to 30 anyway
     if(now.month() == 2){  // its February
       for (int f = newWeekStart; f < 29; f++){
         week5 = week5 + String(f) + " ";  
       }  
       // is it a leap year
       if (now.year()==(now.year()/4)*4){ // its a leap year
         week5 = week5 + "29";
       }        
     }
     else{
       for (int f = newWeekStart; f < 31; f++){
         week5 = week5 + String(f) + " ";
       }
       // are there 31 days
       if (monthLength == 31 && week5.length() <7){
         week5 = week5 + "31"; 
       } 
     } 
   }
   const char* newWeek5 = (const char*) week5.c_str(); 
  display.setCursor(2,45);
  display.print(newWeek5);
  display.display(); 
 } 
/***********************************************************************************************************************************/
// calculate first day of month
int startDayOfWeek(int y, int m, int d){
  static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
  y -= m < 3;
  return (y +y/4 -y/100 + y/400 + t[m-1] + d)% 7; 
} 
/***********************************************************************************************************************************/
void checkLines(){
  boolean full;
  for(short y = 17; y >= 0; y--){
    full = true;
    for(short x = 0; x < 10; x++){
      full = full && grid[x][y];
    }
    if(full){
      breakLine(y);
      y++;
    }
  }
}

void breakLine(short line){
  for(short y = line; y >= 0; y--){
    for(short x = 0; x < 10; x++){
      grid[x][y] = grid[x][y-1];
    }
  }
  for(short x = 0; x < 10; x++){
     grid[x][0] = 0;
  }
  display.invertDisplay(true);
  delay(50);
  display.invertDisplay(false);
  score += 10;
}

void refresh(){
    display.clearDisplay();
    drawLayout();
    drawGrid();
    drawPiece(currentType, 0, pieceX, pieceY);
    display.display();
}

void drawGrid(){
  for(short x = 0; x < 10; x++)
    for(short y = 0; y < 18; y++)
      if(grid[x][y])
        display.fillRect(MARGIN_LEFT + (SIZE + 1)*x, MARGIN_TOP + (SIZE + 1)*y, SIZE, SIZE, WHITE);
}

boolean nextHorizontalCollision(short piece[2][4], int amount){
  for(short i = 0; i < 4; i++){
    short newX = pieceX + piece[0][i] + amount;
    if(newX > 9 || newX < 0 || grid[newX][pieceY + piece[1][i]])
      return true;
  }
  return false;
}

boolean nextCollision(){

  for(short i = 0; i < 4; i++){
    short y = pieceY + piece[1][i] + 1;
    short x = pieceX + piece[0][i];
    if(y > 17 || grid[x][y])
      return true;
  }

  return false;
}

void generate(){
  currentType = nextType;
  nextType = random(TYPES);
  if(currentType != 5)
    pieceX = random(9);
  else
    pieceX = random(7);
  pieceY = 0;
  rotation = 0;
  copyPiece(piece, currentType, rotation);
}

void drawPiece(short type, short rotation, short x, short y){
  for(short i = 0; i < 4; i++)
    display.fillRect(MARGIN_LEFT + (SIZE + 1)*(x + piece[0][i]), MARGIN_TOP + (SIZE + 1)*(y + piece[1][i]), SIZE, SIZE, WHITE);
}

void drawNextPiece(){
  short nPiece[2][4];
  copyPiece(nPiece, nextType, 0);
  for(short i = 0; i < 4; i++)
    display.fillRect(50 + 3*nPiece[0][i], 4 + 3*nPiece[1][i], 2, 2, WHITE);
}

void copyPiece(short piece[2][4], short type, short rotation){
  switch(type){
  case 0: //L_l
    for(short i = 0; i < 4; i++){
      piece[0][i] = pieces_L_l[rotation][0][i];
      piece[1][i] = pieces_L_l[rotation][1][i];
    }
    break;
   case 1: //S_l
    for(short i = 0; i < 4; i++){
      piece[0][i] = pieces_S_l[rotation][0][i];
      piece[1][i] = pieces_S_l[rotation][1][i];
    }
    break;
   case 2: //S_r
    for(short i = 0; i < 4; i++){
      piece[0][i] = pieces_S_r[rotation][0][i];
      piece[1][i] = pieces_S_r[rotation][1][i];
    }
    break;
   case 3: //Sq
    for(short i = 0; i < 4; i++){
      piece[0][i] = pieces_Sq[0][0][i];
      piece[1][i] = pieces_Sq[0][1][i];
    }
    break;
    case 4: //T
    for(short i = 0; i < 4; i++){
      piece[0][i] = pieces_T[rotation][0][i];
      piece[1][i] = pieces_T[rotation][1][i];
    }
    break;
    case 5: //l
    for(short i = 0; i < 4; i++){
      piece[0][i] = pieces_l[rotation][0][i];
      piece[1][i] = pieces_l[rotation][1][i];
    }
    break;
  }
}

short getMaxRotation(short type){
  if(type == 1 || type == 2 || type == 5)
    return 2;
  else if(type == 0 || type == 4)
    return 4;
  else if(type == 3)
    return 1;
  else 
    return 0;
}

boolean canRotate(short rotation){
  short piece[2][4];
  copyPiece(piece, currentType, rotation);
  return !nextHorizontalCollision(piece, 0);
}

void drawLayout(){
  display.drawLine(0, 15, WIDTH, 15, WHITE);
  display.drawRect(0, 0, WIDTH, HEIGHT, WHITE);
  drawNextPiece();
  char text[6];
  itoa(score, text, 10);
  drawText(text, getNumberLength(score), 7, 4);
}

short getNumberLength(int n){
  short counter = 1;
  while(n >= 10){
    n /= 10;
    counter++;
  }
  return counter;
}

void drawText(char text[], short length, int x, int y){
  display.setTextSize(1);      // Normal 1:1 pixel scale
  display.setTextColor(WHITE); // Draw white text
  display.setCursor(x, y);     // Start at top-left corner
  display.cp437(true);         // Use full 256 char 'Code Page 437' font

  for(short i = 0; i < length; i++)
  display.write(text[i]);
}

/***********************************************************************************************************************************/
void drawCourt() {
    display.drawRect(0, 0, 128, 64, WHITE);
}
/***********************************************************************************************************************************/
int weekday(int year, int month, int day)
/* Calculate day of week in proleptic Gregorian calendar. Sunday == 0. */
{
  int adjustment, mm, yy;
  if (year<2000) year+=2000;
  adjustment = (14 - month) / 12;
  mm = month + 12 * adjustment - 2;
  yy = year - adjustment;
  return (day + (13 * mm - 1) / 5 +
    yy + yy / 4 - yy / 100 + yy / 400) % 7;
}
/***********************************************************************************************************************************/
void drawMoon(void){
  DateTime now = rtc1.now();
  display.setTextSize(1);display1.setTextColor(WHITE);
  display.clearDisplay();
  display.setCursor(2,0);
  display.print("Moon Phase Calculator");
  display.drawLine(0,13,128,13, WHITE);
  int mp = moon_phase();
  switch (mp){
  case 0:
  display.setCursor(15,56);
  display.print(" Full Moon ");
  display.drawBitmap(45, 18, full_moon_bits, 30, 30, WHITE);
  break;
  case 1:
  display.setCursor(15,56);
  display.print("Waning Gibbous");
  display.drawBitmap(45, 18, waning_gibbous_bits, 30, 30, WHITE);
  break;
  case 2:
  display.setCursor(15,56);
  display.print(" Last Quarter ");
  display.drawBitmap(45, 18, last_quarter_bits, 30, 30, WHITE);
  break;
  case 3:
  display.setCursor(15,56);
  display.print(" Old Crescent ");
  display.drawBitmap(45, 18, crescent_old_bits, 30, 30, WHITE);
  break;
  case 4:
  display.setCursor(15,56);
  display.print(" New Moon ");
  display.drawBitmap(45, 18, new_moon_bits, 30, 30, WHITE);
  break;
  case 5:
  display.setCursor(15,56);
  display.print(" New Crescent ");
  display.drawBitmap(45, 18, crescent_new_bits, 30, 30, WHITE);
  break;
  case 6:
  display.setCursor(15,56);
  display.print( " First Quarter");
  display.drawBitmap(45, 18, first_quarter_bits, 30, 30, WHITE);
  break;
  case 7:
  display.setCursor(15,56);
  display.print("Waxing Gibbous");
  display.drawBitmap(45, 18, waxing_gibbous_bits, 30, 30, WHITE);
  break;
  }
  const char* newNfm = (const char*) nfm.c_str();
  display.setCursor(110,30);
  display.print(newNfm);
  display.display(); 
  }
   
  int moon_phase(){
  // calculates the age of the moon phase(0 to 7)
  // there are eight stages, 0 is full moon and 4 is a new moon
  DateTime now = rtc1.now();
  double jd = 0; // Julian Date
  double ed = 0; //days elapsed since start of full moon
  int b= 0;
  jd = julianDate(now.year(), now.month(), now.day());
  //jd = julianDate(1972,1,1); // used to debug this is a new moon
  jd = int(jd - 2244116.75); // start at Jan 1 1972
  jd /= 29.53; // divide by the moon cycle
  b = jd;
  jd -= b; // leaves the fractional part of jd
  ed = jd * 29.53; // days elapsed this month
  nfm = String((int(29.53 - ed))); // days to next full moon
  b = jd*8 +0.5;
  b = b & 7;
  return b;
   
  }
  double julianDate(int y, int m, int d){
  // convert a date to a Julian Date}
  int mm,yy;
  double k1, k2, k3;
  double j;
   
  yy = y- int((12-m)/10);
  mm = m+9;
  if(mm >= 12) {
  mm = mm-12;
  }
  k1 = 365.25 *(yy +4172);
  k2 = int((30.6001 * mm) + 0.5);
  k3 = int((((yy/100) + 4) * 0.75) -38);
  j = k1 +k2 + d + 59;
  j = j-k3; // j is the Julian date at 12h UT (Universal Time)
  return j;
}
