Grove OLED Display Code Error

Hello! I recently purchased an Arduino WiFi Rev 2, Grove Base Shield, and Grove 0.96" OLED Display. To learn the basics, I started a program that writes the status of oxygen and battery power (I'm a sci-fi nerd; think space suit display :P) and then simulates them dropping using delay().

I got the display to work if I just write a simple string (something in quotations), but I need to convert an integer variable to a string variable, and then display that. I #included the "string" library, and then my code stopped wanting to compile. I get "Compilation error: Error: 2 UNKNOWN: exit status 1". I also have Visual Studio 2019 installed on my laptop, and I suspect it may be interfering with the Arduino IDE somehow because of some errors referencing Visual Studio or other things I have installed within it, but I haven't confirmed that.

Any help would be appreciated!

Here is a copy of my code:
Space_Wrist_Display.ino (1.1 KB)

#include <Arduino>
#include <U8g2lib>
#include <string>
 
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SSD1306_128X64_ALT0_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // SSD1306 and SSD1308Z are compatible
 
// U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);    //Low spped I2C

int oxygenPercentage = 100, powerPercentage = 100;
const int charWidth = 5, barWidth = 17, titleWidth = 4 + 1, barScale = 100 / barWidth;

void setup(void) {
  u8g2.begin();
}
 
void loop(void) {
  int x;
  std::string oxygenString, powerString;
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_profont12_mf);
  u8g2.setFontMode(1);
  u8g2.setDrawColor(0);
  u8g2.setContrast(255);

  x = charWidth * titleWidth;

  oxygenString = std::to_string(oxygenPercentage) + "%";
  u8g2.drawStr(0,8,"OXY:");
  u8g2.drawStr(x,8,oxygenString);

  powerString = std::to_string(powerPercentage) + "%";
  u8g2.drawStr(0,18,"BAT:");
  u8g2.drawStr(x,18,powerString);
  
  u8g2.sendBuffer();
  delay(1000);
  
  oxygenPercentage -= 3;
  powerPercentage -= 1;
}

Your code.

#include <Arduino>
#include <U8g2lib>
#include <string>
 
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SSD1306_128X64_ALT0_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // SSD1306 and SSD1308Z are compatible
 
// U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);    //Low spped I2C

int oxygenPercentage = 100, powerPercentage = 100;
const int charWidth = 5, barWidth = 17, titleWidth = 4 + 1, barScale = 100 / barWidth;

void setup(void) {
  u8g2.begin();
}
 
void loop(void) {
  int x;
  std::string oxygenString, powerString;
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_profont12_mf);
  u8g2.setFontMode(1);
  u8g2.setDrawColor(0);
  u8g2.setContrast(255);

  x = charWidth * titleWidth;

  oxygenString = std::to_string(oxygenPercentage) + "%";
  u8g2.drawStr(0,8,"OXY:");
  u8g2.drawStr(x,8,oxygenString);

  powerString = std::to_string(powerPercentage) + "%";
  u8g2.drawStr(0,18,"BAT:");
  u8g2.drawStr(x,18,powerString);
  
  u8g2.sendBuffer();
  delay(1000);
  
  oxygenPercentage -= 3;
  powerPercentage -= 1;
}

Much easier to read, don't you think?

1 Like

Much easier to read, don't you think?

Yes, thanks! I wasn't sure how to copy/paste code blocks like that, or if it would take up too much space.

Edit: Found it.