arduinoconfigurationarduino-c++platformio

How do you use <string> and <iostream> with PlatformIO in VSCode:


i've been trying to use and with PlatformIO for my "simple oled GUI". And i'm running into trouble including those libraries without any errors. Note that im a beginner in VScode and PlatformIO. Thanks in advance for any advice incoming :) Here is my code (not finished bcs of the issues):

#include <Arduino.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
#include <SPI.h>

#define outputA 2
#define outputB 3

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADRESS 0x3D
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const int numpages = 3; //number of pages
int thispage = 0;
int counter = 0;
int aState = 0;
int aLastState = 0;

int textX0= 9;
int textY0 = 34;
int textX1 = 9; //default 
int textY1 = 34; //default
int textY2 = 34;
int txtsize = 1;


void setup() {
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
 
Serial.begin (9600);
aLastState = digitalRead(outputA);

display.drawRect(4,22,121,19,WHITE); //Select rectangle
display.drawLine(7,42,1,1,848484); //Outline
}
 
void loop() {
aState = digitalRead(outputA); //Rotary encoder, saved into the "counter" variable 
if (aState != aLastState){
if (digitalRead(outputB) != aState) {
counter ++;
} else {
counter --;
}
Serial.print("Position: "); 
Serial.println(counter);
thispage = counter/2;
}
if(thispage < 0) {
    thispage = numpages;
}else if (thispage > numpages){
    thispage = 0;
}

textX0 = textX1;
textY2 = textY1;
textY2 = textY1;

if(thispage == 0){
    textX0 = 6;
    textY0 = 18;
    textX1 = 9; 
    textY1 = 34;
    textY2 = 56;
    txtsize = 2;
}else{
    textX0 = 9;
    textY0 = 18;
    textX1 = 9;
    textY1 = 34;
    textY2 = 56;
    txtsize = 1;
}

    display.setTextSize(txtsize);
    display.setCursor(textX0,textY0);
    display.setTextColor(WHITE);
    display.print("menu1");
    display.setTextSize(1);
    display.setCursor(textX1,textY1);   
    display.write("menu2");
    display.setCursor(textX1,textY2);
    display.write("menu3");


display.print(thispage);
aLastState = aState; 
display.clearDisplay();

}

and here is my c_cpp_properties.json file:

{
    "configurations": [
        {
            "name": "PlatformIO",
            "includePath": [
                "c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/include",
                "c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/src",
                "c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/.pio/libdeps/nano/Adafruit SSD1306",
                "c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/.pio/libdeps/nano/Adafruit-TFT-LCD",
                "c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/.pio/libdeps/nano/Adafruit GFX Library",
                "c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/.pio/libdeps/nano/Adafruit BusIO",
                "C:/Users/danie/.platformio/packages/framework-arduino-avr/libraries/Wire/src",
                "c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/.pio/libdeps/nano/GFX Library for Arduino/src",
                "C:/Users/danie/.platformio/packages/framework-arduino-avr/libraries/SPI/src",
                "c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/.pio/libdeps/nano/Adafruit TouchScreen",
                "C:/Users/danie/.platformio/packages/framework-arduino-avr/cores/arduino",
                "C:/Users/danie/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs",
                "C:/Users/danie/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
                "C:/Users/danie/.platformio/packages/framework-arduino-avr/libraries/HID/src",
                "C:/Users/danie/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src",
                ""
            ],
            "browse": {
                "limitSymbolsToIncludedHeaders": true,
                "path": [
                    "c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/include",
                    "c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/src",
                    "c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/.pio/libdeps/nano/Adafruit SSD1306",
                    "c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/.pio/libdeps/nano/Adafruit-TFT-LCD",
                    "c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/.pio/libdeps/nano/Adafruit GFX Library",
                    "c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/.pio/libdeps/nano/Adafruit BusIO",
                    "C:/Users/danie/.platformio/packages/framework-arduino-avr/libraries/Wire/src",
                    "c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/.pio/libdeps/nano/GFX Library for Arduino/src",
                    "C:/Users/danie/.platformio/packages/framework-arduino-avr/libraries/SPI/src",
                    "c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/.pio/libdeps/nano/Adafruit TouchScreen",
                    "C:/Users/danie/.platformio/packages/framework-arduino-avr/cores/arduino",
                    "C:/Users/danie/.platformio/packages/framework-arduino-avr/variants/eightanaloginputs",
                    "C:/Users/danie/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
                    "C:/Users/danie/.platformio/packages/framework-arduino-avr/libraries/HID/src",
                    "C:/Users/danie/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src",
                    ""
                ]
            },
            "defines": [
                "PLATFORMIO=60109",
                "ARDUINO_AVR_NANO",
                "F_CPU=16000000L",
                "ARDUINO_ARCH_AVR",
                "ARDUINO=10808",
                "__AVR_ATmega328P__",
                ""
            ],
            "cStandard": "gnu11",
            "cppStandard": "gnu++11",
            "compilerPath": "C:/Users/danie/.platformio/packages/toolchain-atmelavr/bin/avr-gcc.exe",
            "compilerArgs": [
                "-mmcu=atmega328p",
                ""
            ]
        }
    ],
    "version": 4
}

I have consulted these errors with ChatGpt to no extent. I did everything this tutorial said, so i have g++, i can make a new project where and work, but i cannot use it with PlatformIO. I did edit the c_cpp_properties.json, but thats about it.

Here are the errors i have been talking about:

[{
    "resource": "/c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/src/olednano.cpp",
    "owner": "C/C++: IntelliSense",
    "code": "1696",
    "severity": 8,
    "message": "#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (C:\\Users\\danie\\Documents\\Arduino\\Touchscreen UI\\UI_test_touch\\src\\olednano.cpp).",
    "source": "C/C++",
    "startLineNumber": 6,
    "startColumn": 1,
    "endLineNumber": 6,
    "endColumn": 20
}]

and

[{
    "resource": "/c:/Users/danie/Documents/Arduino/Touchscreen UI/UI_test_touch/src/olednano.cpp",
    "owner": "C/C++: IntelliSense",
    "code": "1696",
    "severity": 8,
    "message": "cannot open source file \"iostream\". Please run the 'Select IntelliSense Configuration...' command to locate your system headers.",
    "source": "C/C++",
    "startLineNumber": 6,
    "startColumn": 1,
    "endLineNumber": 6,
    "endColumn": 20
}]

Solution

  • It looks like you are using an Arduino Uno or a similar devices. These devices do not have sufficient memory to implement the standard C++ string and IO classes. Thus, the Arduino people have created replacements that fit into memory.

    So instead of std::string use String and instead of std::ostream use Stream (Serial is a Stream instance).