#include "U8glib.h" U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); int xmax=128; int ymax=62; int xcenter=xmax/2; int ycenter=ymax/2+10; int arc=ymax/2; int angle=0; char* label[] = {"LOAD","COOLANT","INTAKE", "VOLT"}; int labelXpos[] = {53, 45, 49, 53}; #define potmeterPin A1 int p, w, m,a=10; u8g_uint_t xx=0; int chargePin = PB2; // Piezo part // these constants won't change: const int knockSensor = A0; // the piezo is connected to analog pin 0 const int knockSensor1 = A1;// the piezo is connected to analog pin 1 const int threshold = 100; // threshold value to decide when the detected sound is a knock or not // these variables will change: int sensorReading = 0; // variable to store the value read from the sensor pin 0 int sensorReading1 = 0; // variable to store the value read from the sensor pin 1 #include #include void lowPower(); // LiFuelGauge constructor parameters // 1. IC type, MAX17043 or MAX17044 // 2. Number of interrupt to which the alert pin is associated (Optional) // 3. ISR to call when an alert interrupt is generated (Optional) // // Creates a LiFuelGauge instance for the MAX17043 IC // and attaches lowPower to INT0 (PIN2 on most boards, PIN3 on Leonardo) LiFuelGauge gauge(MAX17043, 3, lowPower); // A flag to indicate a generated alert interrupt volatile boolean alert = false; #define u8g_logo_width 128 #define u8g_logo_height 64 static unsigned char u8g_logo_bits[] U8G_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, 0x04, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x80, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x80, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x1e, 0x0f, 0x80, 0x3b, 0x0f, 0x0f, 0x8f, 0x19, 0x9f, 0x1b, 0x8f, 0x8d, 0x1f, 0x0f, 0x00, 0x06, 0x9f, 0x1f, 0x80, 0xbf, 0x9f, 0x9f, 0x9f, 0x19, 0xbf, 0xbf, 0x9f, 0x9f, 0x9f, 0x1f, 0x00, 0x86, 0x81, 0x31, 0x80, 0xb6, 0x31, 0x9e, 0x81, 0x19, 0x83, 0xb6, 0xb1, 0x19, 0x86, 0x01, 0x00, 0x86, 0x81, 0x3f, 0x80, 0xb6, 0xbf, 0x3f, 0x87, 0x19, 0x83, 0xb6, 0xbf, 0x19, 0x06, 0x07, 0x00, 0x86, 0x81, 0x3f, 0x80, 0xb0, 0xbf, 0x31, 0x9c, 0x19, 0x83, 0xb6, 0xbf, 0x19, 0x06, 0x1c, 0x00, 0x86, 0x81, 0x01, 0x80, 0xb0, 0x81, 0x39, 0x98, 0x19, 0x83, 0xb6, 0x81, 0x19, 0x06, 0x18, 0x80, 0x1f, 0x9f, 0x1f, 0x80, 0xb0, 0x9f, 0xbf, 0x9f, 0x1f, 0x83, 0xb6, 0x9f, 0x19, 0x9e, 0x1f, 0x80, 0x1f, 0x1e, 0x1f, 0x80, 0x30, 0x1f, 0x37, 0x0f, 0x1f, 0x83, 0x36, 0x9f, 0x19, 0x1c, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x80, 0x0f, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x83, 0x19, 0x0f, 0x0f, 0x8f, 0x87, 0x0d, 0x37, 0x80, 0x19, 0x8f, 0xb1, 0x07, 0x1e, 0x0f, 0x80, 0x81, 0x99, 0x9f, 0x9f, 0x1f, 0x87, 0x9f, 0x3f, 0x80, 0xb1, 0x9f, 0x19, 0x07, 0x9f, 0x1f, 0x80, 0x99, 0x99, 0xb1, 0x81, 0x01, 0x86, 0x99, 0x39, 0x80, 0xb1, 0xb1, 0x19, 0x86, 0x81, 0x31, 0x80, 0xb9, 0x99, 0x3f, 0x07, 0x07, 0x86, 0x99, 0x39, 0x80, 0xb1, 0x3f, 0x19, 0x86, 0x81, 0x3f, 0x80, 0xb1, 0x99, 0x3f, 0x1c, 0x1c, 0x86, 0x99, 0x39, 0x80, 0xb1, 0x3f, 0x1b, 0x86, 0x81, 0x3f, 0x80, 0xb3, 0x99, 0x01, 0x18, 0x18, 0x86, 0x99, 0x39, 0x80, 0x99, 0x01, 0x0f, 0x86, 0x81, 0x01, 0x00, 0xbf, 0x9f, 0x9f, 0x9f, 0x1f, 0x9e, 0x99, 0x3f, 0x80, 0x9f, 0x1f, 0x0e, 0x1e, 0x9f, 0x1f, 0x00, 0x1e, 0x1f, 0x1f, 0x0f, 0x0f, 0xbc, 0x19, 0x1f, 0x80, 0x0f, 0x1f, 0x0e, 0x3c, 0x1e, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; void gauge1(uint8_t angle) { // draw border of the gauge u8g.drawCircle(xcenter,ycenter,arc+6, U8G_DRAW_UPPER_RIGHT); u8g.drawCircle(xcenter,ycenter,arc+4, U8G_DRAW_UPPER_RIGHT); u8g.drawCircle(xcenter,ycenter,arc+6, U8G_DRAW_UPPER_LEFT); u8g.drawCircle(xcenter,ycenter,arc+4, U8G_DRAW_UPPER_LEFT); // draw the needle float x1=sin(2*angle*2*3.14/360); float y1=cos(2*angle*2*3.14/360); u8g.drawLine(xcenter, ycenter, xcenter+arc*x1, ycenter-arc*y1); u8g.drawDisc(xcenter, ycenter, 5, U8G_DRAW_UPPER_LEFT); u8g.drawDisc(xcenter, ycenter, 5, U8G_DRAW_UPPER_RIGHT); u8g.setFont(u8g_font_chikita); // show scale labels u8g.drawStr( 20, 42, "0"); u8g.drawStr( 25, 18, "25"); u8g.drawStr( 60, 14, "50"); u8g.drawStr( 95, 18, "75"); u8g.drawStr( 105, 42, "100"); // show gauge label u8g.setPrintPos(labelXpos[0],32); u8g.print(label[0]); // show digital value and align its position u8g.setFont(u8g_font_profont22); u8g.setPrintPos(54,60); if (w<10){ u8g.print("0"); } if (w>99) { u8g.setPrintPos(47,60); } u8g.print(w); } void setup(void) { u8g.setFont(u8g_font_chikita); u8g.setColorIndex(1); // assign default color value if ( u8g.getMode() == U8G_MODE_R3G3B2 ) { u8g.setColorIndex(255); } else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) { u8g.setColorIndex(3); } else if ( u8g.getMode() == U8G_MODE_BW ) { u8g.setColorIndex(1); } else if ( u8g.getMode() == U8G_MODE_HICOLOR ) { u8g.setHiColorByRGB(255,255,255); } Serial.begin(9600); // Initializes serial port // Waits for serial port to connect. Needed for Leonardo only //while ( !Serial ) ; gauge.reset(); // Resets MAX17043 delay(200); // Waits for the initial measurements to be made pinMode(chargePin, INPUT_PULLUP); // Sets the Alert Threshold to 10% of full capacity gauge.setAlertThreshold(10); Serial.println(String("Alert Threshold is set to ") + gauge.getAlertThreshold() + '%'); } void loop(void) { if (digitalRead(chargePin) == LOW ) { chargeBatt(); } else { // u8g.firstPage(); // do { // u8g.drawXBMP( 0, 0, u8g_logo_width, u8g_logo_height, u8g_logo_bits); // } while( u8g.nextPage() ); // // clearOLED(); sensorReading = analogRead(knockSensor); sensorReading1 = analogRead(knockSensor1); if (sensorReading >= threshold) { // u8g.firstPage(); // do { // u8g.drawXBMP( 0, 0, u8g_logo_width, u8g_logo_height, u8g_logo_bits); // } while( u8g.nextPage() ); //delay (2000); clearOLED(); u8g.firstPage(); do { u8g.setFont(u8g_font_gdr25r); //u8g.drawStr(8, 30, "Eeee"); char buf[9]; sprintf (buf, "%d", sensorReading); u8g.drawStr(18, 32, buf); //char buf[9]; sprintf (buf, "%d", sensorReading1); u8g.drawStr(18, 62, buf); } while( u8g.nextPage() ); // delay(200); } } } void chargeBatt() { // p = analogRead(D2); p = (gauge.getSOC())*10.24-120; w = map(p,0,1023,0,100); m = map(p,0,1023,0,90); // show needle and dial xx = m; if (xx<45){ xx=xx+135; } else { xx=xx-45; } // picture loop { u8g.firstPage(); do { gauge1(xx); } while( u8g.nextPage() ); } Serial.print("SOC: "); Serial.print(gauge.getSOC()); // Gets the battery's state of charge Serial.print("%, VCELL: "); Serial.print(gauge.getVoltage()); // Gets the battery voltage Serial.println('V'); if ( alert ) { Serial.println("Beware, Low Power!"); Serial.println("Finalizing operations..."); gauge.clearAlertInterrupt(); // Resets the ALRT pin alert = false; Serial.println("Storing data..."); Serial.println("Sending notification..."); Serial.println("System operations are halted..."); gauge.sleep(); // Forces the MAX17043 into sleep mode while ( true ) float cos = (gauge.getSOC()); float cis = (gauge.getVoltage()); } } void clearOLED(){ u8g.firstPage(); do { } while( u8g.nextPage() ); } void lowPower() { alert = true; }