PlatformIO Community

Beginner: code works in arduino IDE, but not in platformio?

Hi everybody,

so far, I only cloned projects that I then built via platformio; now I am trying to finally learn to work with my arduinos and write my own code.

I am trying to create functions, googled some code, and modified it to this

void setup() {
  Serial.begin(9600);
  
  meins();
  //Serial.println("| Program Menu |");
}

void loop() {
}

void meins()
{
  Serial.println("ok");
  delay(500);
  Serial.println("yo!");
}

When I upload this via arduino ide to my Arduino Nano Atmega328, it will work: the monitor will display “ok”, wait 500ms, then display “yo!”, then stop doing anything.

So then I created a platformio project for this.

platformio.ini

[env:nanoatmega328]
platform = atmelavr
board = nanoatmega328
framework = arduino
monitor_speed = 9600
upload_port = /dev/ttyUSB0

src/main.cpp

#include <Arduino.h>

void setup() {
  Serial.begin(9600);
  meins();
}

void loop() {
}

void meins()
{
  Serial.println("ok");
  delay(500);
  Serial.println("yo!");
}

When I try upload and monitor I receive the output from below. Why is that? The code seems to be correct, the hardware information ought to be correct… what am I doing wrong?

Again, this very code will run via arduino ide; I assumed that it’d work in pio as well, as long as I #include <Arduino.h>

Thank you for your feedback :slight_smile:

Processing nanoatmega328 (platform: atmelavr; board: nanoatmega328; framework: arduino)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/nanoatmega328.html
PLATFORM: Atmel AVR 2.0.0 > Arduino Nano ATmega328
HARDWARE: ATMEGA328P 16MHz, 2KB RAM, 30KB Flash
PACKAGES: 
 - framework-arduino-avr 5.0.0 
 - tool-avrdude 1.60300.190424 (6.3.0) 
 - toolchain-atmelavr 1.50400.190710 (5.4.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 5 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio/build/nanoatmega328/src/main.cpp.o
src/main.cpp: In function 'void setup()':
src/main.cpp:5:9: error: 'meins' was not declared in this scope
   meins();
         ^
Archiving .pio/build/nanoatmega328/libFrameworkArduino.a
*** [.pio/build/nanoatmega328/src/main.cpp.o] Error 1
Indexing .pio/build/nanoatmega328/libFrameworkArduino.a
========================================================================================================== [FAILED] Took 0.37 seconds ==========================================================================================================

Hi,

Has the compiler tell you in the output, your “meins()” function has to be declared before you called it.

Simply move your “mein()” function at the begeinning, or add this line before the “setup()” function:

void meins();

If not, the compiler will not understand what function you are talking about.

Thank you! It is still weird to me that I don’t have to do this in the arduino ide, but it works fine :slight_smile:

A more detailed explanation here

It seems that ArduinoIDE is doing a lot of work backstage to be as mush “easy-to-use” as possible, like doing forward declarations by itself ^^