OLED boost gauge code

Here is the code that I am currently running, gives the boost in bar, with atmospheric compensation. It also is set up to keep the figures in the same relative postion with or without the negative sign. I have truncated the u8glib entries, if you have a different screen, you will need to copy the right constructor line from the u8glib hello world example.

----------------------------------------------------------------------------------------------------------------------------------------------------------

#include <Adafruit_BMP085.h>
#include <Wire.h>
#include "U8glib.h"

int mapsen = 0; // Set MAP sensor input on Analog port 0
float boost = 0; // Set boost value to 0
float mapval = 0; // Set raw map value to 0
float atm = 0; //initial air pressure compensation

Adafruit_BMP085 bmp;

// setup u8g object, please remove comment from one of the following constructor calls
// IMPORTANT NOTE: The following list is incomplete. The complete list of supported
// devices with all constructor calls is here: https://github.com/olikraus/u8glib/wiki/device
//U8GLIB_SSD1306_128X64 u8g(13, 11, 10, 9); // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_SSD1306_128X64 u8g(4, 5, 6, 7);  // SW SPI Com: SCK = 4, MOSI = 5, CS = 6, A0 = 7 (new white HalTec OLED)
//U8GLIB_SSD1306_128X64 u8g(10, 9);   // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are  SCK = 13 and MOSI = 11)
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);  // I2C / TWI
//U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST); // Fast I2C / TWI
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);  // Display which does not send AC
//U8GLIB_SSD1306_ADAFRUIT_128X64 u8g(13, 11, 10, 9);  // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_SSD1306_ADAFRUIT_128X64 u8g(10, 9);    // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are  SCK = 13 and MOSI = 11)

void draw(void) {
  // graphic commands to redraw the complete screen should be placed here  
  u8g.setFont(u8g_font_unifont);
  //u8g.setFont(u8g_font_osb21);
  u8g.drawStr( 0, 12, "BOOST");
  u8g.drawStr( 105, 55, "BAR");
  //u8g.setPrintPos(19, 40);
  //u8g.print(atm, 2);   
  u8g.setFont(u8g_font_fub30n);
  if (boost >= 0) {
  u8g.setPrintPos(19, 55);
  u8g.print(boost, 2);
  }
  else if (boost < 0) {
  u8g.setPrintPos(5, 55);
  u8g.print(boost, 2);
  }
 
}

void setup(void) {
   Serial.begin(9600);
  if (!bmp.begin()) {
  Serial.println("Could not find a valid BMP085 sensor, check wiring!");
  while (1) {}
  }
  // flip screen, if required
   u8g.setRot180();
 
  // set SPI backup if required
  //u8g.setHardwareBackup(u8g_backup_avr_spi);

  // assign default color value
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }
}

void loop()
{
atm = (float)(bmp.readPressure());
mapval= analogRead(mapsen); //Reads the MAP sensor raw value on analog port 0
boost = (((mapval*(4.863)/(.022)+20000))-(atm))/100000; //Converts raw MAP value to bar
// picture loop
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
 
  // rebuild the picture after some delay
  delay(50);
}

------------------------------------------------------------------------------------------------------------------------------------

A link to the actual file:

OLED_boost_baro.ino

A more detailed testing file with extra comments and output.

OLED boost_map_baro_temp.ino

 

Note that I don't trust the code, as I cannot work out how the formula for getting tha MAP reading was worked out. I have since re-written this to use the formula given in the MAP sensors data sheet. I will put up the priduction code after cleaning it up, as it is currently outputting a lot of readings to the serial port for sanity testing.