Beginner who wants to migrate to platformIO but a lot of things that I do not understand (my sketch works on Arduino IDE)

hi,

i recently use Platform.IO inside Vscode to dev for teensy 4 ,it’s been a week that I’m trying to understand … but I can not do it all.

Simple example blink work, but anothers code like Encoders dont.

my .ini:
41
my project tree:


my src file:

my encoder example code:

the errors platformIO:

the code in arduino IDE compile and upload perfectly but not in platformIO.
Other question why I have to notify the SPI and Wire libraries in lib_debs knowing that they are in the folder of Framework-arduinoteensy ?
/Users/myName/.platformio/packages/framework-arduinoteensy/libraries/

Now i can’t back to Arduino IDE cause i love vscode and platformIO its very better !

thank you for your help, i would be eternally grateful !

cheer from France

T

The issue you describe is publicly known.(Compile errors when using Teensy4 · Issue #44 · PaulStoffregen/Encoder · GitHub). The same type of issue is known here: Teensy 4.0 and FastLED compiler errors · Issue #49 · platformio/platform-teensy · GitHub. The problem is that you’re using an outtdated library version which activates the wrong IO_REG_TYPE definitions. This version of the library doesn’t even yet know about the Teensy4. Especially, it’s missing this commit which fixes the issue: Add IMXRT · PaulStoffregen/Encoder@cfe6541 · GitHub. Since then the library author has not updated the library version so PlatformIO will also not pick up an updated version.

There are two ways to solve this problem:

  • it seems you have the Encoder library installed in the local library storage via the VSCode UI. This is not necessary since the encoder library is already integrated in the framework-arduino package. If you remove it, it should take the fixed version from the Arduino Package.
  • force usage of the updated version; that is, in the platformio.ini in the lib_deps directive, instead of saying Encoder give it the git link https://github.com/PaulStoffregen/Encoder.git
3 Likes

Thank you very mutch MaxGerhardt !
I really appreciate your speed to reply! and now its working !

now i have another problem i realize that platformio has problems interpreting functions that are used in loop function for example in this code:

//To use the OLED we need to include the following libraries
#include "Arduino.h"
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//We then need to define the pins we are using on the Arduino
#define OLED_DC 11
#define OLED_CS 12
#define OLED_CLK 10
#define OLED_MOSI 9
#define OLED_RESET 13

//#define LOGO16_GLCD_HEIGHT 16
//#define LOGO16_GLCD_WIDTH  16
static const unsigned char PROGMEM battery[] =
{
  B00000000, B00000000, B00000000,
  B00011100, B00000000, B01110000,
  B00011100, B00000000, B01110000,
  B01111111, B11111111, B11111110,
  B11111111, B11111111, B11111111,
  B11111111, B11111111, B11111111,
  B11111111, B11111111, B11111111,
  B11111111, B11111111, B11101111,
  B11111111, B11111111, B11101111,
  B11000001, B11111111, B10000011,
  B11111111, B11111111, B11101111,
  B11111111, B11111111, B11101111,
  B11111111, B11111111, B11111111,
  B11111111, B11111111, B11111111,
  B11111111, B11111111, B11111111,
  B11111111, B11111111, B11111111,
};

//Initialize the display
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

//Initialise the sensor input and pressure variable in PSI for the displa
float val = 0;
float voltage = 0;
int x = 5; //digit display centring variable.
int limit = 15; //pressure limit
float z = 10.13;  // Analog read divider (1024 / 10.13 = 100 steps)
float z1 = 10.13; // bar graph divider  (check because I am not 100% sure)

//Set up the following
void setup()
{
  Serial.begin(9600); //To comunicate with the PC
  display.begin(SSD1306_SWITCHCAPVCC); //To supply the 3.3V power supply
  pinMode(0, INPUT); //Set analog pin 0 as an input from the sensor
  display.clearDisplay(); //Clear the display and ram
  delay(100);
  display.display(); //Show the cleared display to prevent for a smooth transition
  delay(100);


}

void loop()
{
  val = (analogRead(0) / z1);  // bar graph variable

  voltage = ((analogRead(0) / z)+8);

  drawOILPRESS(); //This runs the command specified below inside the loop
}

void drawOILPRESS(void)
{

  display.setTextColor(WHITE);
  display.setTextSize(5);

// check value of pressure and centre display based on that value
  if (voltage < 10)
  {
x = 18;   // single digit position
  }
  else if (voltage < 100)
  {
x = 18;   // double digit position
  }
  else
  {
x = 18;   // triple digit position
  }
  ;



// Print to screen.
  display.setCursor(x, 0);
  display.println(voltage);
  display.setTextSize(1);
  display.setCursor(80, 38);
  display.println("VOLTS");
  display.drawRect(10, 49, 102, 15, 1); //Border of the bar chart
  display.fillRect(11, 50, val, 13, WHITE); //Draws the bar depending on the sensor value



// Draw limit line on bar graph display

  if (voltage >= limit)
  {
display.drawLine((limit + 10), 49, (limit + 10), 63, 0); // black limit line on bar graph
  }
  if (voltage <= limit)
  {
display.drawLine((limit + 10), 49, (limit + 10), 63, 1); // white limit line on bar graph
display.setTextSize(1);
display.setCursor(50, 53);
display.println("ALARM");  // print "ALARM" on display if below limit value.
  }

  display.drawBitmap(13, 26,  battery, 24, 16, 1);
  display.display();

  display.display();
  display.clearDisplay();

  delay (100); //delay loop to slow down update speed

}

drawOILPRESS(); This runs the command specified below inside the loop but when i compinling in platformio i have this :

Same error in this code :
#include “SPI.h”
#include “Wire.h”
#include “Adafruit_GFX.h”
#include “Arduino.h”
#include “Adafruit_SSD1306.h”
//
#define OLED_RESET 4
Adafruit_SSD1306 Display(OLED_RESET);

int r = 0;
int i = 0;

void setup()   {


  Display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
  Display.display();
  delay (1000);

  DrawTitles();

}


void loop() {

  // get some dummy data to display


  r = rand() / 220;
  //r = analogRead(ADJ_PIN);
  //r = r / 7.98;

  Display.setTextSize(1);
  // note set the background color or the old text will still display
  Display.setTextColor(WHITE, BLACK);
  Display.setCursor(0, 33);
  Display.println(Format(r * 7.99 / 204.6, 3, 2));

  //draw the bar graph
  Display.fillRect(r, 50, 128 - r, 10, BLACK);
  Display.fillRect(3, 50, r, 10, WHITE);

  for (i = 1; i < 13; i++) {
    Display.fillRect(i * 10, 50, 2, 10, BLACK);
  }

  Display.display();


}
void DrawTitles (void) {

  Display.setTextSize(1);
  Display.setTextColor(WHITE);
  Display.setCursor(0, 0);
  Display.println("ENCODERS TST");
  Display.setTextSize(1);
  Display.setTextColor(WHITE);
  Display.setCursor(0, 19);
  Display.println("Bargraph.ino0.0");
  Display.display();

}

String Format(double val, int dec, int dig ) {

  int addpad = 0;
  char sbuf[20];
  String fdata = (dtostrf(val, dec, dig, sbuf));
  int slen = fdata.length();
  for ( addpad = 1; addpad <= dec + dig - slen; addpad++) {
    fdata = " " + fdata;
  }
  return (fdata);

} 

19

should I be able to download other extention to implement a vscode so that it matches the arduino environment?
cause in Arduino IDE compiling perfectly…

Exactly, the code was meant to be an .ino file and not a .cpp file; The Arduino IDE does pre-processing on the file, namely, scanning the file for every function implementation and declaring it at the top of the file.

This is invalid C++ code (but valid .ino because of the preprocessing):

void setup() {
   doStuff();
}

void doStuff() { 
  Serial.println("Doing stuff..");
}

because at the time of usage, doStuff() is not previously declared, thus unknown to the compiler. This is fixed by adding the function declaration void doStuff(); over the setup() function.

You can:

  • rename your main.cpp file to main.ino to make PIO also do the same preprocessing
  • or fix the source by making it actually valid C++; see the documentation / FAQ
2 Likes

Maxgerhardt, thank you very much for your response always so fast!

I saw that it worked ! thank you it teaches me a good thing!

I’m going to dig into the platformIO doc to make increments of code, because in my old file nomenclature, my “main.src” was calling different “code” without me having to type all the code in the main Src file

Thank you so mutch

cheer

1 Like